답안 #363799

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
363799 2021-02-07T09:49:50 Z Bill_00 Election Campaign (JOI15_election_campaign) C++14
100 / 100
508 ms 47112 KB
#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define N 100001
typedef long long ll;
const long long MOD=1000000007;
using namespace std;
struct plan{
	ll x,y,votes;
};

ll timer,m,n,l,k,sz[N],h[N],seg[N];
ll tin[N],tout[N],up[N][20];
ll in[N],out[N],tree[4*N+4];

vector<plan>path[N];
vector<ll>adj[N];

void dfs(ll node,ll par=1){
	tin[node]=++timer;
	up[node][0]=par;
	for(ll i=1;i<=l;i++){
		up[node][i]=up[up[node][i-1]][i-1];
	}
	sz[node]=1;
	for(ll x:adj[node]){
		if(x!=par){
			dfs(x,node);
			sz[node]+=sz[x];
		} 
	}
	tout[node]=++timer;
}

void dfs1(ll node,ll par,ll head){
	seg[node]=++k;
	h[node]=head;
	pair<ll,ll>bigchild={0,-1};
	for(ll x:adj[node]){
		if(x!=par) bigchild=max(bigchild,{sz[x],x});
	}
	if(bigchild.ss!=-1) dfs1(bigchild.ss,node,head);
	for(ll x:adj[node]){
		if(x!=par && x!=bigchild.ss) dfs1(x,node,x);
	}
}

bool is(ll a,ll b){
	return (tin[a]<=tin[b] && tout[b]<=tout[a]);
}

ll lca(ll a,ll b){
	if(is(a,b)) return a;
	if(is(b,a)) return b;
	for(ll i=l;i>=0;i--){
		if(!is(up[a][i],b)) a=up[a][i];
	}
	return up[a][0];
}

ll query(ll id,ll l,ll r,ll L,ll R){
	if(r<L || R<l) return 0;
	if(L<=l && r<=R) return tree[id];
	ll m=(l+r)>>1;
	return query(id*2,l,m,L,R)+query(id*2+1,m+1,r,L,R);
}

void update(ll id,ll l,ll r,ll x,ll val){
	if(l==r){
		tree[id]=val;
		return;
	}
	ll m=(l+r)>>1;
	if(m>=x) update(id*2,l,m,x,val);
	else update(id*2+1,m+1,r,x,val);
	tree[id]=tree[id*2]+tree[id*2+1];
}

ll p(ll a,ll b){
	if(is(h[a],b)) return query(1,1,k,seg[b],seg[a]);
	return query(1,1,k,seg[h[a]],seg[a])+p(up[h[a]][0],b);
}

