Submission #890618

# Submission time Handle Problem Language Result Execution time Memory
890618 2023-12-21T16:16:01 Z iskhakkutbilim Election Campaign (JOI15_election_campaign) C++17
100 / 100
142 ms 32336 KB
#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define all(a) a.begin(), a.end()
const int N = 100000;


int n;
vector<int> g[N+5];
int timer, depth[N+10], sub[N+10];
int up[N + 10][18], bigchild[N+10], chain[N+10];
int tin[N+10], tout[N+10], pos[N+10];
void dfs(int v, int par){
	up[v][0] = par;
	tin[v] = timer++;
	sub[v] = 1;
	for(int j = 1; j < 18; j++){
		up[v][j] = up[up[v][j-1]][j-1];
	}
	for(int to : g[v]){
		if(to == par) continue;
		depth[to] = depth[v] + 1;
		dfs(to, v);
		sub[v]+= sub[to];
		if(bigchild[v] == 0 || sub[bigchild[v]] < sub[to]){
			bigchild[v] = to;
		}
	}
	
	tout[v] = timer;
}


void decompose(int v, int par, int head){
	chain[v] = head;
	pos[v] = timer++;
	if(bigchild[v]){
		decompose(bigchild[v], v, head);
	}
	for(int to : g[v]){
		if(to == par || to == bigchild[v]) continue;
		decompose(to, v, to);
	}
}

int upper(int p, int a){
	return (tin[p] <= tin[a] && tout[a] <= tout[p]);
}
int lca(int a, int b){
	if(depth[a] > depth[b]) swap(a, b);
	int k = depth[b] - depth[a];
	for(int i = 0;i < 18; i++){
		if(k & (1<<i)){
			b = up[b][i];
		}
	}
	if(a == b) return a;
	for(int i = 17; i >= 0; i--){
		if(up[a][i] != up[b][i]){
			a = up[a][i];
			b = up[b][i];
		}
	}
	return up[a][0];
}
long long t[N + 100];

int pref(int i){
	int sum = 0;
	for(; i > 0; i-= i & -i){
		sum+= t[i];
	}
	return sum;
}

void add(int i, int x){
	int pos = i;
	for(; i <= n; i+= i & -i){
		t[i]+= x;
	}
}
int sum(int l, int r){
	return pref(r) - pref(l-1);
}
long long query(int a, int b){
	long long ans = 0;
	while(chain[b] != chain[a]){
		if(depth[chain[a]] > depth[chain[b]]) swap(a, b);
		ans+= sum(pos[chain[b]], pos[b]);
		b = up[chain[b]][0];
	}
	if(depth[a] > depth[b]) swap(a, b);
	ans+= sum(pos[a], pos[b]);
	return ans;
}
long long dp[N+10][2];
vector<vector<array<int, 3> > > transit(N+10);
int last[N+10];
void calc(int v, int par){
	long long sum1 = 0;
	for(int to : g[v]){
		if(to == par) continue;
		calc(to, v);
		sum1+= max(dp[to][0], dp[to][1]);
	}
	dp[v][0] = max(dp[v][0], sum1);
	dp[v][1] = max(dp[v][1], sum1);
	for(auto &[a, b, c] : transit[v]){
		long long sumA = query(a, v) + query(b, v) + dp[v][1] + c;
		long long sumB = dp[v][1]; 
		sumA-= dp[v][0];
		sumA = sumA + dp[v][0] - dp[v][1] + dp[v][0] - dp[v][1];
		if(dp[v][1] < sumA + sumB){
			dp[v][1] = max(dp[v][1], sumA + sumB);
		}
	}	
	add(pos[v], (dp[v][0] - dp[v][1]) - last[pos[v]]);
	last[pos[v]] = dp[v][0] - dp[v][1];
}


signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
	for(int i = 1;i < n; i++){
		int a, b; cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	int m; cin >> m;
	dfs(1, 1);
	for(auto i = 0;i < m; i++){
		int a, b, c;
		cin >> a >> b >> c;
		int lc = lca(a, b);
		transit[lc].push_back({a, b, c});
	}
	timer = 1;
	decompose(1, 1, 1);
	calc(1, 1);
	cout << dp[1][1] << '\n';
	
	
	return 0;
}

Compilation message

election_campaign.cpp: In function 'void add(int, int)':
election_campaign.cpp:79:6: warning: unused variable 'pos' [-Wunused-variable]
   79 |  int pos = i;
      |      ^~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13404 KB Output is correct
2 Correct 2 ms 13404 KB Output is correct
3 Correct 3 ms 13404 KB Output is correct
4 Correct 3 ms 13404 KB Output is correct
5 Correct 45 ms 20896 KB Output is correct
6 Correct 37 ms 30036 KB Output is correct
7 Correct 53 ms 26824 KB Output is correct
8 Correct 40 ms 21156 KB Output is correct
9 Correct 81 ms 24916 KB Output is correct
10 Correct 42 ms 21340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13404 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 3 ms 13404 KB Output is correct
4 Correct 77 ms 32052 KB Output is correct
5 Correct 83 ms 32152 KB Output is correct
6 Correct 78 ms 31964 KB Output is correct
7 Correct 75 ms 32180 KB Output is correct
8 Correct 75 ms 32184 KB Output is correct
9 Correct 71 ms 32168 KB Output is correct
10 Correct 82 ms 31944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13404 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 3 ms 13404 KB Output is correct
4 Correct 77 ms 32052 KB Output is correct
5 Correct 83 ms 32152 KB Output is correct
6 Correct 78 ms 31964 KB Output is correct
7 Correct 75 ms 32180 KB Output is correct
8 Correct 75 ms 32184 KB Output is correct
9 Correct 71 ms 32168 KB Output is correct
10 Correct 82 ms 31944 KB Output is correct
11 Correct 9 ms 13912 KB Output is correct
12 Correct 80 ms 32060 KB Output is correct
13 Correct 79 ms 32080 KB Output is correct
14 Correct 91 ms 32212 KB Output is correct
15 Correct 82 ms 32336 KB Output is correct
16 Correct 71 ms 32060 KB Output is correct
17 Correct 79 ms 32084 KB Output is correct
18 Correct 76 ms 32048 KB Output is correct
19 Correct 70 ms 32044 KB Output is correct
20 Correct 95 ms 32076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 114 ms 22276 KB Output is correct
2 Correct 74 ms 32028 KB Output is correct
3 Correct 103 ms 28496 KB Output is correct
4 Correct 71 ms 22800 KB Output is correct
5 Correct 111 ms 27872 KB Output is correct
6 Correct 86 ms 22824 KB Output is correct
7 Correct 138 ms 27536 KB Output is correct
8 Correct 99 ms 22608 KB Output is correct
9 Correct 81 ms 32244 KB Output is correct
10 Correct 113 ms 26380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13404 KB Output is correct
2 Correct 2 ms 13404 KB Output is correct
3 Correct 3 ms 13404 KB Output is correct
4 Correct 3 ms 13404 KB Output is correct
5 Correct 45 ms 20896 KB Output is correct
6 Correct 37 ms 30036 KB Output is correct
7 Correct 53 ms 26824 KB Output is correct
8 Correct 40 ms 21156 KB Output is correct
9 Correct 81 ms 24916 KB Output is correct
10 Correct 42 ms 21340 KB Output is correct
11 Correct 3 ms 13400 KB Output is correct
12 Correct 4 ms 13404 KB Output is correct
13 Correct 3 ms 13404 KB Output is correct
14 Correct 4 ms 13404 KB Output is correct
15 Correct 3 ms 13404 KB Output is correct
16 Correct 4 ms 13404 KB Output is correct
17 Correct 3 ms 13404 KB Output is correct
18 Correct 3 ms 13404 KB Output is correct
19 Correct 3 ms 13404 KB Output is correct
20 Correct 4 ms 13328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13404 KB Output is correct
2 Correct 2 ms 13404 KB Output is correct
3 Correct 3 ms 13404 KB Output is correct
4 Correct 3 ms 13404 KB Output is correct
5 Correct 45 ms 20896 KB Output is correct
6 Correct 37 ms 30036 KB Output is correct
7 Correct 53 ms 26824 KB Output is correct
8 Correct 40 ms 21156 KB Output is correct
9 Correct 81 ms 24916 KB Output is correct
10 Correct 42 ms 21340 KB Output is correct
11 Correct 3 ms 13404 KB Output is correct
12 Correct 3 ms 13404 KB Output is correct
13 Correct 3 ms 13404 KB Output is correct
14 Correct 77 ms 32052 KB Output is correct
15 Correct 83 ms 32152 KB Output is correct
16 Correct 78 ms 31964 KB Output is correct
17 Correct 75 ms 32180 KB Output is correct
18 Correct 75 ms 32184 KB Output is correct
19 Correct 71 ms 32168 KB Output is correct
20 Correct 82 ms 31944 KB Output is correct
21 Correct 9 ms 13912 KB Output is correct
22 Correct 80 ms 32060 KB Output is correct
23 Correct 79 ms 32080 KB Output is correct
24 Correct 91 ms 32212 KB Output is correct
25 Correct 82 ms 32336 KB Output is correct
26 Correct 71 ms 32060 KB Output is correct
27 Correct 79 ms 32084 KB Output is correct
28 Correct 76 ms 32048 KB Output is correct
29 Correct 70 ms 32044 KB Output is correct
30 Correct 95 ms 32076 KB Output is correct
31 Correct 114 ms 22276 KB Output is correct
32 Correct 74 ms 32028 KB Output is correct
33 Correct 103 ms 28496 KB Output is correct
34 Correct 71 ms 22800 KB Output is correct
35 Correct 111 ms 27872 KB Output is correct
36 Correct 86 ms 22824 KB Output is correct
37 Correct 138 ms 27536 KB Output is correct
38 Correct 99 ms 22608 KB Output is correct
39 Correct 81 ms 32244 KB Output is correct
40 Correct 113 ms 26380 KB Output is correct
41 Correct 3 ms 13400 KB Output is correct
42 Correct 4 ms 13404 KB Output is correct
43 Correct 3 ms 13404 KB Output is correct
44 Correct 4 ms 13404 KB Output is correct
45 Correct 3 ms 13404 KB Output is correct
46 Correct 4 ms 13404 KB Output is correct
47 Correct 3 ms 13404 KB Output is correct
48 Correct 3 ms 13404 KB Output is correct
49 Correct 3 ms 13404 KB Output is correct
50 Correct 4 ms 13328 KB Output is correct
51 Correct 125 ms 22604 KB Output is correct
52 Correct 77 ms 32084 KB Output is correct
53 Correct 123 ms 26576 KB Output is correct
54 Correct 74 ms 22672 KB Output is correct
55 Correct 139 ms 22944 KB Output is correct
56 Correct 83 ms 32180 KB Output is correct
57 Correct 142 ms 27216 KB Output is correct
58 Correct 76 ms 22796 KB Output is correct
59 Correct 119 ms 22552 KB Output is correct
60 Correct 78 ms 32080 KB Output is correct
61 Correct 99 ms 27472 KB Output is correct
62 Correct 73 ms 22704 KB Output is correct
63 Correct 125 ms 22248 KB Output is correct
64 Correct 76 ms 32020 KB Output is correct
65 Correct 138 ms 27144 KB Output is correct
66 Correct 69 ms 22668 KB Output is correct
67 Correct 134 ms 22472 KB Output is correct
68 Correct 84 ms 32148 KB Output is correct
69 Correct 94 ms 25936 KB Output is correct
70 Correct 88 ms 22760 KB Output is correct