Submission #41319

# Submission time Handle Problem Language Result Execution time Memory
41319 2018-02-15T22:52:13 Z nibnalin Election Campaign (JOI15_election_campaign) C++14
100 / 100
411 ms 172892 KB
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

const int maxn = int(2e5)+5, maxlog = 19;

int n, ctr = 0, st[4*maxn+10], start[maxn], en[maxn], H[maxn], dp[maxn], T[maxn][maxlog+1];
vector<int> graph[maxn];
vector<int> C[maxn];
vector<pair<int, int>> A[maxn];
vector<pair<int, int>> B[maxn];

inline int left(int node) { return (node<<1); }
inline int right(int node) { return (node<<1)+1; }

void upd(int node, int L, int R, int a, int b, int v)
{
	if(a > R || b < L) return;
	else if(a <= L && R <= b) st[node] += v;
	else
	{
		upd(left(node), L, (L+R)/2, a, b, v);
		upd(right(node), (L+R)/2+1, R, a, b, v);
	}
}

int qry(int node, int L, int R, int a)
{
	if(L == R) return st[node];
	else
	{
		if(a <= (L+R)/2) return st[node]+qry(left(node), L, (L+R)/2, a);
		else return st[node]+qry(right(node), (L+R)/2+1, R, a);
	}
}

void dfs0(int node, int par, int ht)
{
	H[node] = ht, T[node][0] = par;
	start[node] = ctr++;
	for(auto it: graph[node])
	{
		if(it != par) dfs0(it, node, ht+1);
	}
	en[node] = ctr++;
}

void init()
{
	for(int i = 1;i <= maxlog;i++)
	{
		for(int j = 0;j < n;j++)
		{
			if(T[j][i-1] != -1) T[j][i] = T[T[j][i-1]][i-1];
		}
	}
}

pair<int, pair<int, int>> LCA(int x, int y)
{
	bool latswap = 0;
	if(H[x] > H[y]) swap(x, y), latswap = 1;
	int kidy = y;
	for(int i = maxlog;i >= 0;i--)
	{
		if(H[y]-(1<<i) >= H[x]) y = T[y][i];
		if(H[kidy]-(1<<i) > H[x]) kidy = T[kidy][i];
	}

	if(x == y)
	{
		if(latswap) return {x, {kidy, -1}};
		else return {x, {-1, kidy}};
	}

	for(int i = maxlog;i >= 0;i--)
	{
		if(T[x][i] != T[y][i]) x = T[x][i], y = T[y][i];
	}
	if(latswap) swap(x, y);
	return {T[x][0], {x, y}};
}

void dfs1(int node, int par)
{
	int s = 0;
	dp[node] = 0;
	for(auto it: graph[node])
	{
		if(it != par)
		{
			dfs1(it, node);
			s += dp[it];
		}
	}
	dp[node] = max(dp[node], s);

	for(int i = 0;i < int(A[node].size());i++)
	{
		int x = A[node][i].first, y = A[node][i].second, c = C[node][i], xx = B[node][i].first, yy = B[node][i].second;
		int tmp = qry(1, 0, maxn-1, start[x])+qry(1, 0, maxn-1, start[y])+c+s;
		tmp -= (xx != -1)*dp[xx]+(yy != -1)*dp[yy];
		dp[node] = max(dp[node], tmp);
	}
	
	upd(1, 0, maxn-1, start[node], start[node], s);
	for(auto it: graph[node])
	{
		if(it != par) upd(1, 0, maxn-1, start[it], en[it], s-dp[it]);
	}
}

int main(void)
{
	int m, x, y, c;
	scanf("%d", &n);
	for(int i = 1;i < n;i++)
	{
		scanf("%d%d", &x, &y);
		x--, y--;
		graph[x].push_back(y), graph[y].push_back(x);
	}

	for(int i = 0;i <= n;i++)
	{
		for(int j = 0;j <= maxlog;j++) T[i][j] = -1;
	}

	dfs0(0, -1, 0);
	init();

	scanf("%d", &m);
	for(int i = 0;i < m;i++)
	{
		scanf("%d%d%d", &x, &y, &c);
		x--, y--;
		pair<int, pair<int, int>> l = LCA(x, y);
		//cout << l.first << " " << l.second.first << " " << l.second.second << "\n";
		A[l.first].push_back({x, y}), C[l.first].push_back(c), B[l.first].push_back(l.second);
	}

	dfs1(0, -1);
	printf("%d\n", dp[0]);
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:117:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
                 ^
election_campaign.cpp:120:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &x, &y);
                        ^
