Submission #158161

# Submission time Handle Problem Language Result Execution time Memory
158161 2019-10-15T06:32:34 Z username Mergers (JOI19_mergers) C++14
100 / 100
1955 ms 172148 KB
#pragma GCC optimize("O3")
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define tr(it,a) for(auto it:a)
#define pob pop_back
#define pf push_front
#define pof pop_front
#define umin(x,y) (x=min(x,(y)))
#define umax(x,y) (x=max(x,(y)))
#define mid ((l+r)/2)
#define lch (idx*2+1)
#define rch (idx*2+2)
//
#include<bits/stdc++.h>
#define int int_fast64_t
using namespace std;
typedef pair<int,int> pii;
#define rep(i,j,k) for(int i=(j);i<(k);++i)
#define rrep(i,j,k) for(int i=int(j)-1;i>=(k);--i)
#define all(a) a.begin(),a.end()
#define pb push_back
#define f first
#define s second
#define endl '\n'
 #define __debug 
#ifdef __debug
	#define ios (void)0
	#define pr(...) cerr<<__VA_ARGS__
	#define ar(a,s,t) {rep(zy,s,t)pr(a[zy]<<' ');pr(endl);}
#else
	#define ios cin.tie(0),cout.tie(0),ios_base::sync_with_stdio(false)
	#define pr(...) (void)0
	#define ar(...) (void)0
#endif
//
const int maxn=5e5+9,mxlgn=20;
int n,k,cl=0,res=0,s[maxn],h[maxn],in[maxn],ex[maxn],p[mxlgn][maxn];
vector<int>g[maxn],c[maxn];

int isa(int u,int v){return in[u]<=in[v]&&ex[v]<=ex[u];}

int lca(int u,int v){
	if(isa(u,v))return u;
	else if(isa(v,u))return v;
	else{
		rrep(i,mxlgn,0)if(p[i][u]>=0&&!isa(p[i][u],v))u=p[i][u];
		return p[0][u];
	}
}

void dfs1(int u,int f){
	p[0][u]=f;
	in[u]=cl++;
	rep(i,0,g[u].size()){
		int v=g[u][i];
		if(v==f)continue;
		dfs1(v,u);
	}
	ex[u]=cl;
}

pii dfs2(int u,int f){
	int top=h[u],cnt=0;
	rep(i,0,g[u].size()){
		int v=g[u][i];
		if(v==f)continue;
		pii tt=dfs2(v,u);
		if(isa(tt.f,top))top=tt.f;
		cnt+=tt.s;
	}
	if(!u&&cnt==1)++res;
	if(u==top)res+=cnt<1,cnt=1;
	return {top,cnt};
}

main(){
	ios;
	cin>>n>>k;
	rep(i,1,n){
		int a,b;cin>>a>>b,--a,--b;
		g[a].pb(b),g[b].pb(a);
	}
	rep(i,0,n)cin>>s[i],--s[i],c[s[i]].pb(i);
	memset(p,-1,sizeof p);
	dfs1(0,-1);
	rep(i,0,mxlgn-1)rep(j,0,n)if(p[i][j]>=0)p[i+1][j]=p[i][p[i][j]];
	rep(i,0,k){
		int top=c[i][0];
		rep(j,1,c[i].size())top=lca(top,c[i][j]);
		rep(j,0,c[i].size())h[c[i][j]]=top;
	}
	dfs2(0,-1);
	cout<<(res>1?(res+1)/2:0)<<endl;
}

Compilation message

mergers.cpp: In function 'void dfs1(int_fast64_t, int_fast64_t)':
mergers.cpp:21:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i,j,k) for(int i=(j);i<(k);++i)
                                   ^
mergers.cpp:57:2: note: in expansion of macro 'rep'
  rep(i,0,g[u].size()){
  ^~~
mergers.cpp: In function 'pii dfs2(int_fast64_t, int_fast64_t)':
mergers.cpp:21:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i,j,k) for(int i=(j);i<(k);++i)
                                   ^
