Submission #329764

# Submission time Handle Problem Language Result Execution time Memory
329764 2020-11-22T11:40:28 Z GioChkhaidze Election Campaign (JOI15_election_campaign) C++14
100 / 100
346 ms 33260 KB
#include <bits/stdc++.h>

#define Tree int h,int l,int r
#define Left (h<<1),l,(l+r)>>1
#define Right ((h<<1)|1),((l+r)>>1)+1,r
#define ll long long
#define pb push_back
#define F first
#define S second

using namespace std;

const int N=1e5+5;

int n,q,depth,timer;
ll S[N],dp[N],t[4*N];
int in[N],out[N],dep[N],P[N][19];

vector < int > v[N];
vector < pair < pair < int , int > , int > > E[N];

bool anc(int a,int b) {
	return (in[a]<=in[b] && out[b]<=out[a]);
}

int LCA(int a,int b) {
	if (dep[a]>dep[b]) swap(a,b);
	if (anc(a,b)) return a;
	for (int i=17; i>=0; i--) 
		if (!anc(P[a][i],b)) a=P[a][i];
	return P[a][0];
}

void Dfs(int x,int p) {
	P[x][0]=p;
	in[x]=++timer;
	dep[x]=++depth;
	for (int i=0; i<v[x].size(); i++) {
		int to=v[x][i];
		if (to==p) continue;
		Dfs(to,x);
	}
	out[x]=timer;
	--depth;
}

void Upd(Tree,int L,int R,ll dl) {
	if (r<L || R<l) return ;
	if (L<=l && r<=R) {
		t[h]+=dl;
		return ;
	} 
	
	Upd(Left,L,R,dl);
	Upd(Right,L,R,dl);
}

ll get(Tree,int id) {
	if (id<l || r<id) return 0;
	if (l==id && r==id) return t[h];
	return get(Left,id)+get(Right,id)+t[h];
}

void Ufs(int x,int p) {
	for (int i=0; i<v[x].size(); i++) {
		int to=v[x][i];
		if (to==p) continue;
		Ufs(to,x);
		S[x]+=dp[to];
	}
	
	dp[x]=S[x];
	for (int i=0; i<E[x].size(); i++) {
		int a=E[x][i].F.F,b=E[x][i].F.S;
		ll cost=E[x][i].S;
		
		if (b!=x) 
			cost+=get(1,1,n,in[a])+get(1,1,n,in[b]);
				else
			cost+=get(1,1,n,in[a]);		

		dp[x]=max(dp[x],S[x]+cost);
	}
	
	Upd(1,1,n,in[x],out[x],S[x]-dp[x]);
} 

main () {
	cin>>n;
	for (int i=1; i<n; i++) {
		int a,b;
		cin>>a>>b;
		v[a].pb(b);
		v[b].pb(a);
	}
	
	Dfs(1,1);
	
	for (int j=1; j<=17; j++)
		for (int i=1; i<=n; i++) 
			P[i][j]=P[P[i][j-1]][j-1];
	
	cin>>q;
	for (int i=1; i<=q; i++) {
		int a,b,c;
		cin>>a>>b>>c;
		if (dep[a]<dep[b]) swap(a,b);
		E[LCA(a,b)].pb({{a,b},c});
	}
	
	Ufs(1,1);
	
	cout<<dp[1]<<"\n";
}

Compilation message

