Submission #550714

# Submission time Handle Problem Language Result Execution time Memory
550714 2022-04-18T21:27:30 Z nafis_shifat Election Campaign (JOI15_election_campaign) C++17
100 / 100
338 ms 55020 KB
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=3e5+5;
const ll inf=1e15;
int n;

vector<int> adj[mxn];
int level[mxn] = {};
int pa[18][mxn] = {};
int in[mxn] = {};
int T = 0;
int out[mxn];
int q;
vector<tuple<int,int,int>> con[mxn];
void dfs0(int u,int prev) {
	level[u] = level[prev]+1;
	pa[0][u] = prev;
	for(int i = 1; i < 18; i++) pa[i][u] = pa[i - 1][pa[i - 1][u]];
	for(int v : adj[u]) {
		if(v == prev) continue;
		dfs0(v,u);
	}
}


int findLCA(int u,int v) {

	if(level[u] < level[v]) swap(u,v);
	int diff = level[u] - level[v];
	for(int i = 0;i < 18;i++) if((diff>>i)&1) u = pa[i][u];
	if(u == v) return u;
	for(int i = 17; i >= 0; i--) {
		if(pa[i][u] != pa[i][v]){
			u = pa[i][u];
			v = pa[i][v];
		}
	}
	return pa[0][u];
}


ll dp[mxn] = {};
ll child_sum[mxn] = {};

ll tot[18][mxn];