void solve(ll node,ll par=1){
	for(ll x:adj[node]){
		if(x!=par){
			solve(x,node);
			out[node]+=in[x];
		} 
	}
	in[node]=out[node];
	for(plan xx:path[node]){
		ll a=xx.x;
		ll b=xx.y;
		ll c=xx.votes;
		in[node]=max(in[node],p(a,node)+p(b,node)+c+out[node]);
	}
	update(1,1,k,seg[node],out[node]-in[node]);
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	l=ceil(log2(n));
	for(int i=1;i<n;i++){
		int a,b;
		cin >> a >> b;
		adj[a].pb(b);
		adj[b].pb(a);
	}	
	dfs(1);
	dfs1(1,1,1);
	cin >> m;
	while(m--){
		ll a,b,c;
		cin >> a >> b >> c;
		ll x=lca(a,b);
		path[x].pb({a,b,c});
	}
	solve(1);
	cout << in[1];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 5 ms 5356 KB Output is correct
5 Correct 161 ms 32108 KB Output is correct
6 Correct 85 ms 40812 KB Output is correct
7 Correct 170 ms 37876 KB Output is correct
8 Correct 140 ms 31468 KB Output is correct
9 Correct 154 ms 36076 KB Output is correct
10 Correct 111 ms 31852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 5 ms 5484 KB Output is correct
4 Correct 183 ms 44240 KB Output is correct
5 Correct 183 ms 44352 KB Output is correct
6 Correct 168 ms 44268 KB Output is correct
7 Correct 176 ms 44268 KB Output is correct
8 Correct 174 ms 44268 KB Output is correct
9 Correct 172 ms 44524 KB Output is correct
10 Correct 186 ms 44268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 5 ms 5484 KB Output is correct
4 Correct 183 ms 44240 KB Output is correct
5 Correct 183 ms 44352 KB Output is correct
6 Correct 168 ms 44268 KB Output is correct
7 Correct 176 ms 44268 KB Output is correct
8 Correct 174 ms 44268 KB Output is correct
9 Correct 172 ms 44524 KB Output is correct
10 Correct 186 ms 44268 KB Output is correct
11 Correct 17 ms 6380 KB Output is correct
12 Correct 189 ms 44396 KB Output is correct
13 Correct 180 ms 44268 KB Output is correct
14 Correct 169 ms 44412 KB Output is correct
15 Correct 179 ms 44396 KB Output is correct
16 Correct 180 ms 44396 KB Output is correct
17 Correct 173 ms 44268 KB Output is correct
18 Correct 180 ms 44396 KB Output is correct
19 Correct 196 ms 44268 KB Output is correct
20 Correct 185 ms 44268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 477 ms 35104 KB Output is correct
2 Correct 198 ms 44268 KB Output is correct
3 Correct 340 ms 41000 KB Output is correct
4 Correct 324 ms 37244 KB Output is correct
5 Correct 293 ms 42792 KB Output is correct
6 Correct 278 ms 36952 KB Output is correct
7 Correct 365 ms 42404 KB Output is correct
8 Correct 379 ms 37612 KB Output is correct
9 Correct 197 ms 46700 KB Output is correct
10 Correct 370 ms 41256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 5 ms 5356 KB Output is correct
5 Correct 161 ms 32108 KB Output is correct
6 Correct 85 ms 40812 KB Output is correct
7 Correct 170 ms 37876 KB Output is correct
8 Correct 140 ms 31468 KB Output is correct
9 Correct 154 ms 36076 KB Output is correct
10 Correct 111 ms 31852 KB Output is correct
11 Correct 6 ms 5356 KB Output is correct
12 Correct 5 ms 5484 KB Output is correct
13 Correct 5 ms 5484 KB Output is correct
14 Correct 5 ms 5484 KB Output is correct
15 Correct 5 ms 5356 KB Output is correct
16 Correct 5 ms 5484 KB Output is correct
17 Correct 5 ms 5484 KB Output is correct
18 Correct 5 ms 5484 KB Output is correct
19 Correct 6 ms 5612 KB Output is correct
20 Correct 6 ms 5524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 5 ms 5356 KB Output is correct
5 Correct 161 ms 32108 KB Output is correct
6 Correct 85 ms 40812 KB Output is correct
7 Correct 170 ms 37876 KB Output is correct
8 Correct 140 ms 31468 KB Output is correct
9 Correct 154 ms 36076 KB Output is correct
10 Correct 111 ms 31852 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 5 ms 5484 KB Output is correct
14 Correct 183 ms 44240 KB Output is correct
15 Correct 183 ms 44352 KB Output is correct
16 Correct 168 ms 44268 KB Output is correct
17 Correct 176 ms 44268 KB Output is correct
18 Correct 174 ms 44268 KB Output is correct
19 Correct 172 ms 44524 KB Output is correct
20 Correct 186 ms 44268 KB Output is correct
21 Correct 17 ms 6380 KB Output is correct
22 Correct 189 ms 44396 KB Output is correct
23 Correct 180 ms 44268 KB Output is correct
24 Correct 169 ms 44412 KB Output is correct
25 Correct 179 ms 44396 KB Output is correct
26 Correct 180 ms 44396 KB Output is correct
27 Correct 173 ms 44268 KB Output is correct
28 Correct 180 ms 44396 KB Output is correct
29 Correct 196 ms 44268 KB Output is correct
30 Correct 185 ms 44268 KB Output is correct
31 Correct 477 ms 35104 KB Output is correct
32 Correct 198 ms 44268 KB Output is correct
33 Correct 340 ms 41000 KB Output is correct
34 Correct 324 ms 37244 KB Output is correct
35 Correct 293 ms 42792 KB Output is correct
36 Correct 278 ms 36952 KB Output is correct
37 Correct 365 ms 42404 KB Output is correct
38 Correct 379 ms 37612 KB Output is correct
39 Correct 197 ms 46700 KB Output is correct
40 Correct 370 ms 41256 KB Output is correct
41 Correct 6 ms 5356 KB Output is correct
42 Correct 5 ms 5484 KB Output is correct
43 Correct 5 ms 5484 KB Output is correct
44 Correct 5 ms 5484 KB Output is correct
45 Correct 5 ms 5356 KB Output is correct
46 Correct 5 ms 5484 KB Output is correct
47 Correct 5 ms 5484 KB Output is correct
48 Correct 5 ms 5484 KB Output is correct
49 Correct 6 ms 5612 KB Output is correct
50 Correct 6 ms 5524 KB Output is correct
51 Correct 352 ms 37928 KB Output is correct
52 Correct 178 ms 47112 KB Output is correct
53 Correct 402 ms 41632 KB Output is correct
54 Correct 251 ms 37344 KB Output is correct
55 Correct 494 ms 37944 KB Output is correct
56 Correct 188 ms 47084 KB Output is correct
57 Correct 297 ms 42408 KB Output is correct
58 Correct 285 ms 37412 KB Output is correct
59 Correct 377 ms 37988 KB Output is correct
60 Correct 190 ms 47084 KB Output is correct
61 Correct 327 ms 42664 KB Output is correct
62 Correct 274 ms 37056 KB Output is correct
63 Correct 508 ms 37508 KB Output is correct
64 Correct 175 ms 46956 KB Output is correct
65 Correct 383 ms 42528 KB Output is correct
66 Correct 234 ms 37148 KB Output is correct
67 Correct 485 ms 37860 KB Output is correct
68 Correct 178 ms 46956 KB Output is correct
69 Correct 292 ms 41028 KB Output is correct
70 Correct 295 ms 37268 KB Output is correct