election_campaign.cpp: In function 'void Dfs(int, int)':
election_campaign.cpp:38:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |  for (int i=0; i<v[x].size(); i++) {
      |                ~^~~~~~~~~~~~
election_campaign.cpp: In function 'void Ufs(int, int)':
election_campaign.cpp:65:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |  for (int i=0; i<v[x].size(); i++) {
      |                ~^~~~~~~~~~~~
election_campaign.cpp:73:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |  for (int i=0; i<E[x].size(); i++) {
      |                ~^~~~~~~~~~~~
election_campaign.cpp: At global scope:
election_campaign.cpp:88:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   88 | main () {
      |       ^
# Verdict Execution time Memory 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 5228 KB Output is correct
5 Correct 168 ms 21740 KB Output is correct
6 Correct 112 ms 29420 KB Output is correct
7 Correct 172 ms 26860 KB Output is correct
8 Correct 134 ms 20972 KB Output is correct
9 Correct 161 ms 25216 KB Output is correct
10 Correct 146 ms 21228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 5 ms 5356 KB Output is correct
4 Correct 225 ms 32620 KB Output is correct
5 Correct 234 ms 32752 KB Output is correct
6 Correct 225 ms 32672 KB Output is correct
7 Correct 226 ms 32748 KB Output is correct
8 Correct 242 ms 32952 KB Output is correct
9 Correct 227 ms 32768 KB Output is correct
10 Correct 223 ms 32748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 5 ms 5356 KB Output is correct
4 Correct 225 ms 32620 KB Output is correct
5 Correct 234 ms 32752 KB Output is correct
6 Correct 225 ms 32672 KB Output is correct
7 Correct 226 ms 32748 KB Output is correct
8 Correct 242 ms 32952 KB Output is correct
9 Correct 227 ms 32768 KB Output is correct
10 Correct 223 ms 32748 KB Output is correct
11 Correct 25 ms 6144 KB Output is correct
12 Correct 249 ms 33004 KB Output is correct
13 Correct 239 ms 33260 KB Output is correct
14 Correct 233 ms 33132 KB Output is correct
15 Correct 230 ms 33004 KB Output is correct
16 Correct 231 ms 33004 KB Output is correct
17 Correct 232 ms 33004 KB Output is correct
18 Correct 236 ms 33004 KB Output is correct
19 Correct 234 ms 33004 KB Output is correct
20 Correct 232 ms 33124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 346 ms 24484 KB Output is correct
2 Correct 227 ms 32788 KB Output is correct
3 Correct 325 ms 29548 KB Output is correct
4 Correct 288 ms 24104 KB Output is correct
5 Correct 303 ms 29164 KB Output is correct
6 Correct 288 ms 23916 KB Output is correct
7 Correct 324 ms 28840 KB Output is correct
8 Correct 310 ms 24812 KB Output is correct
9 Correct 218 ms 32748 KB Output is correct
10 Correct 328 ms 27812 KB Output is correct
# Verdict Execution time Memory 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 5228 KB Output is correct
5 Correct 168 ms 21740 KB Output is correct
6 Correct 112 ms 29420 KB Output is correct
7 Correct 172 ms 26860 KB Output is correct
8 Correct 134 ms 20972 KB Output is correct
9 Correct 161 ms 25216 KB Output is correct
10 Correct 146 ms 21228 KB Output is correct
11 Correct 6 ms 5228 KB Output is correct
12 Correct 5 ms 5356 KB Output is correct
13 Correct 6 ms 5228 KB Output is correct
14 Correct 5 ms 5228 KB Output is correct
15 Correct 6 ms 5228 KB Output is correct
16 Correct 6 ms 5228 KB Output is correct
17 Correct 6 ms 5248 KB Output is correct
18 Correct 6 ms 5356 KB Output is correct
19 Correct 5 ms 5228 KB Output is correct
20 Correct 5 ms 5356 KB Output is correct
# Verdict Execution time Memory 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 5228 KB Output is correct
5 Correct 168 ms 21740 KB Output is correct
6 Correct 112 ms 29420 KB Output is correct
7 Correct 172 ms 26860 KB Output is correct
8 Correct 134 ms 20972 KB Output is correct
9 Correct 161 ms 25216 KB Output is correct
10 Correct 146 ms 21228 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 5356 KB Output is correct
14 Correct 225 ms 32620 KB Output is correct
15 Correct 234 ms 32752 KB Output is correct
16 Correct 225 ms 32672 KB Output is correct
17 Correct 226 ms 32748 KB Output is correct
18 Correct 242 ms 32952 KB Output is correct
19 Correct 227 ms 32768 KB Output is correct
20 Correct 223 ms 32748 KB Output is correct
21 Correct 25 ms 6144 KB Output is correct
22 Correct 249 ms 33004 KB Output is correct
23 Correct 239 ms 33260 KB Output is correct
24 Correct 233 ms 33132 KB Output is correct
25 Correct 230 ms 33004 KB Output is correct
26 Correct 231 ms 33004 KB Output is correct
27 Correct 232 ms 33004 KB Output is correct
28 Correct 236 ms 33004 KB Output is correct
29 Correct 234 ms 33004 KB Output is correct
30 Correct 232 ms 33124 KB Output is correct
31 Correct 346 ms 24484 KB Output is correct
32 Correct 227 ms 32788 KB Output is correct
33 Correct 325 ms 29548 KB Output is correct
34 Correct 288 ms 24104 KB Output is correct
35 Correct 303 ms 29164 KB Output is correct
36 Correct 288 ms 23916 KB Output is correct
37 Correct 324 ms 28840 KB Output is correct
38 Correct 310 ms 24812 KB Output is correct
39 Correct 218 ms 32748 KB Output is correct
40 Correct 328 ms 27812 KB Output is correct
41 Correct 6 ms 5228 KB Output is correct
42 Correct 5 ms 5356 KB Output is correct
43 Correct 6 ms 5228 KB Output is correct
44 Correct 5 ms 5228 KB Output is correct
45 Correct 6 ms 5228 KB Output is correct
46 Correct 6 ms 5228 KB Output is correct
47 Correct 6 ms 5248 KB Output is correct
48 Correct 6 ms 5356 KB Output is correct
49 Correct 5 ms 5228 KB Output is correct
50 Correct 5 ms 5356 KB Output is correct
51 Correct 324 ms 25244 KB Output is correct
52 Correct 246 ms 33076 KB Output is correct
53 Correct 345 ms 28304 KB Output is correct
54 Correct 266 ms 24232 KB Output is correct
55 Correct 341 ms 25004 KB Output is correct
56 Correct 230 ms 33004 KB Output is correct
57 Correct 309 ms 29164 KB Output is correct
58 Correct 294 ms 23972 KB Output is correct
59 Correct 320 ms 25196 KB Output is correct
60 Correct 234 ms 33004 KB Output is correct
61 Correct 305 ms 29164 KB Output is correct
62 Correct 299 ms 23880 KB Output is correct
63 Correct 334 ms 24680 KB Output is correct
64 Correct 240 ms 33132 KB Output is correct
65 Correct 330 ms 28836 KB Output is correct
66 Correct 271 ms 24104 KB Output is correct
67 Correct 333 ms 24672 KB Output is correct
68 Correct 234 ms 33004 KB Output is correct
69 Correct 308 ms 27884 KB Output is correct
70 Correct 295 ms 24400 KB Output is correct