Submission #287522

# Submission time Handle Problem Language Result Execution time Memory
287522 2020-08-31T18:43:42 Z YJU Mergers (JOI19_mergers) C++14
100 / 100
2245 ms 233044 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,k)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);
		for(auto j:node[i]){
			x=j;
			while(vis[x]!=i&&x!=lca){
				col[x].pb(i);
				vis[x]=i;
				x=anc[x][0];
			}
		}
		col[lca].pb(i);
	}
	REP1(i,n){
		REP(j,SZ(col[i])-1){
			dir[f(col[i][j])]=f(col[i][j+1]);
		}
	}
	for(auto i:ed){
		if(f(s[i.X])!=f(s[i.Y])){
			++dg[f(s[i.X])];++dg[f(s[i.Y])];
		}
	}
	REP1(i,k){
		if(dg[i]==1)++ans;
	}
	cout<<(ans==1?0:(ans+1)/2)<<"\n";
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 23 ms 35584 KB Output is correct
2 Correct 24 ms 35584 KB Output is correct
3 Correct 23 ms 35584 KB Output is correct
4 Correct 23 ms 35584 KB Output is correct
5 Correct 23 ms 35584 KB Output is correct
6 Correct 23 ms 35584 KB Output is correct
7 Correct 23 ms 35584 KB Output is correct
8 Correct 23 ms 35584 KB Output is correct
9 Correct 23 ms 35584 KB Output is correct
10 Correct 23 ms 35576 KB Output is correct
11 Correct 23 ms 35584 KB Output is correct
12 Correct 23 ms 35684 KB Output is correct
13 Correct 25 ms 35584 KB Output is correct
14 Correct 24 ms 35712 KB Output is correct
15 Correct 24 ms 35584 KB Output is correct
16 Correct 23 ms 35584 KB Output is correct
17 Correct 23 ms 35584 KB Output is correct
18 Correct 23 ms 35584 KB Output is correct
19 Correct 23 ms 35584 KB Output is correct
20 Correct 23 ms 35584 KB Output is correct
21 Correct 24 ms 35712 KB Output is correct
22 Correct 23 ms 35584 KB Output is correct
23 Correct 23 ms 35584 KB Output is correct
24 Correct 23 ms 35584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 35584 KB Output is correct
2 Correct 24 ms 35584 KB Output is correct
3 Correct 23 ms 35584 KB Output is correct
4 Correct 23 ms 35584 KB Output is correct
5 Correct 23 ms 35584 KB Output is correct
6 Correct 23 ms 35584 KB Output is correct
7 Correct 23 ms 35584 KB Output is correct
8 Correct 23 ms 35584 KB Output is correct
9 Correct 23 ms 35584 KB Output is correct
10 Correct 23 ms 35576 KB Output is correct
11 Correct 23 ms 35584 KB Output is correct
12 Correct 23 ms 35684 KB Output is correct
13 Correct 25 ms 35584 KB Output is correct
14 Correct 24 ms 35712 KB Output is correct
15 Correct 24 ms 35584 KB Output is correct
16 Correct 23 ms 35584 KB Output is correct
17 Correct 23 ms 35584 KB Output is correct
18 Correct 23 ms 35584 KB Output is correct
19 Correct 23 ms 35584 KB Output is correct
20 Correct 23 ms 35584 KB Output is correct
21 Correct 24 ms 35712 KB Output is correct
22 Correct 23 ms 35584 KB Output is correct
23 Correct 23 ms 35584 KB Output is correct
24 Correct 23 ms 35584 KB Output is correct
25 Correct 22 ms 35584 KB Output is correct
26 Correct 26 ms 36480 KB Output is correct
27 Correct 26 ms 36480 KB Output is correct
28 Correct 27 ms 36608 KB Output is correct
29 Correct 26 ms 36608 KB Output is correct
30 Correct 27 ms 36608 KB Output is correct
31 Correct 23 ms 35584 KB Output is correct
32 Correct 27 ms 36728 KB Output is correct
33 Correct 24 ms 35712 KB Output is correct
34 Correct 26 ms 36480 KB Output is correct
35 Correct 27 ms 36608 KB Output is correct
36 Correct 26 ms 36472 KB Output is correct
37 Correct 28 ms 36608 KB Output is correct
38 Correct 23 ms 35584 KB Output is correct
39 Correct 29 ms 36736 KB Output is correct
40 Correct 25 ms 36480 KB Output is correct
41 Correct 26 ms 36480 KB Output is correct
42 Correct 26 ms 36480 KB Output is correct
43 Correct 27 ms 36600 KB Output is correct
44 Correct 23 ms 35584 KB Output is correct
45 Correct 27 ms 36736 KB Output is correct
46 Correct 27 ms 36480 KB Output is correct
47 Correct 24 ms 35712 KB Output is correct
48 Correct 26 ms 36480 KB Output is correct
49 Correct 26 ms 36608 KB Output is correct
50 Correct 27 ms 36608 KB Output is correct
51 Correct 28 ms 36604 KB Output is correct
52 Correct 26 ms 36472 KB Output is correct
53 Correct 26 ms 36480 KB Output is correct
54 Correct 27 ms 36608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 35584 KB Output is correct
2 Correct 24 ms 35584 KB Output is correct
3 Correct 23 ms 35584 KB Output is correct
4 Correct 23 ms 35584 KB Output is correct
5 Correct 23 ms 35584 KB Output is correct
6 Correct 23 ms 35584 KB Output is correct
7 Correct 23 ms 35584 KB Output is correct
8 Correct 23 ms 35584 KB Output is correct
9 Correct 23 ms 35584 KB Output is correct
10 Correct 23 ms 35576 KB Output is correct
11 Correct 23 ms 35584 KB Output is correct
12 Correct 23 ms 35684 KB Output is correct
13 Correct 25 ms 35584 KB Output is correct
14 Correct 24 ms 35712 KB Output is correct
15 Correct 24 ms 35584 KB Output is correct
16 Correct 23 ms 35584 KB Output is correct
17 Correct 23 ms 35584 KB Output is correct
18 Correct 23 ms 35584 KB Output is correct
19 Correct 23 ms 35584 KB Output is correct
20 Correct 23 ms 35584 KB Output is correct
21 Correct 24 ms 35712 KB Output is correct
22 Correct 23 ms 35584 KB Output is correct
23 Correct 23 ms 35584 KB Output is correct
24 Correct 23 ms 35584 KB Output is correct
25 Correct 23 ms 35712 KB Output is correct
26 Correct 134 ms 63968 KB Output is correct
27 Correct 235 ms 66660 KB Output is correct
28 Correct 26 ms 36480 KB Output is correct
29 Correct 23 ms 35584 KB Output is correct
30 Correct 23 ms 35576 KB Output is correct
31 Correct 164 ms 63808 KB Output is correct
32 Correct 26 ms 36608 KB Output is correct
33 Correct 1215 ms 116400 KB Output is correct
34 Correct 240 ms 65892 KB Output is correct
35 Correct 26 ms 36472 KB Output is correct
36 Correct 202 ms 63552 KB Output is correct
37 Correct 26 ms 36472 KB Output is correct
38 Correct 26 ms 36480 KB Output is correct
39 Correct 145 ms 63852 KB Output is correct
40 Correct 26 ms 36608 KB Output is correct
41 Correct 153 ms 63640 KB Output is correct
42 Correct 771 ms 88932 KB Output is correct
43 Correct 24 ms 35824 KB Output is correct
44 Correct 205 ms 67684 KB Output is correct
45 Correct 750 ms 90596 KB Output is correct
46 Correct 27 ms 36480 KB Output is correct
47 Correct 28 ms 36592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 135 ms 63840 KB Output is correct
2 Correct 170 ms 67024 KB Output is correct
3 Correct 27 ms 36480 KB Output is correct
4 Correct 26 ms 36420 KB Output is correct
5 Correct 24 ms 35584 KB Output is correct
6 Correct 23 ms 35584 KB Output is correct
7 Correct 26 ms 36480 KB Output is correct
8 Correct 264 ms 67336 KB Output is correct
9 Correct 27 ms 36480 KB Output is correct
10 Correct 167 ms 63848 KB Output is correct
11 Correct 23 ms 35576 KB Output is correct
12 Correct 194 ms 63376 KB Output is correct
13 Correct 208 ms 65636 KB Output is correct
14 Correct 198 ms 66788 KB Output is correct
15 Correct 141 ms 63988 KB Output is correct
16 Correct 27 ms 36496 KB Output is correct
17 Correct 23 ms 35584 KB Output is correct
18 Correct 174 ms 67556 KB Output is correct
19 Correct 250 ms 68708 KB Output is correct
20 Correct 27 ms 36480 KB Output is correct
21 Correct 23 ms 35584 KB Output is correct
22 Correct 164 ms 66012 KB Output is correct
23 Correct 27 ms 36472 KB Output is correct
24 Correct 187 ms 64100 KB Output is correct
25 Correct 243 ms 68580 KB Output is correct
26 Correct 27 ms 36600 KB Output is correct
27 Correct 27 ms 36600 KB Output is correct
28 Correct 26 ms 36480 KB Output is correct
29 Correct 26 ms 36480 KB Output is correct
30 Correct 29 ms 36472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 35584 KB Output is correct
2 Correct 24 ms 35584 KB Output is correct
3 Correct 23 ms 35584 KB Output is correct
4 Correct 23 ms 35584 KB Output is correct
5 Correct 23 ms 35584 KB Output is correct
6 Correct 23 ms 35584 KB Output is correct
7 Correct 23 ms 35584 KB Output is correct
8 Correct 23 ms 35584 KB Output is correct
9 Correct 23 ms 35584 KB Output is correct
10 Correct 23 ms 35576 KB Output is correct
11 Correct 23 ms 35584 KB Output is correct
12 Correct 23 ms 35684 KB Output is correct
13 Correct 25 ms 35584 KB Output is correct
14 Correct 24 ms 35712 KB Output is correct
15 Correct 24 ms 35584 KB Output is correct
16 Correct 23 ms 35584 KB Output is correct
17 Correct 23 ms 35584 KB Output is correct
18 Correct 23 ms 35584 KB Output is correct
19 Correct 23 ms 35584 KB Output is correct
20 Correct 23 ms 35584 KB Output is correct
21 Correct 24 ms 35712 KB Output is correct
22 Correct 23 ms 35584 KB Output is correct
23 Correct 23 ms 35584 KB Output is correct
24 Correct 23 ms 35584 KB Output is correct
25 Correct 22 ms 35584 KB Output is correct
26 Correct 26 ms 36480 KB Output is correct
27 Correct 26 ms 36480 KB Output is correct
28 Correct 27 ms 36608 KB Output is correct
29 Correct 26 ms 36608 KB Output is correct
30 Correct 27 ms 36608 KB Output is correct
31 Correct 23 ms 35584 KB Output is correct
32 Correct 27 ms 36728 KB Output is correct
33 Correct 24 ms 35712 KB Output is correct
34 Correct 26 ms 36480 KB Output is correct
35 Correct 27 ms 36608 KB Output is correct
36 Correct 26 ms 36472 KB Output is correct
37 Correct 28 ms 36608 KB Output is correct
38 Correct 23 ms 35584 KB Output is correct
39 Correct 29 ms 36736 KB Output is correct
40 Correct 25 ms 36480 KB Output is correct
41 Correct 26 ms 36480 KB Output is correct
42 Correct 26 ms 36480 KB Output is correct
43 Correct 27 ms 36600 KB Output is correct
44 Correct 23 ms 35584 KB Output is correct
45 Correct 27 ms 36736 KB Output is correct
46 Correct 27 ms 36480 KB Output is correct
47 Correct 24 ms 35712 KB Output is correct
48 Correct 26 ms 36480 KB Output is correct
49 Correct 26 ms 36608 KB Output is correct
50 Correct 27 ms 36608 KB Output is correct
51 Correct 28 ms 36604 KB Output is correct
52 Correct 26 ms 36472 KB Output is correct
53 Correct 26 ms 36480 KB Output is correct
54 Correct 27 ms 36608 KB Output is correct
55 Correct 23 ms 35712 KB Output is correct
56 Correct 134 ms 63968 KB Output is correct
57 Correct 235 ms 66660 KB Output is correct
58 Correct 26 ms 36480 KB Output is correct
59 Correct 23 ms 35584 KB Output is correct
60 Correct 23 ms 35576 KB Output is correct
61 Correct 164 ms 63808 KB Output is correct
62 Correct 26 ms 36608 KB Output is correct
63 Correct 1215 ms 116400 KB Output is correct
64 Correct 240 ms 65892 KB Output is correct
65 Correct 26 ms 36472 KB Output is correct
66 Correct 202 ms 63552 KB Output is correct
67 Correct 26 ms 36472 KB Output is correct
68 Correct 26 ms 36480 KB Output is correct
69 Correct 145 ms 63852 KB Output is correct
70 Correct 26 ms 36608 KB Output is correct
71 Correct 153 ms 63640 KB Output is correct
72 Correct 771 ms 88932 KB Output is correct
73 Correct 24 ms 35824 KB Output is correct
74 Correct 205 ms 67684 KB Output is correct
75 Correct 750 ms 90596 KB Output is correct
76 Correct 27 ms 36480 KB Output is correct
77 Correct 28 ms 36592 KB Output is correct
78 Correct 135 ms 63840 KB Output is correct
79 Correct 170 ms 67024 KB Output is correct
80 Correct 27 ms 36480 KB Output is correct
81 Correct 26 ms 36420 KB Output is correct
82 Correct 24 ms 35584 KB Output is correct
83 Correct 23 ms 35584 KB Output is correct
84 Correct 26 ms 36480 KB Output is correct
85 Correct 264 ms 67336 KB Output is correct
86 Correct 27 ms 36480 KB Output is correct
87 Correct 167 ms 63848 KB Output is correct
88 Correct 23 ms 35576 KB Output is correct
89 Correct 194 ms 63376 KB Output is correct
90 Correct 208 ms 65636 KB Output is correct
91 Correct 198 ms 66788 KB Output is correct
92 Correct 141 ms 63988 KB Output is correct
93 Correct 27 ms 36496 KB Output is correct
94 Correct 23 ms 35584 KB Output is correct
95 Correct 174 ms 67556 KB Output is correct
96 Correct 250 ms 68708 KB Output is correct
97 Correct 27 ms 36480 KB Output is correct
98 Correct 23 ms 35584 KB Output is correct
99 Correct 164 ms 66012 KB Output is correct
100 Correct 27 ms 36472 KB Output is correct
101 Correct 187 ms 64100 KB Output is correct
102 Correct 243 ms 68580 KB Output is correct
103 Correct 27 ms 36600 KB Output is correct
104 Correct 27 ms 36600 KB Output is correct
105 Correct 26 ms 36480 KB Output is correct
106 Correct 26 ms 36480 KB Output is correct
107 Correct 29 ms 36472 KB Output is correct
108 Correct 947 ms 196060 KB Output is correct
109 Correct 1451 ms 191568 KB Output is correct
110 Correct 1257 ms 198096 KB Output is correct
111 Correct 1942 ms 216344 KB Output is correct
112 Correct 2219 ms 220512 KB Output is correct
113 Correct 1154 ms 205008 KB Output is correct
114 Correct 959 ms 183504 KB Output is correct
115 Correct 933 ms 183248 KB Output is correct
116 Correct 2153 ms 219796 KB Output is correct
117 Correct 1402 ms 201424 KB Output is correct
118 Correct 981 ms 183120 KB Output is correct
119 Correct 1395 ms 201424 KB Output is correct
120 Correct 1887 ms 211288 KB Output is correct
121 Correct 1419 ms 204464 KB Output is correct
122 Correct 2245 ms 233044 KB Output is correct
123 Correct 1048 ms 202304 KB Output is correct
124 Correct 1763 ms 199168 KB Output is correct