답안 #518882

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
518882 2022-01-25T02:52:59 Z amunduzbaev Election Campaign (JOI15_election_campaign) C++14
100 / 100
232 ms 36716 KB
#include "bits/stdc++.h"
using namespace std;

#define ar array

const int N = 1e5 + 5;
vector<int> edges[N], qq[N];
int par[N][20], tin[N], tout[N], t;
int dp[N], p[N][3];

struct BIT{
	int tree[N];
	
	void add(int i, int v){
		for(;i<N;i+=(i&-i)) tree[i] += v;
	}
	
	int get(int i){
		int c=0;
		for(;i>0;i-=(i&-i)) c += tree[i];
		return c;
	}
}tree;

void dfs(int u, int p = -1){
	tin[u] = ++t;
	for(int i=1;i<20;i++) par[u][i] = par[par[u][i-1]][i-1];
	for(auto x : edges[u]){
		if(x == p) continue;
		par[x][0] = u;
		dfs(x, u);
	} tout[u] = t;
}
bool is(int a, int b) { return (tin[a] <= tin[b] && tout[b] <= tout[a]); }
int lca(int a, int b){
	if(is(a, b)) return a;
	if(is(b, a)) return b;
	for(int i=19;~i;i--){
		if(!is(par[a][i], b)) a = par[a][i];
	} return par[a][0];
}

int first(int a, int b){
	assert(is(a, b));
	for(int i=19;~i;i--){
		if(!is(par[b][i], a)) b = par[b][i];
	} 
	assert(par[b][0] == a);
	return b;
}

void dpdfs(int u, int pp = -1){
	int tot = 0;
	for(auto x : edges[u]){
		if(x == pp) continue;
		dpdfs(x, u);
		tot += dp[x];
	} dp[u] = tot;
	
	for(auto i : qq[u]){
		if(p[i][0] == u){
			int f = first(u, p[i][1]);
			dp[u] = max(dp[u], tree.get(tin[p[i][1]]) + tot - dp[f] + p[i][2]);
		} else if(p[i][1] == u){
			int f = first(u, p[i][0]);
			dp[u] = max(dp[u], tree.get(tin[p[i][0]]) + tot - dp[f] + p[i][2]);
		} else {
			int a = first(u, p[i][0]), b = first(u, p[i][1]);
			dp[u] = max(dp[u], tree.get(tin[p[i][0]]) + tree.get(tin[p[i][1]]) + tot - dp[a] - dp[b] + p[i][2]);
		}
	}
	
	for(auto x : edges[u]){
		if(x == pp) continue;
		tree.add(tin[x], tot - dp[x]);
		tree.add(tout[x] + 1, dp[x] - tot);
	} tree.add(tin[u], tot); tree.add(tin[u] + 1, -tot);
}

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	int n; cin>>n;
	for(int i=1;i<n;i++){
		int a, b; cin>>a>>b;
		edges[a].push_back(b);
		edges[b].push_back(a);
	}
	par[1][0] = 1;
	dfs(1);
	
	int m; cin>>m;
	for(int i=0;i<m;i++){
		cin>>p[i][0]>>p[i][1]>>p[i][2];
		//~ cout<<lca(p[i][0], p[i][1])<<"\n";
		qq[lca(p[i][0], p[i][1])].push_back(i);
	}
	
	dpdfs(1);
	cout<<dp[1]<<"\n";
	//~ for(int i=1;i<=n;i++) cout<<dp[i]<<"\n";
}

# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5016 KB Output is correct
4 Correct 3 ms 5196 KB Output is correct
5 Correct 97 ms 18836 KB Output is correct
6 Correct 51 ms 32760 KB Output is correct
7 Correct 103 ms 27820 KB Output is correct
8 Correct 66 ms 19160 KB Output is correct
9 Correct 95 ms 25028 KB Output is correct
10 Correct 62 ms 19148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5016 KB Output is correct
2 Correct 4 ms 5020 KB Output is correct
3 Correct 3 ms 5324 KB Output is correct
4 Correct 111 ms 36292 KB Output is correct
5 Correct 118 ms 36300 KB Output is correct
6 Correct 105 ms 36360 KB Output is correct
7 Correct 121 ms 36260 KB Output is correct
8 Correct 124 ms 36296 KB Output is correct
9 Correct 107 ms 36344 KB Output is correct
10 Correct 127 ms 36264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5016 KB Output is correct
2 Correct 4 ms 5020 KB Output is correct
3 Correct 3 ms 5324 KB Output is correct
4 Correct 111 ms 36292 KB Output is correct
5 Correct 118 ms 36300 KB Output is correct
6 Correct 105 ms 36360 KB Output is correct
7 Correct 121 ms 36260 KB Output is correct
8 Correct 124 ms 36296 KB Output is correct
9 Correct 107 ms 36344 KB Output is correct
10 Correct 127 ms 36264 KB Output is correct
11 Correct 14 ms 6040 KB Output is correct
12 Correct 122 ms 36512 KB Output is correct
13 Correct 114 ms 36556 KB Output is correct
14 Correct 126 ms 36576 KB Output is correct
15 Correct 135 ms 36508 KB Output is correct
16 Correct 116 ms 36632 KB Output is correct
17 Correct 145 ms 36596 KB Output is correct
18 Correct 142 ms 36648 KB Output is correct
19 Correct 120 ms 36628 KB Output is correct
20 Correct 126 ms 36716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 193 ms 21828 KB Output is correct
2 Correct 108 ms 36336 KB Output is correct
3 Correct 225 ms 30868 KB Output is correct
4 Correct 149 ms 22096 KB Output is correct
5 Correct 195 ms 30108 KB Output is correct
6 Correct 165 ms 22296 KB Output is correct
7 Correct 223 ms 29600 KB Output is correct
8 Correct 165 ms 22288 KB Output is correct
9 Correct 102 ms 36348 KB Output is correct
10 Correct 215 ms 28060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5016 KB Output is correct
4 Correct 3 ms 5196 KB Output is correct
5 Correct 97 ms 18836 KB Output is correct
6 Correct 51 ms 32760 KB Output is correct
7 Correct 103 ms 27820 KB Output is correct
8 Correct 66 ms 19160 KB Output is correct
9 Correct 95 ms 25028 KB Output is correct
10 Correct 62 ms 19148 KB Output is correct
11 Correct 4 ms 5156 KB Output is correct
12 Correct 4 ms 5324 KB Output is correct
13 Correct 4 ms 5196 KB Output is correct
14 Correct 4 ms 5196 KB Output is correct
15 Correct 4 ms 5196 KB Output is correct
16 Correct 4 ms 5188 KB Output is correct
17 Correct 4 ms 5196 KB Output is correct
18 Correct 4 ms 5196 KB Output is correct
19 Correct 4 ms 5156 KB Output is correct
20 Correct 5 ms 5236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5016 KB Output is correct
4 Correct 3 ms 5196 KB Output is correct
5 Correct 97 ms 18836 KB Output is correct
6 Correct 51 ms 32760 KB Output is correct
7 Correct 103 ms 27820 KB Output is correct
8 Correct 66 ms 19160 KB Output is correct
9 Correct 95 ms 25028 KB Output is correct
10 Correct 62 ms 19148 KB Output is correct
11 Correct 2 ms 5016 KB Output is correct
12 Correct 4 ms 5020 KB Output is correct
13 Correct 3 ms 5324 KB Output is correct
14 Correct 111 ms 36292 KB Output is correct
15 Correct 118 ms 36300 KB Output is correct
16 Correct 105 ms 36360 KB Output is correct
17 Correct 121 ms 36260 KB Output is correct
18 Correct 124 ms 36296 KB Output is correct
19 Correct 107 ms 36344 KB Output is correct
20 Correct 127 ms 36264 KB Output is correct
21 Correct 14 ms 6040 KB Output is correct
22 Correct 122 ms 36512 KB Output is correct
23 Correct 114 ms 36556 KB Output is correct
24 Correct 126 ms 36576 KB Output is correct
25 Correct 135 ms 36508 KB Output is correct
26 Correct 116 ms 36632 KB Output is correct
27 Correct 145 ms 36596 KB Output is correct
28 Correct 142 ms 36648 KB Output is correct
29 Correct 120 ms 36628 KB Output is correct
30 Correct 126 ms 36716 KB Output is correct
31 Correct 193 ms 21828 KB Output is correct
32 Correct 108 ms 36336 KB Output is correct
33 Correct 225 ms 30868 KB Output is correct
34 Correct 149 ms 22096 KB Output is correct
35 Correct 195 ms 30108 KB Output is correct
36 Correct 165 ms 22296 KB Output is correct
37 Correct 223 ms 29600 KB Output is correct
38 Correct 165 ms 22288 KB Output is correct
39 Correct 102 ms 36348 KB Output is correct
40 Correct 215 ms 28060 KB Output is correct
41 Correct 4 ms 5156 KB Output is correct
42 Correct 4 ms 5324 KB Output is correct
43 Correct 4 ms 5196 KB Output is correct
44 Correct 4 ms 5196 KB Output is correct
45 Correct 4 ms 5196 KB Output is correct
46 Correct 4 ms 5188 KB Output is correct
47 Correct 4 ms 5196 KB Output is correct
48 Correct 4 ms 5196 KB Output is correct
49 Correct 4 ms 5156 KB Output is correct
50 Correct 5 ms 5236 KB Output is correct
51 Correct 173 ms 22564 KB Output is correct
52 Correct 123 ms 36540 KB Output is correct
53 Correct 232 ms 28488 KB Output is correct
54 Correct 121 ms 22428 KB Output is correct
55 Correct 174 ms 22112 KB Output is correct
56 Correct 112 ms 36508 KB Output is correct
57 Correct 176 ms 29544 KB Output is correct
58 Correct 147 ms 22456 KB Output is correct
59 Correct 167 ms 22640 KB Output is correct
60 Correct 115 ms 36596 KB Output is correct
61 Correct 181 ms 29724 KB Output is correct
62 Correct 156 ms 22520 KB Output is correct
63 Correct 175 ms 22184 KB Output is correct
64 Correct 129 ms 36508 KB Output is correct
65 Correct 222 ms 29520 KB Output is correct
66 Correct 132 ms 22448 KB Output is correct
67 Correct 182 ms 22308 KB Output is correct
68 Correct 117 ms 36508 KB Output is correct
69 Correct 194 ms 27540 KB Output is correct
70 Correct 150 ms 22700 KB Output is correct