ll TOT(int i, int x) {
	if(tot[i][x] != -inf) return tot[i][x];
	return tot[i][x] = TOT(i - 1, x) + TOT(i - 1, pa[i - 1][x]);
}
ll get(int x, int dif) {
	ll sum = 0;

	for(int i = 17; i >= 0; i--) {
		if((dif >> i) & 1) {
			sum += TOT(i, x);
			x = pa[i][x];
		}
	}
	return sum;

}
void dfs(int u, int prev) {

	for(int v : adj[u]) {
		if(v == prev) continue;
		dfs(v, u);
		child_sum[u] += dp[v];
	}
	dp[u] = child_sum[u];

	for(auto [a, b, c] : con[u]) {
		dp[u] = max(dp[u], get(a, level[a] - level[u]) + get(b, level[b] - level[u]) + c + child_sum[u]);
	}

	tot[0][u] = child_sum[u] - dp[u];

	
}
int main() {
	cin >> n;
	for(int i = 1; i < n; i++) {
		int u, v;
		scanf("%d%d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs0(1, 0);

	for(int i = 0; i < 18; i++) for(int j = 1; j <= n; j++) tot[i][j] = -inf;

	cin >> q;
	for(int i = 1; i <= q; i++) {
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		con[findLCA(a, b)].push_back({a, b, c});
	}

	dfs(1, 0);

	cout<<dp[1]<<endl;




	
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:87:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |   scanf("%d%d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~
election_campaign.cpp:98:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |   scanf("%d%d%d", &a, &b, &c);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14676 KB Output is correct
2 Correct 8 ms 14664 KB Output is correct
3 Correct 9 ms 14624 KB Output is correct
4 Correct 8 ms 14848 KB Output is correct
5 Correct 97 ms 41076 KB Output is correct
6 Correct 65 ms 50376 KB Output is correct
7 Correct 96 ms 47028 KB Output is correct
8 Correct 74 ms 40724 KB Output is correct
9 Correct 92 ms 45204 KB Output is correct
10 Correct 72 ms 40652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14736 KB Output is correct
2 Correct 10 ms 14676 KB Output is correct
3 Correct 9 ms 14932 KB Output is correct
4 Correct 128 ms 52300 KB Output is correct
5 Correct 133 ms 54764 KB Output is correct
6 Correct 130 ms 54668 KB Output is correct
7 Correct 134 ms 54676 KB Output is correct
8 Correct 124 ms 54764 KB Output is correct
9 Correct 119 ms 54700 KB Output is correct
10 Correct 139 ms 54604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14736 KB Output is correct
2 Correct 10 ms 14676 KB Output is correct
3 Correct 9 ms 14932 KB Output is correct
4 Correct 128 ms 52300 KB Output is correct
5 Correct 133 ms 54764 KB Output is correct
6 Correct 130 ms 54668 KB Output is correct
7 Correct 134 ms 54676 KB Output is correct
8 Correct 124 ms 54764 KB Output is correct
9 Correct 119 ms 54700 KB Output is correct
10 Correct 139 ms 54604 KB Output is correct
11 Correct 19 ms 15744 KB Output is correct
12 Correct 137 ms 54912 KB Output is correct
13 Correct 140 ms 54944 KB Output is correct
14 Correct 132 ms 55020 KB Output is correct
15 Correct 154 ms 54960 KB Output is correct
16 Correct 116 ms 54952 KB Output is correct
17 Correct 137 ms 54964 KB Output is correct
18 Correct 189 ms 54956 KB Output is correct
19 Correct 118 ms 54980 KB Output is correct
20 Correct 137 ms 54900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 175 ms 42596 KB Output is correct
2 Correct 111 ms 52524 KB Output is correct
3 Correct 338 ms 51144 KB Output is correct
4 Correct 134 ms 44744 KB Output is correct
5 Correct 211 ms 50456 KB Output is correct
6 Correct 134 ms 44608 KB Output is correct
7 Correct 288 ms 50132 KB Output is correct
8 Correct 156 ms 45220 KB Output is correct
9 Correct 133 ms 54704 KB Output is correct
10 Correct 333 ms 48872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14676 KB Output is correct
2 Correct 8 ms 14664 KB Output is correct
3 Correct 9 ms 14624 KB Output is correct
4 Correct 8 ms 14848 KB Output is correct
5 Correct 97 ms 41076 KB Output is correct
6 Correct 65 ms 50376 KB Output is correct
7 Correct 96 ms 47028 KB Output is correct
8 Correct 74 ms 40724 KB Output is correct
9 Correct 92 ms 45204 KB Output is correct
10 Correct 72 ms 40652 KB Output is correct
11 Correct 9 ms 14932 KB Output is correct
12 Correct 9 ms 15060 KB Output is correct
13 Correct 11 ms 14928 KB Output is correct
14 Correct 9 ms 14876 KB Output is correct
15 Correct 9 ms 14932 KB Output is correct
16 Correct 9 ms 14932 KB Output is correct
17 Correct 11 ms 14928 KB Output is correct
18 Correct 9 ms 14932 KB Output is correct
19 Correct 10 ms 14924 KB Output is correct
20 Correct 10 ms 15024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14676 KB Output is correct
2 Correct 8 ms 14664 KB Output is correct
3 Correct 9 ms 14624 KB Output is correct
4 Correct 8 ms 14848 KB Output is correct
5 Correct 97 ms 41076 KB Output is correct
6 Correct 65 ms 50376 KB Output is correct
7 Correct 96 ms 47028 KB Output is correct
8 Correct 74 ms 40724 KB Output is correct
9 Correct 92 ms 45204 KB Output is correct
10 Correct 72 ms 40652 KB Output is correct
11 Correct 9 ms 14736 KB Output is correct
12 Correct 10 ms 14676 KB Output is correct
13 Correct 9 ms 14932 KB Output is correct
14 Correct 128 ms 52300 KB Output is correct
15 Correct 133 ms 54764 KB Output is correct
16 Correct 130 ms 54668 KB Output is correct
17 Correct 134 ms 54676 KB Output is correct
18 Correct 124 ms 54764 KB Output is correct
19 Correct 119 ms 54700 KB Output is correct
20 Correct 139 ms 54604 KB Output is correct
21 Correct 19 ms 15744 KB Output is correct
22 Correct 137 ms 54912 KB Output is correct
23 Correct 140 ms 54944 KB Output is correct
24 Correct 132 ms 55020 KB Output is correct
25 Correct 154 ms 54960 KB Output is correct
26 Correct 116 ms 54952 KB Output is correct
27 Correct 137 ms 54964 KB Output is correct
28 Correct 189 ms 54956 KB Output is correct
29 Correct 118 ms 54980 KB Output is correct
30 Correct 137 ms 54900 KB Output is correct
31 Correct 175 ms 42596 KB Output is correct
32 Correct 111 ms 52524 KB Output is correct
33 Correct 338 ms 51144 KB Output is correct
34 Correct 134 ms 44744 KB Output is correct
35 Correct 211 ms 50456 KB Output is correct
36 Correct 134 ms 44608 KB Output is correct
37 Correct 288 ms 50132 KB Output is correct
38 Correct 156 ms 45220 KB Output is correct
39 Correct 133 ms 54704 KB Output is correct
40 Correct 333 ms 48872 KB Output is correct
41 Correct 9 ms 14932 KB Output is correct
42 Correct 9 ms 15060 KB Output is correct
43 Correct 11 ms 14928 KB Output is correct
44 Correct 9 ms 14876 KB Output is correct
45 Correct 9 ms 14932 KB Output is correct
46 Correct 9 ms 14932 KB Output is correct
47 Correct 11 ms 14928 KB Output is correct
48 Correct 9 ms 14932 KB Output is correct
49 Correct 10 ms 14924 KB Output is correct
50 Correct 10 ms 15024 KB Output is correct
51 Correct 154 ms 45476 KB Output is correct
52 Correct 179 ms 54984 KB Output is correct
53 Correct 290 ms 49408 KB Output is correct
54 Correct 114 ms 44916 KB Output is correct
55 Correct 178 ms 45260 KB Output is correct
56 Correct 159 ms 54976 KB Output is correct
57 Correct 224 ms 50120 KB Output is correct
58 Correct 149 ms 44864 KB Output is correct
59 Correct 163 ms 45448 KB Output is correct
60 Correct 130 ms 54960 KB Output is correct
61 Correct 245 ms 50300 KB Output is correct
62 Correct 139 ms 44572 KB Output is correct
63 Correct 171 ms 44972 KB Output is correct
64 Correct 141 ms 54880 KB Output is correct
65 Correct 290 ms 50140 KB Output is correct
66 Correct 121 ms 44808 KB Output is correct
67 Correct 173 ms 45156 KB Output is correct
68 Correct 140 ms 55016 KB Output is correct
69 Correct 230 ms 48796 KB Output is correct
70 Correct 130 ms 44908 KB Output is correct