Submission #114024

# Submission time Handle Problem Language Result Execution time Memory
114024 2019-05-29T15:35:31 Z Diuven Election Campaign (JOI15_election_campaign) C++14
100 / 100
412 ms 39036 KB
#include <bits/stdc++.h>
using namespace std;

const int MAX = 1e5+10;
const int LG = 18;

int n, de[MAX], pa[LG][MAX], su[MAX], dn[MAX];
vector<int> G[MAX];

void dfs1(int v, int p, int d=1){
	de[v] = d, su[v] = 1;
	for(int x:G[v]){
		if(x==p) continue;
		pa[0][x] = v;
		for(int i=1; i<LG; i++) pa[i][x] = pa[i-1][pa[i-1][x]];
		dfs1(x,v,d+1); su[v]+=su[x];
		if(su[dn[v]]<su[x]) dn[v]=x;
	}
}

int lca(int u, int v){
	if(de[u]<de[v]) swap(u,v);
	for(int i=LG-1; i>=0; i--) if(de[u]-(1<<i)>=de[v]) u = pa[i][u];
	if(u==v) return u;
	for(int i=LG-1; i>=0; i--) if(pa[i][u]!=pa[i][v]) u = pa[i][u], v = pa[i][v];
	return pa[0][u];
}

class Seg_t{
	int n; vector<int> T, A;
	void _upt(int p, int x){
		for(; p<=n; p+=p&(-p)) T[p]+=x;
	}
	int _sum(int p){
		int res=0;
		for(; p>0; p-=p&(-p)) res += T[p];
		return res;
	}
  public:
	Seg_t(int sz): n(sz), T(n+1), A(n) {}
    Seg_t(): Seg_t(0) {}
	void upt(int p, int x){ _upt(p+1,x-A[p]), A[p]=x; }
	int sum(int l, int r){ return _sum(r+1) - _sum(l); }
	int val(int p){ return sum(p,p); }
};

class HLD_t{
	Seg_t Seg[MAX]; int rt[MAX], sz[MAX];
	void dfs0(int v, int p, int c){
		rt[v] = c; sz[c]++;
		for(int x:G[v]) if(x!=p)
			dfs0(x, v, (x==dn[v] ? c : x));
	}
	int sum_u(int x, int v){
		int res = 0;
		for(int t, r; de[x]>=de[v]; x=pa[0][t]){
			r = rt[x], t = (de[r]<de[v] ? v : r);
			res += Seg[r].sum(de[t]-de[r], de[x]-de[r]);
		}
		return res;
	}
	int val(int v){ return Seg[rt[v]].val(de[v]-de[rt[v]]); }
  public:
    void init(){
		dfs0(1,0,1);
		for(int i=1; i<=n; i++) Seg[i] = Seg_t(sz[i]);
	}
	void upt(int v, int x){
		Seg[rt[v]].upt(de[v]-de[rt[v]], x);
	}
	int sum_p(int x, int y, int p){ return sum_u(x,p) + sum_u(y,p) - val(p); }
} HLD;

vector<int> plan[MAX];
int m, X[MAX], Y[MAX], C[MAX];

int D[MAX], S[MAX];

void dfs2(int v, int p){
	for(int x:G[v]){
		if(x==p) continue;
		dfs2(x,v); S[v]+=D[x];
	}
	D[v] = S[v]; HLD.upt(v, S[v]);

	for(int idx:plan[v]){
		int x=X[idx], y=Y[idx], c=C[idx];
		int now = c + HLD.sum_p(x,y,v);
		D[v] = max(now, D[v]);
	}
	HLD.upt(v, S[v]-D[v]);
}

