Submission #103633

# Submission time Handle Problem Language Result Execution time Memory
103633 2019-04-01T15:05:33 Z luciocf Election Campaign (JOI15_election_campaign) C++14
100 / 100
392 ms 27356 KB
#include <bits/stdc++.h>
 
using namespace std;
 
const int maxn = 1e5+10;
 
int n, m;
 
int A[maxn], B[maxn], C[maxn];
 
int nivel[maxn], pai[maxn], sz[maxn];
 
int chain[maxn], head[maxn], pos[maxn];
int cc, pp;
 
int bit[2][maxn];
 
int dp[maxn];
 
vector<int> grafo[maxn], op[maxn];
 
void upd(int x, int v, int t)
{
	for (; x < maxn; x += (x&-x))
		bit[t][x] += v;
}
 
int soma(int x, int t)
{
	int ans = 0;
	for (; x > 0; x -= (x&-x))
		ans += bit[t][x];
	return ans;
}
 
void dfs(int u, int p)
{
	sz[u] = 1;
 
	for (auto v: grafo[u])
	{
		if (v == p) continue;
 
		pai[v] = u, nivel[v] = nivel[u]+1;
 
		dfs(v, u);
 
		sz[u] += sz[v];
	}
}
 
void hld(int u)
{
	if (!head[cc]) head[cc] = u;
	chain[u] = cc, pos[u] = ++pp;
 
	int maior = -1, ind = -1;
	for (auto v: grafo[u])
		if (v != pai[u] && sz[v] > maior)
			maior = sz[v], ind = v;
 
	if (ind != -1) hld(ind);
 
	for (auto v: grafo[u])
		if (v != pai[u] && v != ind)
			cc++, hld(v);
}
 
int lca(int u, int v)
{
	while (chain[u] != chain[v])
	{
		if (nivel[head[chain[u]]] > nivel[head[chain[v]]])
			u = pai[head[chain[u]]];
		else
			v = pai[head[chain[v]]];
	}
 
	if (nivel[u] > nivel[v]) return v;
	return u;
}
 
int query(int u, int v, int t)
{
	int ans = 0;
 
	while (true)
	{
		if (chain[u] == chain[v]) return ans+soma(pos[u], t)-soma(pos[v]-1, t);
 
		ans += soma(pos[u], t)-soma(pos[head[chain[u]]]-1, t);
		u = pai[head[chain[u]]];
	}
}
 
int solve(int u, int p)
{
	int sum = 0;
	for (auto v: grafo[u])
	{
		if (v == p) continue;
 
		solve(v, u);
 
		sum += dp[v];
	}

	upd(pos[u], sum, 0);
 
	dp[u] = sum;
 
	for (auto i: op[u])
	{
		int a = A[i], b = B[i], c = C[i];
		int x = query(a, u, 0) + query(b, u, 0) - sum;

		dp[u] = max(dp[u], c+x-query(a, u, 1)-query(b, u, 1));
	}

	upd(pos[u], dp[u], 1);
}
 
int main(void)
{
	scanf("%d", &n);
 
	for (int i = 1; i < n; i++)
	{
		int u, v;
		scanf("%d %d", &u, &v);
 
		grafo[u].push_back(v); grafo[v].push_back(u);
	}
 
	dfs(1, 0);
	hld(1);
 
	scanf("%d", &m);
 
	for (int i = 1; i <= m; i++)
	{
		scanf("%d %d %d", &A[i], &B[i], &C[i]);
 
		op[lca(A[i], B[i])].push_back(i);
	}
 
	solve(1, 0);
 
	printf("%d\n", dp[1]);
}

Compilation message