election_campaign.cpp:133:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &m);
                 ^
election_campaign.cpp:136:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &x, &y, &c);
                              ^
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19064 KB Output is correct
2 Correct 16 ms 19172 KB Output is correct
3 Correct 16 ms 19280 KB Output is correct
4 Correct 18 ms 19428 KB Output is correct
5 Correct 190 ms 35360 KB Output is correct
6 Correct 129 ms 47224 KB Output is correct
7 Correct 185 ms 47224 KB Output is correct
8 Correct 138 ms 47224 KB Output is correct
9 Correct 183 ms 47224 KB Output is correct
10 Correct 146 ms 47224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 47224 KB Output is correct
2 Correct 16 ms 47224 KB Output is correct
3 Correct 19 ms 47224 KB Output is correct
4 Correct 271 ms 58736 KB Output is correct
5 Correct 278 ms 61288 KB Output is correct
6 Correct 319 ms 63724 KB Output is correct
7 Correct 280 ms 66048 KB Output is correct
8 Correct 285 ms 68500 KB Output is correct
9 Correct 252 ms 71100 KB Output is correct
10 Correct 275 ms 73568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 47224 KB Output is correct
2 Correct 16 ms 47224 KB Output is correct
3 Correct 19 ms 47224 KB Output is correct
4 Correct 271 ms 58736 KB Output is correct
5 Correct 278 ms 61288 KB Output is correct
6 Correct 319 ms 63724 KB Output is correct
7 Correct 280 ms 66048 KB Output is correct
8 Correct 285 ms 68500 KB Output is correct
9 Correct 252 ms 71100 KB Output is correct
10 Correct 275 ms 73568 KB Output is correct
11 Correct 46 ms 73568 KB Output is correct
12 Correct 275 ms 76508 KB Output is correct
13 Correct 323 ms 79176 KB Output is correct
14 Correct 266 ms 82236 KB Output is correct
15 Correct 286 ms 84676 KB Output is correct
16 Correct 264 ms 87532 KB Output is correct
17 Correct 269 ms 90256 KB Output is correct
18 Correct 272 ms 92920 KB Output is correct
19 Correct 251 ms 95744 KB Output is correct
20 Correct 277 ms 98316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 278 ms 98316 KB Output is correct
2 Correct 258 ms 103420 KB Output is correct
3 Correct 411 ms 103420 KB Output is correct
4 Correct 229 ms 103420 KB Output is correct
5 Correct 354 ms 106024 KB Output is correct
6 Correct 268 ms 106024 KB Output is correct
7 Correct 373 ms 110016 KB Output is correct
8 Correct 307 ms 110016 KB Output is correct
9 Correct 261 ms 120820 KB Output is correct
10 Correct 379 ms 120820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19064 KB Output is correct
2 Correct 16 ms 19172 KB Output is correct
3 Correct 16 ms 19280 KB Output is correct
4 Correct 18 ms 19428 KB Output is correct
5 Correct 190 ms 35360 KB Output is correct
6 Correct 129 ms 47224 KB Output is correct
7 Correct 185 ms 47224 KB Output is correct
8 Correct 138 ms 47224 KB Output is correct
9 Correct 183 ms 47224 KB Output is correct
10 Correct 146 ms 47224 KB Output is correct
11 Correct 19 ms 120820 KB Output is correct
12 Correct 21 ms 120820 KB Output is correct
13 Correct 19 ms 120820 KB Output is correct
14 Correct 18 ms 120820 KB Output is correct
15 Correct 18 ms 120820 KB Output is correct
16 Correct 18 ms 120820 KB Output is correct
17 Correct 18 ms 120820 KB Output is correct
18 Correct 18 ms 120820 KB Output is correct
19 Correct 17 ms 120820 KB Output is correct
20 Correct 20 ms 120820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19064 KB Output is correct
2 Correct 16 ms 19172 KB Output is correct
3 Correct 16 ms 19280 KB Output is correct
4 Correct 18 ms 19428 KB Output is correct
5 Correct 190 ms 35360 KB Output is correct
6 Correct 129 ms 47224 KB Output is correct
7 Correct 185 ms 47224 KB Output is correct
8 Correct 138 ms 47224 KB Output is correct
9 Correct 183 ms 47224 KB Output is correct
10 Correct 146 ms 47224 KB Output is correct
11 Correct 17 ms 47224 KB Output is correct
12 Correct 16 ms 47224 KB Output is correct
13 Correct 19 ms 47224 KB Output is correct
14 Correct 271 ms 58736 KB Output is correct
15 Correct 278 ms 61288 KB Output is correct
16 Correct 319 ms 63724 KB Output is correct
17 Correct 280 ms 66048 KB Output is correct
18 Correct 285 ms 68500 KB Output is correct
19 Correct 252 ms 71100 KB Output is correct
20 Correct 275 ms 73568 KB Output is correct
21 Correct 46 ms 73568 KB Output is correct
22 Correct 275 ms 76508 KB Output is correct
23 Correct 323 ms 79176 KB Output is correct
24 Correct 266 ms 82236 KB Output is correct
25 Correct 286 ms 84676 KB Output is correct
26 Correct 264 ms 87532 KB Output is correct
27 Correct 269 ms 90256 KB Output is correct
28 Correct 272 ms 92920 KB Output is correct
29 Correct 251 ms 95744 KB Output is correct
30 Correct 277 ms 98316 KB Output is correct
31 Correct 278 ms 98316 KB Output is correct
32 Correct 258 ms 103420 KB Output is correct
33 Correct 411 ms 103420 KB Output is correct
34 Correct 229 ms 103420 KB Output is correct
35 Correct 354 ms 106024 KB Output is correct
36 Correct 268 ms 106024 KB Output is correct
37 Correct 373 ms 110016 KB Output is correct
38 Correct 307 ms 110016 KB Output is correct
39 Correct 261 ms 120820 KB Output is correct
40 Correct 379 ms 120820 KB Output is correct
41 Correct 19 ms 120820 KB Output is correct
42 Correct 21 ms 120820 KB Output is correct
43 Correct 19 ms 120820 KB Output is correct
44 Correct 18 ms 120820 KB Output is correct
45 Correct 18 ms 120820 KB Output is correct
46 Correct 18 ms 120820 KB Output is correct
47 Correct 18 ms 120820 KB Output is correct
48 Correct 18 ms 120820 KB Output is correct
49 Correct 17 ms 120820 KB Output is correct
50 Correct 20 ms 120820 KB Output is correct
51 Correct 320 ms 120820 KB Output is correct
52 Correct 281 ms 128808 KB Output is correct
53 Correct 385 ms 128808 KB Output is correct
54 Correct 236 ms 128808 KB Output is correct
55 Correct 321 ms 128808 KB Output is correct
56 Correct 284 ms 139764 KB Output is correct
57 Correct 363 ms 139764 KB Output is correct
58 Correct 235 ms 139764 KB Output is correct
59 Correct 293 ms 139764 KB Output is correct
60 Correct 287 ms 150900 KB Output is correct
61 Correct 360 ms 150900 KB Output is correct
62 Correct 243 ms 150900 KB Output is correct
63 Correct 280 ms 150900 KB Output is correct
64 Correct 272 ms 161788 KB Output is correct
65 Correct 377 ms 161788 KB Output is correct
66 Correct 235 ms 161788 KB Output is correct
67 Correct 287 ms 161788 KB Output is correct
68 Correct 279 ms 172892 KB Output is correct
69 Correct 348 ms 172892 KB Output is correct
70 Correct 230 ms 172892 KB Output is correct