int main(){
	ios::sync_with_stdio(0); cin.tie(0);

	cin>>n;
	for(int i=1; i<n; i++){
		int u,v; cin>>u>>v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	dfs1(1,0);

	HLD.init();

	cin>>m;
	for(int i=1; i<=m; i++){
		cin>>X[i]>>Y[i]>>C[i];
		plan[lca(X[i], Y[i])].push_back(i);
	}

	dfs2(1,0);

	cout<<D[1]<<'\n';

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 13824 KB Output is correct
2 Correct 16 ms 13824 KB Output is correct
3 Correct 16 ms 13824 KB Output is correct
4 Correct 16 ms 13952 KB Output is correct
5 Correct 174 ms 28664 KB Output is correct
6 Correct 79 ms 36600 KB Output is correct
7 Correct 182 ms 34228 KB Output is correct
8 Correct 188 ms 29796 KB Output is correct
9 Correct 199 ms 32376 KB Output is correct
10 Correct 171 ms 29816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 13824 KB Output is correct
2 Correct 16 ms 13824 KB Output is correct
3 Correct 23 ms 13944 KB Output is correct
4 Correct 194 ms 38932 KB Output is correct
5 Correct 184 ms 39036 KB Output is correct
6 Correct 148 ms 38908 KB Output is correct
7 Correct 184 ms 38904 KB Output is correct
8 Correct 196 ms 38904 KB Output is correct
9 Correct 166 ms 38904 KB Output is correct
10 Correct 161 ms 38916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 13824 KB Output is correct
2 Correct 16 ms 13824 KB Output is correct
3 Correct 23 ms 13944 KB Output is correct
4 Correct 194 ms 38932 KB Output is correct
5 Correct 184 ms 39036 KB Output is correct
6 Correct 148 ms 38908 KB Output is correct
7 Correct 184 ms 38904 KB Output is correct
8 Correct 196 ms 38904 KB Output is correct
9 Correct 166 ms 38904 KB Output is correct
10 Correct 161 ms 38916 KB Output is correct
11 Correct 27 ms 14720 KB Output is correct
12 Correct 164 ms 38836 KB Output is correct
13 Correct 164 ms 38864 KB Output is correct
14 Correct 149 ms 38904 KB Output is correct
15 Correct 176 ms 38904 KB Output is correct
16 Correct 156 ms 38964 KB Output is correct
17 Correct 201 ms 38904 KB Output is correct
18 Correct 239 ms 38892 KB Output is correct
19 Correct 169 ms 38904 KB Output is correct
20 Correct 203 ms 38904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 319 ms 30396 KB Output is correct
2 Correct 161 ms 38904 KB Output is correct
3 Correct 412 ms 36216 KB Output is correct
4 Correct 267 ms 31344 KB Output is correct
5 Correct 320 ms 35704 KB Output is correct
6 Correct 248 ms 31508 KB Output is correct
7 Correct 384 ms 35320 KB Output is correct
8 Correct 311 ms 30712 KB Output is correct
9 Correct 161 ms 38904 KB Output is correct
10 Correct 411 ms 34192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 13824 KB Output is correct
2 Correct 16 ms 13824 KB Output is correct
3 Correct 16 ms 13824 KB Output is correct
4 Correct 16 ms 13952 KB Output is correct
5 Correct 174 ms 28664 KB Output is correct
6 Correct 79 ms 36600 KB Output is correct
7 Correct 182 ms 34228 KB Output is correct
8 Correct 188 ms 29796 KB Output is correct
9 Correct 199 ms 32376 KB Output is correct
10 Correct 171 ms 29816 KB Output is correct
11 Correct 17 ms 13952 KB Output is correct
12 Correct 18 ms 14080 KB Output is correct
13 Correct 18 ms 14080 KB Output is correct
14 Correct 18 ms 13952 KB Output is correct
15 Correct 18 ms 13952 KB Output is correct
16 Correct 19 ms 14080 KB Output is correct
17 Correct 18 ms 13996 KB Output is correct
18 Correct 18 ms 14052 KB Output is correct
19 Correct 18 ms 13952 KB Output is correct
20 Correct 18 ms 14072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 13824 KB Output is correct
2 Correct 16 ms 13824 KB Output is correct
3 Correct 16 ms 13824 KB Output is correct
4 Correct 16 ms 13952 KB Output is correct
5 Correct 174 ms 28664 KB Output is correct
6 Correct 79 ms 36600 KB Output is correct
7 Correct 182 ms 34228 KB Output is correct
8 Correct 188 ms 29796 KB Output is correct
9 Correct 199 ms 32376 KB Output is correct
10 Correct 171 ms 29816 KB Output is correct
11 Correct 16 ms 13824 KB Output is correct
12 Correct 16 ms 13824 KB Output is correct
13 Correct 23 ms 13944 KB Output is correct
14 Correct 194 ms 38932 KB Output is correct
15 Correct 184 ms 39036 KB Output is correct
16 Correct 148 ms 38908 KB Output is correct
17 Correct 184 ms 38904 KB Output is correct
18 Correct 196 ms 38904 KB Output is correct
19 Correct 166 ms 38904 KB Output is correct
20 Correct 161 ms 38916 KB Output is correct
21 Correct 27 ms 14720 KB Output is correct
22 Correct 164 ms 38836 KB Output is correct
23 Correct 164 ms 38864 KB Output is correct
24 Correct 149 ms 38904 KB Output is correct
25 Correct 176 ms 38904 KB Output is correct
26 Correct 156 ms 38964 KB Output is correct
27 Correct 201 ms 38904 KB Output is correct
28 Correct 239 ms 38892 KB Output is correct
29 Correct 169 ms 38904 KB Output is correct
30 Correct 203 ms 38904 KB Output is correct
31 Correct 319 ms 30396 KB Output is correct
32 Correct 161 ms 38904 KB Output is correct
33 Correct 412 ms 36216 KB Output is correct
34 Correct 267 ms 31344 KB Output is correct
35 Correct 320 ms 35704 KB Output is correct
36 Correct 248 ms 31508 KB Output is correct
37 Correct 384 ms 35320 KB Output is correct
38 Correct 311 ms 30712 KB Output is correct
39 Correct 161 ms 38904 KB Output is correct
40 Correct 411 ms 34192 KB Output is correct
41 Correct 17 ms 13952 KB Output is correct
42 Correct 18 ms 14080 KB Output is correct
43 Correct 18 ms 14080 KB Output is correct
44 Correct 18 ms 13952 KB Output is correct
45 Correct 18 ms 13952 KB Output is correct
46 Correct 19 ms 14080 KB Output is correct
47 Correct 18 ms 13996 KB Output is correct
48 Correct 18 ms 14052 KB Output is correct
49 Correct 18 ms 13952 KB Output is correct
50 Correct 18 ms 14072 KB Output is correct
51 Correct 291 ms 30840 KB Output is correct
52 Correct 218 ms 38940 KB Output is correct
53 Correct 382 ms 34488 KB Output is correct
54 Correct 247 ms 31480 KB Output is correct
55 Correct 270 ms 30196 KB Output is correct
56 Correct 179 ms 39004 KB Output is correct
57 Correct 284 ms 35064 KB Output is correct
58 Correct 251 ms 31608 KB Output is correct
59 Correct 263 ms 30768 KB Output is correct
60 Correct 168 ms 38904 KB Output is correct
61 Correct 295 ms 35192 KB Output is correct
62 Correct 231 ms 31612 KB Output is correct
63 Correct 288 ms 30292 KB Output is correct
64 Correct 170 ms 38904 KB Output is correct
65 Correct 376 ms 35060 KB Output is correct
66 Correct 253 ms 31620 KB Output is correct
67 Correct 272 ms 30196 KB Output is correct
68 Correct 166 ms 38836 KB Output is correct
69 Correct 289 ms 33528 KB Output is correct
70 Correct 230 ms 31352 KB Output is correct