mergers.cpp:67:2: note: in expansion of macro 'rep'
  rep(i,0,g[u].size()){
  ^~~
mergers.cpp: At global scope:
mergers.cpp:79:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
mergers.cpp: In function 'int main()':
mergers.cpp:21:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i,j,k) for(int i=(j);i<(k);++i)
                                   ^
mergers.cpp:92:3: note: in expansion of macro 'rep'
   rep(j,1,c[i].size())top=lca(top,c[i][j]);
   ^~~
mergers.cpp:21:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i,j,k) for(int i=(j);i<(k);++i)
                                   ^
mergers.cpp:93:3: note: in expansion of macro 'rep'
   rep(j,0,c[i].size())h[c[i][j]]=top;
   ^~~
# Verdict Execution time Memory Grader output
1 Correct 90 ms 102136 KB Output is correct
2 Correct 91 ms 102176 KB Output is correct
3 Correct 91 ms 102136 KB Output is correct
4 Correct 91 ms 102136 KB Output is correct
5 Correct 91 ms 102120 KB Output is correct
6 Correct 103 ms 102108 KB Output is correct
7 Correct 91 ms 102136 KB Output is correct
8 Correct 92 ms 102136 KB Output is correct
9 Correct 111 ms 102216 KB Output is correct
10 Correct 91 ms 102060 KB Output is correct
11 Correct 91 ms 102136 KB Output is correct
12 Correct 90 ms 102140 KB Output is correct
13 Correct 97 ms 102100 KB Output is correct
14 Correct 90 ms 102132 KB Output is correct
15 Correct 91 ms 102136 KB Output is correct
16 Correct 90 ms 102064 KB Output is correct
17 Correct 90 ms 102136 KB Output is correct
18 Correct 90 ms 102136 KB Output is correct
19 Correct 89 ms 102136 KB Output is correct
20 Correct 90 ms 102136 KB Output is correct
21 Correct 91 ms 102136 KB Output is correct
22 Correct 91 ms 102104 KB Output is correct
23 Correct 90 ms 102136 KB Output is correct
24 Correct 91 ms 102108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 102136 KB Output is correct
2 Correct 91 ms 102176 KB Output is correct
3 Correct 91 ms 102136 KB Output is correct
4 Correct 91 ms 102136 KB Output is correct
5 Correct 91 ms 102120 KB Output is correct
6 Correct 103 ms 102108 KB Output is correct
7 Correct 91 ms 102136 KB Output is correct
8 Correct 92 ms 102136 KB Output is correct
9 Correct 111 ms 102216 KB Output is correct
10 Correct 91 ms 102060 KB Output is correct
11 Correct 91 ms 102136 KB Output is correct
12 Correct 90 ms 102140 KB Output is correct
13 Correct 97 ms 102100 KB Output is correct
14 Correct 90 ms 102132 KB Output is correct
15 Correct 91 ms 102136 KB Output is correct
16 Correct 90 ms 102064 KB Output is correct
17 Correct 90 ms 102136 KB Output is correct
18 Correct 90 ms 102136 KB Output is correct
19 Correct 89 ms 102136 KB Output is correct
20 Correct 90 ms 102136 KB Output is correct
21 Correct 91 ms 102136 KB Output is correct
22 Correct 91 ms 102104 KB Output is correct
23 Correct 90 ms 102136 KB Output is correct
24 Correct 91 ms 102108 KB Output is correct
25 Correct 90 ms 102240 KB Output is correct
26 Correct 95 ms 102428 KB Output is correct
27 Correct 94 ms 102392 KB Output is correct
28 Correct 95 ms 102520 KB Output is correct
29 Correct 95 ms 102520 KB Output is correct
30 Correct 95 ms 102388 KB Output is correct
31 Correct 90 ms 102136 KB Output is correct
32 Correct 106 ms 102520 KB Output is correct
33 Correct 102 ms 102136 KB Output is correct
34 Correct 107 ms 102392 KB Output is correct
35 Correct 109 ms 102520 KB Output is correct
36 Correct 100 ms 102448 KB Output is correct
37 Correct 95 ms 102392 KB Output is correct
38 Correct 104 ms 102136 KB Output is correct
39 Correct 99 ms 102392 KB Output is correct
40 Correct 93 ms 102392 KB Output is correct
41 Correct 95 ms 102392 KB Output is correct
42 Correct 101 ms 102460 KB Output is correct
43 Correct 107 ms 102492 KB Output is correct
44 Correct 93 ms 102160 KB Output is correct
45 Correct 96 ms 102392 KB Output is correct
46 Correct 95 ms 102372 KB Output is correct
47 Correct 91 ms 102136 KB Output is correct
48 Correct 95 ms 102392 KB Output is correct
49 Correct 95 ms 102396 KB Output is correct
50 Correct 95 ms 102496 KB Output is correct
51 Correct 112 ms 102392 KB Output is correct
52 Correct 100 ms 102392 KB Output is correct
53 Correct 95 ms 102392 KB Output is correct
54 Correct 95 ms 102364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 102136 KB Output is correct
2 Correct 91 ms 102176 KB Output is correct
3 Correct 91 ms 102136 KB Output is correct
4 Correct 91 ms 102136 KB Output is correct
5 Correct 91 ms 102120 KB Output is correct
6 Correct 103 ms 102108 KB Output is correct
7 Correct 91 ms 102136 KB Output is correct
8 Correct 92 ms 102136 KB Output is correct
9 Correct 111 ms 102216 KB Output is correct
10 Correct 91 ms 102060 KB Output is correct
11 Correct 91 ms 102136 KB Output is correct
12 Correct 90 ms 102140 KB Output is correct
13 Correct 97 ms 102100 KB Output is correct
14 Correct 90 ms 102132 KB Output is correct
15 Correct 91 ms 102136 KB Output is correct
16 Correct 90 ms 102064 KB Output is correct
17 Correct 90 ms 102136 KB Output is correct
18 Correct 90 ms 102136 KB Output is correct
19 Correct 89 ms 102136 KB Output is correct
20 Correct 90 ms 102136 KB Output is correct
21 Correct 91 ms 102136 KB Output is correct
22 Correct 91 ms 102104 KB Output is correct
23 Correct 90 ms 102136 KB Output is correct
24 Correct 91 ms 102108 KB Output is correct
25 Correct 90 ms 102280 KB Output is correct
26 Correct 257 ms 111568 KB Output is correct
27 Correct 307 ms 111480 KB Output is correct
28 Correct 95 ms 102392 KB Output is correct
29 Correct 105 ms 102180 KB Output is correct
30 Correct 103 ms 102076 KB Output is correct
31 Correct 286 ms 111312 KB Output is correct
32 Correct 94 ms 102392 KB Output is correct
33 Correct 308 ms 114956 KB Output is correct
34 Correct 300 ms 111492 KB Output is correct
35 Correct 95 ms 102396 KB Output is correct
36 Correct 301 ms 111140 KB Output is correct
37 Correct 94 ms 102364 KB Output is correct
38 Correct 95 ms 102468 KB Output is correct
39 Correct 256 ms 111460 KB Output is correct
40 Correct 95 ms 102648 KB Output is correct
41 Correct 265 ms 111304 KB Output is correct
42 Correct 294 ms 112248 KB Output is correct
43 Correct 90 ms 102136 KB Output is correct
44 Correct 323 ms 114928 KB Output is correct
45 Correct 294 ms 114128 KB Output is correct
46 Correct 94 ms 102392 KB Output is correct
47 Correct 94 ms 102428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 262 ms 110300 KB Output is correct
2 Correct 300 ms 112364 KB Output is correct
3 Correct 94 ms 102392 KB Output is correct
4 Correct 96 ms 102392 KB Output is correct
5 Correct 91 ms 102140 KB Output is correct
6 Correct 90 ms 102136 KB Output is correct
7 Correct 94 ms 102468 KB Output is correct
8 Correct 328 ms 112508 KB Output is correct
9 Correct 96 ms 102392 KB Output is correct
10 Correct 299 ms 111584 KB Output is correct
11 Correct 90 ms 102136 KB Output is correct
12 Correct 336 ms 111028 KB Output is correct
13 Correct 339 ms 112508 KB Output is correct
14 Correct 333 ms 114036 KB Output is correct
15 Correct 265 ms 111592 KB Output is correct
16 Correct 95 ms 102520 KB Output is correct
17 Correct 90 ms 102152 KB Output is correct
18 Correct 313 ms 113868 KB Output is correct
19 Correct 363 ms 115656 KB Output is correct
20 Correct 106 ms 102488 KB Output is correct
21 Correct 96 ms 102104 KB Output is correct
22 Correct 327 ms 112620 KB Output is correct
23 Correct 96 ms 102392 KB Output is correct
24 Correct 341 ms 111944 KB Output is correct
25 Correct 343 ms 115740 KB Output is correct
26 Correct 95 ms 102520 KB Output is correct
27 Correct 96 ms 102520 KB Output is correct
28 Correct 96 ms 102556 KB Output is correct
29 Correct 95 ms 102392 KB Output is correct
30 Correct 95 ms 102392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 102136 KB Output is correct
2 Correct 91 ms 102176 KB Output is correct
3 Correct 91 ms 102136 KB Output is correct
4 Correct 91 ms 102136 KB Output is correct
5 Correct 91 ms 102120 KB Output is correct
6 Correct 103 ms 102108 KB Output is correct
7 Correct 91 ms 102136 KB Output is correct
8 Correct 92 ms 102136 KB Output is correct
9 Correct 111 ms 102216 KB Output is correct
10 Correct 91 ms 102060 KB Output is correct
11 Correct 91 ms 102136 KB Output is correct
12 Correct 90 ms 102140 KB Output is correct
13 Correct 97 ms 102100 KB Output is correct
14 Correct 90 ms 102132 KB Output is correct
15 Correct 91 ms 102136 KB Output is correct
16 Correct 90 ms 102064 KB Output is correct
17 Correct 90 ms 102136 KB Output is correct
18 Correct 90 ms 102136 KB Output is correct
19 Correct 89 ms 102136 KB Output is correct
20 Correct 90 ms 102136 KB Output is correct
21 Correct 91 ms 102136 KB Output is correct
22 Correct 91 ms 102104 KB Output is correct
23 Correct 90 ms 102136 KB Output is correct
24 Correct 91 ms 102108 KB Output is correct
25 Correct 90 ms 102240 KB Output is correct
26 Correct 95 ms 102428 KB Output is correct
27 Correct 94 ms 102392 KB Output is correct
28 Correct 95 ms 102520 KB Output is correct
29 Correct 95 ms 102520 KB Output is correct
30 Correct 95 ms 102388 KB Output is correct
31 Correct 90 ms 102136 KB Output is correct
32 Correct 106 ms 102520 KB Output is correct
33 Correct 102 ms 102136 KB Output is correct
34 Correct 107 ms 102392 KB Output is correct
35 Correct 109 ms 102520 KB Output is correct
36 Correct 100 ms 102448 KB Output is correct
37 Correct 95 ms 102392 KB Output is correct
38 Correct 104 ms 102136 KB Output is correct
39 Correct 99 ms 102392 KB Output is correct
40 Correct 93 ms 102392 KB Output is correct
41 Correct 95 ms 102392 KB Output is correct
42 Correct 101 ms 102460 KB Output is correct
43 Correct 107 ms 102492 KB Output is correct
44 Correct 93 ms 102160 KB Output is correct
45 Correct 96 ms 102392 KB Output is correct
46 Correct 95 ms 102372 KB Output is correct
47 Correct 91 ms 102136 KB Output is correct
48 Correct 95 ms 102392 KB Output is correct
49 Correct 95 ms 102396 KB Output is correct
50 Correct 95 ms 102496 KB Output is correct
51 Correct 112 ms 102392 KB Output is correct
52 Correct 100 ms 102392 KB Output is correct
53 Correct 95 ms 102392 KB Output is correct
54 Correct 95 ms 102364 KB Output is correct
55 Correct 90 ms 102280 KB Output is correct
56 Correct 257 ms 111568 KB Output is correct
57 Correct 307 ms 111480 KB Output is correct
58 Correct 95 ms 102392 KB Output is correct
59 Correct 105 ms 102180 KB Output is correct
60 Correct 103 ms 102076 KB Output is correct
61 Correct 286 ms 111312 KB Output is correct
62 Correct 94 ms 102392 KB Output is correct
63 Correct 308 ms 114956 KB Output is correct
64 Correct 300 ms 111492 KB Output is correct
65 Correct 95 ms 102396 KB Output is correct
66 Correct 301 ms 111140 KB Output is correct
67 Correct 94 ms 102364 KB Output is correct
68 Correct 95 ms 102468 KB Output is correct
69 Correct 256 ms 111460 KB Output is correct
70 Correct 95 ms 102648 KB Output is correct
71 Correct 265 ms 111304 KB Output is correct
72 Correct 294 ms 112248 KB Output is correct
73 Correct 90 ms 102136 KB Output is correct
74 Correct 323 ms 114928 KB Output is correct
75 Correct 294 ms 114128 KB Output is correct
76 Correct 94 ms 102392 KB Output is correct
77 Correct 94 ms 102428 KB Output is correct
78 Correct 262 ms 110300 KB Output is correct
79 Correct 300 ms 112364 KB Output is correct
80 Correct 94 ms 102392 KB Output is correct
81 Correct 96 ms 102392 KB Output is correct
82 Correct 91 ms 102140 KB Output is correct
83 Correct 90 ms 102136 KB Output is correct
84 Correct 94 ms 102468 KB Output is correct
85 Correct 328 ms 112508 KB Output is correct
86 Correct 96 ms 102392 KB Output is correct
87 Correct 299 ms 111584 KB Output is correct
88 Correct 90 ms 102136 KB Output is correct
89 Correct 336 ms 111028 KB Output is correct
90 Correct 339 ms 112508 KB Output is correct
91 Correct 333 ms 114036 KB Output is correct
92 Correct 265 ms 111592 KB Output is correct
93 Correct 95 ms 102520 KB Output is correct
94 Correct 90 ms 102152 KB Output is correct
95 Correct 313 ms 113868 KB Output is correct
96 Correct 363 ms 115656 KB Output is correct
97 Correct 106 ms 102488 KB Output is correct
98 Correct 96 ms 102104 KB Output is correct
99 Correct 327 ms 112620 KB Output is correct
100 Correct 96 ms 102392 KB Output is correct
101 Correct 341 ms 111944 KB Output is correct
102 Correct 343 ms 115740 KB Output is correct
103 Correct 95 ms 102520 KB Output is correct
104 Correct 96 ms 102520 KB Output is correct
105 Correct 96 ms 102556 KB Output is correct
106 Correct 95 ms 102392 KB Output is correct
107 Correct 95 ms 102392 KB Output is correct
108 Correct 1493 ms 155968 KB Output is correct
109 Correct 1656 ms 155524 KB Output is correct
110 Correct 1708 ms 161700 KB Output is correct
111 Correct 1761 ms 172148 KB Output is correct
112 Correct 1708 ms 170724 KB Output is correct
113 Correct 1573 ms 161464 KB Output is correct
114 Correct 1335 ms 148396 KB Output is correct
115 Correct 1304 ms 148192 KB Output is correct
116 Correct 1907 ms 153676 KB Output is correct
117 Correct 1760 ms 162076 KB Output is correct
118 Correct 1349 ms 150308 KB Output is correct
119 Correct 1613 ms 162180 KB Output is correct
120 Correct 1929 ms 171208 KB Output is correct
121 Correct 1863 ms 162204 KB Output is correct
122 Correct 1955 ms 156584 KB Output is correct
123 Correct 1503 ms 163092 KB Output is correct
124 Correct 1760 ms 158176 KB Output is correct