Submission #890614

# Submission time Handle Problem Language Result Execution time Memory
890614 2023-12-21T15:59:02 Z iskhakkutbilim Election Campaign (JOI15_election_campaign) C++17
30 / 100
1000 ms 34384 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 pos[N+10];
void dfs(int v, int par){
	up[v][0] = par;
	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[to] || sub[bigchild[v]] < sub[to]){
			bigchild[v] = to;
		}
	}
}


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 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]){
		ans+= sum(pos[chain[b]], pos[b]);
		b = up[chain[b]][0];
	}
	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(v, a) + query(v, b) + 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:72:6: warning: unused variable 'pos' [-Wunused-variable]
   72 |  int pos = i;
      |      ^~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 10332 KB Output is correct
2 Correct 2 ms 10332 KB Output is correct
3 Correct 3 ms 10576 KB Output is correct
4 Correct 3 ms 10588 KB Output is correct
5 Correct 55 ms 21188 KB Output is correct
6 Correct 38 ms 30544 KB Output is correct
7 Correct 62 ms 27140 KB Output is correct
8 Correct 40 ms 21588 KB Output is correct
9 Correct 68 ms 25428 KB Output is correct
10 Correct 42 ms 21508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10332 KB Output is correct
2 Correct 3 ms 10332 KB Output is correct
3 Correct 4 ms 10588 KB Output is correct
4 Correct 75 ms 33836 KB Output is correct
5 Correct 88 ms 33852 KB Output is correct
6 Correct 69 ms 33888 KB Output is correct
7 Correct 76 ms 33872 KB Output is correct
8 Correct 75 ms 33876 KB Output is correct
9 Correct 70 ms 33872 KB Output is correct
10 Correct 76 ms 33744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10332 KB Output is correct
2 Correct 3 ms 10332 KB Output is correct
3 Correct 4 ms 10588 KB Output is correct
4 Correct 75 ms 33836 KB Output is correct
5 Correct 88 ms 33852 KB Output is correct
6 Correct 69 ms 33888 KB Output is correct
7 Correct 76 ms 33872 KB Output is correct
8 Correct 75 ms 33876 KB Output is correct
9 Correct 70 ms 33872 KB Output is correct
10 Correct 76 ms 33744 KB Output is correct
11 Correct 10 ms 11352 KB Output is correct
12 Correct 83 ms 34152 KB Output is correct
13 Correct 79 ms 33928 KB Output is correct
14 Correct 72 ms 34004 KB Output is correct
15 Correct 77 ms 33948 KB Output is correct
16 Correct 80 ms 34132 KB Output is correct
17 Correct 92 ms 34384 KB Output is correct
18 Correct 79 ms 34128 KB Output is correct
19 Correct 73 ms 34132 KB Output is correct
20 Correct 76 ms 34128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 117 ms 24072 KB Output is correct
2 Correct 71 ms 33876 KB Output is correct
3 Execution timed out 1055 ms 30296 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 10332 KB Output is correct
2 Correct 2 ms 10332 KB Output is correct
3 Correct 3 ms 10576 KB Output is correct
4 Correct 3 ms 10588 KB Output is correct
5 Correct 55 ms 21188 KB Output is correct
6 Correct 38 ms 30544 KB Output is correct
7 Correct 62 ms 27140 KB Output is correct
8 Correct 40 ms 21588 KB Output is correct
9 Correct 68 ms 25428 KB Output is correct
10 Correct 42 ms 21508 KB Output is correct
11 Correct 3 ms 10588 KB Output is correct
12 Correct 5 ms 10584 KB Output is correct
13 Correct 4 ms 10584 KB Output is correct
14 Correct 3 ms 10588 KB Output is correct
15 Correct 3 ms 10588 KB Output is correct
16 Correct 4 ms 10652 KB Output is correct
17 Correct 3 ms 10584 KB Output is correct
18 Correct 4 ms 10588 KB Output is correct
19 Correct 3 ms 10588 KB Output is correct
20 Correct 3 ms 10588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 10332 KB Output is correct
2 Correct 2 ms 10332 KB Output is correct
3 Correct 3 ms 10576 KB Output is correct
4 Correct 3 ms 10588 KB Output is correct
5 Correct 55 ms 21188 KB Output is correct
6 Correct 38 ms 30544 KB Output is correct
7 Correct 62 ms 27140 KB Output is correct
8 Correct 40 ms 21588 KB Output is correct
9 Correct 68 ms 25428 KB Output is correct
10 Correct 42 ms 21508 KB Output is correct
11 Correct 3 ms 10332 KB Output is correct
12 Correct 3 ms 10332 KB Output is correct
13 Correct 4 ms 10588 KB Output is correct
14 Correct 75 ms 33836 KB Output is correct
15 Correct 88 ms 33852 KB Output is correct
16 Correct 69 ms 33888 KB Output is correct
17 Correct 76 ms 33872 KB Output is correct
18 Correct 75 ms 33876 KB Output is correct
19 Correct 70 ms 33872 KB Output is correct
20 Correct 76 ms 33744 KB Output is correct
21 Correct 10 ms 11352 KB Output is correct
22 Correct 83 ms 34152 KB Output is correct
23 Correct 79 ms 33928 KB Output is correct
24 Correct 72 ms 34004 KB Output is correct
25 Correct 77 ms 33948 KB Output is correct
26 Correct 80 ms 34132 KB Output is correct
27 Correct 92 ms 34384 KB Output is correct
28 Correct 79 ms 34128 KB Output is correct
29 Correct 73 ms 34132 KB Output is correct
30 Correct 76 ms 34128 KB Output is correct
31 Correct 117 ms 24072 KB Output is correct
32 Correct 71 ms 33876 KB Output is correct
33 Execution timed out 1055 ms 30296 KB Time limit exceeded
34 Halted 0 ms 0 KB -