election_campaign.cpp: In function 'int solve(int, int)':
election_campaign.cpp:121:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:125:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:130:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~~
election_campaign.cpp:138:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &m);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:142:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &A[i], &B[i], &C[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 7 ms 5248 KB Output is correct
5 Correct 164 ms 12904 KB Output is correct
6 Correct 69 ms 23392 KB Output is correct
7 Correct 145 ms 19708 KB Output is correct
8 Correct 122 ms 13340 KB Output is correct
9 Correct 172 ms 17544 KB Output is correct
10 Correct 133 ms 13304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 7 ms 5248 KB Output is correct
4 Correct 140 ms 24696 KB Output is correct
5 Correct 144 ms 24440 KB Output is correct
6 Correct 146 ms 24540 KB Output is correct
7 Correct 171 ms 24616 KB Output is correct
8 Correct 140 ms 24548 KB Output is correct
9 Correct 166 ms 24552 KB Output is correct
10 Correct 146 ms 24588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 7 ms 5248 KB Output is correct
4 Correct 140 ms 24696 KB Output is correct
5 Correct 144 ms 24440 KB Output is correct
6 Correct 146 ms 24540 KB Output is correct
7 Correct 171 ms 24616 KB Output is correct
8 Correct 140 ms 24548 KB Output is correct
9 Correct 166 ms 24552 KB Output is correct
10 Correct 146 ms 24588 KB Output is correct
11 Correct 17 ms 5760 KB Output is correct
12 Correct 153 ms 27356 KB Output is correct
13 Correct 151 ms 27260 KB Output is correct
14 Correct 173 ms 27352 KB Output is correct
15 Correct 145 ms 27256 KB Output is correct
16 Correct 175 ms 27320 KB Output is correct
17 Correct 149 ms 27220 KB Output is correct
18 Correct 147 ms 27256 KB Output is correct
19 Correct 152 ms 27244 KB Output is correct
20 Correct 143 ms 27312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 392 ms 13368 KB Output is correct
2 Correct 182 ms 24540 KB Output is correct
3 Correct 333 ms 20476 KB Output is correct
4 Correct 280 ms 13812 KB Output is correct
5 Correct 310 ms 19832 KB Output is correct
6 Correct 216 ms 13972 KB Output is correct
7 Correct 284 ms 19448 KB Output is correct
8 Correct 333 ms 13816 KB Output is correct
9 Correct 150 ms 24696 KB Output is correct
10 Correct 270 ms 18220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 7 ms 5248 KB Output is correct
5 Correct 164 ms 12904 KB Output is correct
6 Correct 69 ms 23392 KB Output is correct
7 Correct 145 ms 19708 KB Output is correct
8 Correct 122 ms 13340 KB Output is correct
9 Correct 172 ms 17544 KB Output is correct
10 Correct 133 ms 13304 KB Output is correct
11 Correct 11 ms 5248 KB Output is correct
12 Correct 10 ms 5376 KB Output is correct
13 Correct 10 ms 5348 KB Output is correct
14 Correct 8 ms 5248 KB Output is correct
15 Correct 8 ms 5248 KB Output is correct
16 Correct 7 ms 5248 KB Output is correct
17 Correct 6 ms 5248 KB Output is correct
18 Correct 8 ms 5248 KB Output is correct
19 Correct 10 ms 5248 KB Output is correct
20 Correct 9 ms 5376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 7 ms 5248 KB Output is correct
5 Correct 164 ms 12904 KB Output is correct
6 Correct 69 ms 23392 KB Output is correct
7 Correct 145 ms 19708 KB Output is correct
8 Correct 122 ms 13340 KB Output is correct
9 Correct 172 ms 17544 KB Output is correct
10 Correct 133 ms 13304 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 8 ms 5120 KB Output is correct
13 Correct 7 ms 5248 KB Output is correct
14 Correct 140 ms 24696 KB Output is correct
15 Correct 144 ms 24440 KB Output is correct
16 Correct 146 ms 24540 KB Output is correct
17 Correct 171 ms 24616 KB Output is correct
18 Correct 140 ms 24548 KB Output is correct
19 Correct 166 ms 24552 KB Output is correct
20 Correct 146 ms 24588 KB Output is correct
21 Correct 17 ms 5760 KB Output is correct
22 Correct 153 ms 27356 KB Output is correct
23 Correct 151 ms 27260 KB Output is correct
24 Correct 173 ms 27352 KB Output is correct
25 Correct 145 ms 27256 KB Output is correct
26 Correct 175 ms 27320 KB Output is correct
27 Correct 149 ms 27220 KB Output is correct
28 Correct 147 ms 27256 KB Output is correct
29 Correct 152 ms 27244 KB Output is correct
30 Correct 143 ms 27312 KB Output is correct
31 Correct 392 ms 13368 KB Output is correct
32 Correct 182 ms 24540 KB Output is correct
33 Correct 333 ms 20476 KB Output is correct
34 Correct 280 ms 13812 KB Output is correct
35 Correct 310 ms 19832 KB Output is correct
36 Correct 216 ms 13972 KB Output is correct
37 Correct 284 ms 19448 KB Output is correct
38 Correct 333 ms 13816 KB Output is correct
39 Correct 150 ms 24696 KB Output is correct
40 Correct 270 ms 18220 KB Output is correct
41 Correct 11 ms 5248 KB Output is correct
42 Correct 10 ms 5376 KB Output is correct
43 Correct 10 ms 5348 KB Output is correct
44 Correct 8 ms 5248 KB Output is correct
45 Correct 8 ms 5248 KB Output is correct
46 Correct 7 ms 5248 KB Output is correct
47 Correct 6 ms 5248 KB Output is correct
48 Correct 8 ms 5248 KB Output is correct
49 Correct 10 ms 5248 KB Output is correct
50 Correct 9 ms 5376 KB Output is correct
51 Correct 386 ms 16616 KB Output is correct
52 Correct 146 ms 27256 KB Output is correct
53 Correct 294 ms 21040 KB Output is correct
54 Correct 225 ms 16652 KB Output is correct
55 Correct 386 ms 16248 KB Output is correct
56 Correct 153 ms 27260 KB Output is correct
57 Correct 287 ms 22060 KB Output is correct
58 Correct 237 ms 16648 KB Output is correct
59 Correct 311 ms 16476 KB Output is correct
60 Correct 151 ms 27228 KB Output is correct
61 Correct 333 ms 22008 KB Output is correct
62 Correct 234 ms 16664 KB Output is correct
63 Correct 361 ms 16208 KB Output is correct
64 Correct 162 ms 27256 KB Output is correct
65 Correct 253 ms 21972 KB Output is correct
66 Correct 208 ms 16632 KB Output is correct
67 Correct 318 ms 16340 KB Output is correct
68 Correct 144 ms 27256 KB Output is correct
69 Correct 288 ms 20384 KB Output is correct
70 Correct 226 ms 16912 KB Output is correct