Submission #287580

# Submission time Handle Problem Language Result Execution time Memory
287580 2020-08-31T20:17:37 Z YJU Mergers (JOI19_mergers) C++14
100 / 100
1582 ms 196708 KB
#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=5e5+5;
const ld pi=3.14159265359;
const ll INF=(1LL<<60);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()
 
ll dir[N],d[N],anc[N][21],x,y,n,k,s[N],vis[N],ans,dg[N];
vector<ll> v[N],node[N],col[N];
vector<pll> ed;
 
ll f(ll id){return (dir[id]==id?id:dir[id]=f(dir[id]));}
 
void DFS(ll nd,ll pa){
	anc[nd][0]=pa;
	REP1(i,20)anc[nd][i]=anc[anc[nd][i-1]][i-1];
	for(auto i:v[nd]){
		if(i==pa)continue;
		d[i]=d[nd]+1;
		DFS(i,nd);
	}
}
 
ll LCA(ll nda,ll ndb){
	if(d[nda]<d[ndb])swap(nda,ndb);
	for(int i=20;i>=0;i--)if(d[anc[nda][i]]>=d[ndb])nda=anc[nda][i];
	if(nda==ndb)return nda;
	for(int i=20;i>=0;i--){
		if(anc[nda][i]!=anc[ndb][i])nda=anc[nda][i],ndb=anc[ndb][i];
	}
	return anc[nda][0];
}
 
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin>>n>>k;
	REP1(i,n)dir[i]=i;
	REP(i,n-1){
		cin>>x>>y;
		v[x].pb(y);v[y].pb(x);
		ed.pb(mp(x,y));
	}
	DFS(1,0);
	REP1(i,n)cin>>s[i],node[s[i]].pb(i);
	REP1(i,k){
		if(!SZ(node[i]))continue;
		ll lca=node[i][0];
		for(auto j:node[i])lca=LCA(j,lca);
		lca=f(lca);
		for(auto j:node[i]){
			x=f(j);
			while(d[x]>d[lca]){
				dir[x]=f(anc[x][0]);
				x=f(anc[x][0]);
			}
		}
	}
	for(auto i:ed){
		if(f(i.X)!=f(i.Y)){
			++dg[f(i.X)];++dg[f(i.Y)];
		}
	}
	REP1(i,n){
		if(dg[i]==1)++ans;
	}
	cout<<(ans==1?0:(ans+1)/2)<<"\n";
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 25 ms 35584 KB Output is correct
2 Correct 24 ms 35584 KB Output is correct
3 Correct 24 ms 35576 KB Output is correct
4 Correct 24 ms 35584 KB Output is correct
5 Correct 24 ms 35576 KB Output is correct
6 Correct 24 ms 35584 KB Output is correct
7 Correct 24 ms 35576 KB Output is correct
8 Correct 25 ms 35584 KB Output is correct
9 Correct 24 ms 35584 KB Output is correct
10 Correct 24 ms 35664 KB Output is correct
11 Correct 24 ms 35584 KB Output is correct
12 Correct 24 ms 35576 KB Output is correct
13 Correct 24 ms 35584 KB Output is correct
14 Correct 25 ms 35584 KB Output is correct
15 Correct 24 ms 35584 KB Output is correct
16 Correct 24 ms 35584 KB Output is correct
17 Correct 24 ms 35584 KB Output is correct
18 Correct 24 ms 35576 KB Output is correct
19 Correct 24 ms 35584 KB Output is correct
20 Correct 24 ms 35576 KB Output is correct
21 Correct 24 ms 35584 KB Output is correct
22 Correct 24 ms 35584 KB Output is correct
23 Correct 24 ms 35584 KB Output is correct
24 Correct 24 ms 35584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 35584 KB Output is correct
2 Correct 24 ms 35584 KB Output is correct
3 Correct 24 ms 35576 KB Output is correct
4 Correct 24 ms 35584 KB Output is correct
5 Correct 24 ms 35576 KB Output is correct
6 Correct 24 ms 35584 KB Output is correct
7 Correct 24 ms 35576 KB Output is correct
8 Correct 25 ms 35584 KB Output is correct
9 Correct 24 ms 35584 KB Output is correct
10 Correct 24 ms 35664 KB Output is correct
11 Correct 24 ms 35584 KB Output is correct
12 Correct 24 ms 35576 KB Output is correct
13 Correct 24 ms 35584 KB Output is correct
14 Correct 25 ms 35584 KB Output is correct
15 Correct 24 ms 35584 KB Output is correct
16 Correct 24 ms 35584 KB Output is correct
17 Correct 24 ms 35584 KB Output is correct
18 Correct 24 ms 35576 KB Output is correct
19 Correct 24 ms 35584 KB Output is correct
20 Correct 24 ms 35576 KB Output is correct
21 Correct 24 ms 35584 KB Output is correct
22 Correct 24 ms 35584 KB Output is correct
23 Correct 24 ms 35584 KB Output is correct
24 Correct 24 ms 35584 KB Output is correct
25 Correct 24 ms 35584 KB Output is correct
26 Correct 27 ms 36428 KB Output is correct
27 Correct 27 ms 36472 KB Output is correct
28 Correct 28 ms 36608 KB Output is correct
29 Correct 27 ms 36480 KB Output is correct
30 Correct 26 ms 36480 KB Output is correct
31 Correct 24 ms 35584 KB Output is correct
32 Correct 27 ms 36608 KB Output is correct
33 Correct 24 ms 35584 KB Output is correct
34 Correct 27 ms 36352 KB Output is correct
35 Correct 27 ms 36480 KB Output is correct
36 Correct 27 ms 36480 KB Output is correct
37 Correct 27 ms 36472 KB Output is correct
38 Correct 24 ms 35584 KB Output is correct
39 Correct 27 ms 36472 KB Output is correct
40 Correct 26 ms 36480 KB Output is correct
41 Correct 27 ms 36472 KB Output is correct
42 Correct 27 ms 36480 KB Output is correct
43 Correct 26 ms 36608 KB Output is correct
44 Correct 24 ms 35652 KB Output is correct
45 Correct 27 ms 36480 KB Output is correct
46 Correct 27 ms 36384 KB Output is correct
47 Correct 24 ms 35584 KB Output is correct
48 Correct 27 ms 36480 KB Output is correct
49 Correct 27 ms 36604 KB Output is correct
50 Correct 27 ms 36480 KB Output is correct
51 Correct 27 ms 36480 KB Output is correct
52 Correct 26 ms 36476 KB Output is correct
53 Correct 27 ms 36480 KB Output is correct
54 Correct 27 ms 36344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 35584 KB Output is correct
2 Correct 24 ms 35584 KB Output is correct
3 Correct 24 ms 35576 KB Output is correct
4 Correct 24 ms 35584 KB Output is correct
5 Correct 24 ms 35576 KB Output is correct
6 Correct 24 ms 35584 KB Output is correct
7 Correct 24 ms 35576 KB Output is correct
8 Correct 25 ms 35584 KB Output is correct
9 Correct 24 ms 35584 KB Output is correct
10 Correct 24 ms 35664 KB Output is correct
11 Correct 24 ms 35584 KB Output is correct
12 Correct 24 ms 35576 KB Output is correct
13 Correct 24 ms 35584 KB Output is correct
14 Correct 25 ms 35584 KB Output is correct
15 Correct 24 ms 35584 KB Output is correct
16 Correct 24 ms 35584 KB Output is correct
17 Correct 24 ms 35584 KB Output is correct
18 Correct 24 ms 35576 KB Output is correct
19 Correct 24 ms 35584 KB Output is correct
20 Correct 24 ms 35576 KB Output is correct
21 Correct 24 ms 35584 KB Output is correct
22 Correct 24 ms 35584 KB Output is correct
23 Correct 24 ms 35584 KB Output is correct
24 Correct 24 ms 35584 KB Output is correct
25 Correct 24 ms 35584 KB Output is correct
26 Correct 131 ms 62300 KB Output is correct
27 Correct 159 ms 62180 KB Output is correct
28 Correct 27 ms 36480 KB Output is correct
29 Correct 24 ms 35576 KB Output is correct
30 Correct 24 ms 35584 KB Output is correct
31 Correct 160 ms 62180 KB Output is correct
32 Correct 26 ms 36352 KB Output is correct
33 Correct 202 ms 65892 KB Output is correct
34 Correct 156 ms 62308 KB Output is correct
35 Correct 27 ms 36472 KB Output is correct
36 Correct 198 ms 62180 KB Output is correct
37 Correct 27 ms 36472 KB Output is correct
38 Correct 27 ms 36480 KB Output is correct
39 Correct 131 ms 62308 KB Output is correct
40 Correct 27 ms 36608 KB Output is correct
41 Correct 149 ms 62952 KB Output is correct
42 Correct 215 ms 63076 KB Output is correct
43 Correct 24 ms 35576 KB Output is correct
44 Correct 192 ms 66792 KB Output is correct
45 Correct 196 ms 65124 KB Output is correct
46 Correct 26 ms 36472 KB Output is correct
47 Correct 27 ms 36352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 125 ms 60968 KB Output is correct
2 Correct 147 ms 63904 KB Output is correct
3 Correct 27 ms 36352 KB Output is correct
4 Correct 27 ms 36472 KB Output is correct
5 Correct 24 ms 35584 KB Output is correct
6 Correct 24 ms 35688 KB Output is correct
7 Correct 27 ms 36352 KB Output is correct
8 Correct 197 ms 64152 KB Output is correct
9 Correct 27 ms 36480 KB Output is correct
10 Correct 169 ms 63076 KB Output is correct
11 Correct 24 ms 35584 KB Output is correct
12 Correct 178 ms 62772 KB Output is correct
13 Correct 179 ms 64012 KB Output is correct
14 Correct 184 ms 65380 KB Output is correct
15 Correct 134 ms 62448 KB Output is correct
16 Correct 27 ms 36472 KB Output is correct
17 Correct 24 ms 35584 KB Output is correct
18 Correct 153 ms 65252 KB Output is correct
19 Correct 214 ms 67172 KB Output is correct
20 Correct 28 ms 36472 KB Output is correct
21 Correct 24 ms 35584 KB Output is correct
22 Correct 144 ms 64096 KB Output is correct
23 Correct 28 ms 36480 KB Output is correct
24 Correct 175 ms 63332 KB Output is correct
25 Correct 210 ms 67172 KB Output is correct
26 Correct 27 ms 36472 KB Output is correct
27 Correct 28 ms 36480 KB Output is correct
28 Correct 27 ms 36480 KB Output is correct
29 Correct 26 ms 36480 KB Output is correct
30 Correct 27 ms 36476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 35584 KB Output is correct
2 Correct 24 ms 35584 KB Output is correct
3 Correct 24 ms 35576 KB Output is correct
4 Correct 24 ms 35584 KB Output is correct
5 Correct 24 ms 35576 KB Output is correct
6 Correct 24 ms 35584 KB Output is correct
7 Correct 24 ms 35576 KB Output is correct
8 Correct 25 ms 35584 KB Output is correct
9 Correct 24 ms 35584 KB Output is correct
10 Correct 24 ms 35664 KB Output is correct
11 Correct 24 ms 35584 KB Output is correct
12 Correct 24 ms 35576 KB Output is correct
13 Correct 24 ms 35584 KB Output is correct
14 Correct 25 ms 35584 KB Output is correct
15 Correct 24 ms 35584 KB Output is correct
16 Correct 24 ms 35584 KB Output is correct
17 Correct 24 ms 35584 KB Output is correct
18 Correct 24 ms 35576 KB Output is correct
19 Correct 24 ms 35584 KB Output is correct
20 Correct 24 ms 35576 KB Output is correct
21 Correct 24 ms 35584 KB Output is correct
22 Correct 24 ms 35584 KB Output is correct
23 Correct 24 ms 35584 KB Output is correct
24 Correct 24 ms 35584 KB Output is correct
25 Correct 24 ms 35584 KB Output is correct
26 Correct 27 ms 36428 KB Output is correct
27 Correct 27 ms 36472 KB Output is correct
28 Correct 28 ms 36608 KB Output is correct
29 Correct 27 ms 36480 KB Output is correct
30 Correct 26 ms 36480 KB Output is correct
31 Correct 24 ms 35584 KB Output is correct
32 Correct 27 ms 36608 KB Output is correct
33 Correct 24 ms 35584 KB Output is correct
34 Correct 27 ms 36352 KB Output is correct
35 Correct 27 ms 36480 KB Output is correct
36 Correct 27 ms 36480 KB Output is correct
37 Correct 27 ms 36472 KB Output is correct
38 Correct 24 ms 35584 KB Output is correct
39 Correct 27 ms 36472 KB Output is correct
40 Correct 26 ms 36480 KB Output is correct
41 Correct 27 ms 36472 KB Output is correct
42 Correct 27 ms 36480 KB Output is correct
43 Correct 26 ms 36608 KB Output is correct
44 Correct 24 ms 35652 KB Output is correct
45 Correct 27 ms 36480 KB Output is correct
46 Correct 27 ms 36384 KB Output is correct
47 Correct 24 ms 35584 KB Output is correct
48 Correct 27 ms 36480 KB Output is correct
49 Correct 27 ms 36604 KB Output is correct
50 Correct 27 ms 36480 KB Output is correct
51 Correct 27 ms 36480 KB Output is correct
52 Correct 26 ms 36476 KB Output is correct
53 Correct 27 ms 36480 KB Output is correct
54 Correct 27 ms 36344 KB Output is correct
55 Correct 24 ms 35584 KB Output is correct
56 Correct 131 ms 62300 KB Output is correct
57 Correct 159 ms 62180 KB Output is correct
58 Correct 27 ms 36480 KB Output is correct
59 Correct 24 ms 35576 KB Output is correct
60 Correct 24 ms 35584 KB Output is correct
61 Correct 160 ms 62180 KB Output is correct
62 Correct 26 ms 36352 KB Output is correct
63 Correct 202 ms 65892 KB Output is correct
64 Correct 156 ms 62308 KB Output is correct
65 Correct 27 ms 36472 KB Output is correct
66 Correct 198 ms 62180 KB Output is correct
67 Correct 27 ms 36472 KB Output is correct
68 Correct 27 ms 36480 KB Output is correct
69 Correct 131 ms 62308 KB Output is correct
70 Correct 27 ms 36608 KB Output is correct
71 Correct 149 ms 62952 KB Output is correct
72 Correct 215 ms 63076 KB Output is correct
73 Correct 24 ms 35576 KB Output is correct
74 Correct 192 ms 66792 KB Output is correct
75 Correct 196 ms 65124 KB Output is correct
76 Correct 26 ms 36472 KB Output is correct
77 Correct 27 ms 36352 KB Output is correct
78 Correct 125 ms 60968 KB Output is correct
79 Correct 147 ms 63904 KB Output is correct
80 Correct 27 ms 36352 KB Output is correct
81 Correct 27 ms 36472 KB Output is correct
82 Correct 24 ms 35584 KB Output is correct
83 Correct 24 ms 35688 KB Output is correct
84 Correct 27 ms 36352 KB Output is correct
85 Correct 197 ms 64152 KB Output is correct
86 Correct 27 ms 36480 KB Output is correct
87 Correct 169 ms 63076 KB Output is correct
88 Correct 24 ms 35584 KB Output is correct
89 Correct 178 ms 62772 KB Output is correct
90 Correct 179 ms 64012 KB Output is correct
91 Correct 184 ms 65380 KB Output is correct
92 Correct 134 ms 62448 KB Output is correct
93 Correct 27 ms 36472 KB Output is correct
94 Correct 24 ms 35584 KB Output is correct
95 Correct 153 ms 65252 KB Output is correct
96 Correct 214 ms 67172 KB Output is correct
97 Correct 28 ms 36472 KB Output is correct
98 Correct 24 ms 35584 KB Output is correct
99 Correct 144 ms 64096 KB Output is correct
100 Correct 28 ms 36480 KB Output is correct
101 Correct 175 ms 63332 KB Output is correct
102 Correct 210 ms 67172 KB Output is correct
103 Correct 27 ms 36472 KB Output is correct
104 Correct 28 ms 36480 KB Output is correct
105 Correct 27 ms 36480 KB Output is correct
106 Correct 26 ms 36480 KB Output is correct
107 Correct 27 ms 36476 KB Output is correct
108 Correct 830 ms 179464 KB Output is correct
109 Correct 1358 ms 179240 KB Output is correct
110 Correct 1166 ms 186576 KB Output is correct
111 Correct 1483 ms 196708 KB Output is correct
112 Correct 1582 ms 195024 KB Output is correct
113 Correct 923 ms 185080 KB Output is correct
114 Correct 858 ms 169168 KB Output is correct
115 Correct 841 ms 171712 KB Output is correct
116 Correct 1166 ms 175696 KB Output is correct
117 Correct 1058 ms 185596 KB Output is correct
118 Correct 886 ms 170128 KB Output is correct
119 Correct 1045 ms 185552 KB Output is correct
120 Correct 1509 ms 195280 KB Output is correct
121 Correct 1035 ms 185552 KB Output is correct
122 Correct 1198 ms 180016 KB Output is correct
123 Correct 798 ms 186560 KB Output is correct
124 Correct 1325 ms 179780 KB Output is correct