Submission #287521

# Submission time Handle Problem Language Result Execution time Memory
287521 2020-08-31T18:39:06 Z YJU Mergers (JOI19_mergers) C++14
70 / 100
1245 ms 96668 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=2e5+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 10 ms 14464 KB Output is correct
2 Correct 10 ms 14464 KB Output is correct
3 Correct 10 ms 14592 KB Output is correct
4 Correct 11 ms 14464 KB Output is correct
5 Correct 10 ms 14464 KB Output is correct
6 Correct 10 ms 14464 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 10 ms 14464 KB Output is correct
9 Correct 10 ms 14496 KB Output is correct
10 Correct 9 ms 14464 KB Output is correct
11 Correct 10 ms 14464 KB Output is correct
12 Correct 10 ms 14464 KB Output is correct
13 Correct 10 ms 14464 KB Output is correct
14 Correct 9 ms 14464 KB Output is correct
15 Correct 10 ms 14464 KB Output is correct
16 Correct 10 ms 14464 KB Output is correct
17 Correct 9 ms 14464 KB Output is correct
18 Correct 10 ms 14464 KB Output is correct
19 Correct 10 ms 14464 KB Output is correct
20 Correct 10 ms 14464 KB Output is correct
21 Correct 10 ms 14464 KB Output is correct
22 Correct 10 ms 14496 KB Output is correct
23 Correct 10 ms 14464 KB Output is correct
24 Correct 9 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14464 KB Output is correct
2 Correct 10 ms 14464 KB Output is correct
3 Correct 10 ms 14592 KB Output is correct
4 Correct 11 ms 14464 KB Output is correct
5 Correct 10 ms 14464 KB Output is correct
6 Correct 10 ms 14464 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 10 ms 14464 KB Output is correct
9 Correct 10 ms 14496 KB Output is correct
10 Correct 9 ms 14464 KB Output is correct
11 Correct 10 ms 14464 KB Output is correct
12 Correct 10 ms 14464 KB Output is correct
13 Correct 10 ms 14464 KB Output is correct
14 Correct 9 ms 14464 KB Output is correct
15 Correct 10 ms 14464 KB Output is correct
16 Correct 10 ms 14464 KB Output is correct
17 Correct 9 ms 14464 KB Output is correct
18 Correct 10 ms 14464 KB Output is correct
19 Correct 10 ms 14464 KB Output is correct
20 Correct 10 ms 14464 KB Output is correct
21 Correct 10 ms 14464 KB Output is correct
22 Correct 10 ms 14496 KB Output is correct
23 Correct 10 ms 14464 KB Output is correct
24 Correct 9 ms 14464 KB Output is correct
25 Correct 9 ms 14464 KB Output is correct
26 Correct 12 ms 15360 KB Output is correct
27 Correct 12 ms 15360 KB Output is correct
28 Correct 13 ms 15488 KB Output is correct
29 Correct 12 ms 15488 KB Output is correct
30 Correct 14 ms 15616 KB Output is correct
31 Correct 9 ms 14464 KB Output is correct
32 Correct 14 ms 15616 KB Output is correct
33 Correct 10 ms 14464 KB Output is correct
34 Correct 12 ms 15360 KB Output is correct
35 Correct 13 ms 15488 KB Output is correct
36 Correct 12 ms 15360 KB Output is correct
37 Correct 13 ms 15488 KB Output is correct
38 Correct 12 ms 14592 KB Output is correct
39 Correct 14 ms 15616 KB Output is correct
40 Correct 12 ms 15360 KB Output is correct
41 Correct 12 ms 15360 KB Output is correct
42 Correct 12 ms 15488 KB Output is correct
43 Correct 12 ms 15488 KB Output is correct
44 Correct 9 ms 14464 KB Output is correct
45 Correct 14 ms 15616 KB Output is correct
46 Correct 13 ms 15488 KB Output is correct
47 Correct 9 ms 14464 KB Output is correct
48 Correct 13 ms 15360 KB Output is correct
49 Correct 13 ms 15488 KB Output is correct
50 Correct 13 ms 15616 KB Output is correct
51 Correct 14 ms 15360 KB Output is correct
52 Correct 12 ms 15360 KB Output is correct
53 Correct 12 ms 15384 KB Output is correct
54 Correct 13 ms 15488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14464 KB Output is correct
2 Correct 10 ms 14464 KB Output is correct
3 Correct 10 ms 14592 KB Output is correct
4 Correct 11 ms 14464 KB Output is correct
5 Correct 10 ms 14464 KB Output is correct
6 Correct 10 ms 14464 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 10 ms 14464 KB Output is correct
9 Correct 10 ms 14496 KB Output is correct
10 Correct 9 ms 14464 KB Output is correct
11 Correct 10 ms 14464 KB Output is correct
12 Correct 10 ms 14464 KB Output is correct
13 Correct 10 ms 14464 KB Output is correct
14 Correct 9 ms 14464 KB Output is correct
15 Correct 10 ms 14464 KB Output is correct
16 Correct 10 ms 14464 KB Output is correct
17 Correct 9 ms 14464 KB Output is correct
18 Correct 10 ms 14464 KB Output is correct
19 Correct 10 ms 14464 KB Output is correct
20 Correct 10 ms 14464 KB Output is correct
21 Correct 10 ms 14464 KB Output is correct
22 Correct 10 ms 14496 KB Output is correct
23 Correct 10 ms 14464 KB Output is correct
24 Correct 9 ms 14464 KB Output is correct
25 Correct 10 ms 14592 KB Output is correct
26 Correct 126 ms 44256 KB Output is correct
27 Correct 242 ms 46868 KB Output is correct
28 Correct 13 ms 15360 KB Output is correct
29 Correct 10 ms 14464 KB Output is correct
30 Correct 10 ms 14464 KB Output is correct
31 Correct 158 ms 44016 KB Output is correct
32 Correct 13 ms 15360 KB Output is correct
33 Correct 1245 ms 96668 KB Output is correct
34 Correct 221 ms 46188 KB Output is correct
35 Correct 12 ms 15360 KB Output is correct
36 Correct 192 ms 43876 KB Output is correct
37 Correct 13 ms 15360 KB Output is correct
38 Correct 12 ms 15360 KB Output is correct
39 Correct 129 ms 44132 KB Output is correct
40 Correct 13 ms 15488 KB Output is correct
41 Correct 142 ms 43876 KB Output is correct
42 Correct 743 ms 69168 KB Output is correct
43 Correct 10 ms 14464 KB Output is correct
44 Correct 193 ms 47844 KB Output is correct
45 Correct 736 ms 70884 KB Output is correct
46 Correct 12 ms 15360 KB Output is correct
47 Correct 14 ms 15488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 128 ms 42716 KB Output is correct
2 Correct 156 ms 45968 KB Output is correct
3 Correct 13 ms 15360 KB Output is correct
4 Correct 13 ms 15360 KB Output is correct
5 Correct 10 ms 14464 KB Output is correct
6 Correct 10 ms 14464 KB Output is correct
7 Correct 12 ms 15360 KB Output is correct
8 Correct 252 ms 47972 KB Output is correct
9 Correct 13 ms 15360 KB Output is correct
10 Correct 160 ms 44388 KB Output is correct
11 Correct 10 ms 14464 KB Output is correct
12 Correct 177 ms 43748 KB Output is correct
13 Correct 213 ms 46180 KB Output is correct
14 Correct 194 ms 47364 KB Output is correct
15 Correct 129 ms 44136 KB Output is correct
16 Correct 14 ms 15488 KB Output is correct
17 Correct 10 ms 14464 KB Output is correct
18 Correct 161 ms 48100 KB Output is correct
19 Correct 235 ms 49256 KB Output is correct
20 Correct 14 ms 15488 KB Output is correct
21 Correct 10 ms 14464 KB Output is correct
22 Correct 160 ms 46720 KB Output is correct
23 Correct 13 ms 15360 KB Output is correct
24 Correct 175 ms 44520 KB Output is correct
25 Correct 232 ms 49124 KB Output is correct
26 Correct 13 ms 15488 KB Output is correct
27 Correct 13 ms 15488 KB Output is correct
28 Correct 13 ms 15360 KB Output is correct
29 Correct 12 ms 15360 KB Output is correct
30 Correct 13 ms 15488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14464 KB Output is correct
2 Correct 10 ms 14464 KB Output is correct
3 Correct 10 ms 14592 KB Output is correct
4 Correct 11 ms 14464 KB Output is correct
5 Correct 10 ms 14464 KB Output is correct
6 Correct 10 ms 14464 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 10 ms 14464 KB Output is correct
9 Correct 10 ms 14496 KB Output is correct
10 Correct 9 ms 14464 KB Output is correct
11 Correct 10 ms 14464 KB Output is correct
12 Correct 10 ms 14464 KB Output is correct
13 Correct 10 ms 14464 KB Output is correct
14 Correct 9 ms 14464 KB Output is correct
15 Correct 10 ms 14464 KB Output is correct
16 Correct 10 ms 14464 KB Output is correct
17 Correct 9 ms 14464 KB Output is correct
18 Correct 10 ms 14464 KB Output is correct
19 Correct 10 ms 14464 KB Output is correct
20 Correct 10 ms 14464 KB Output is correct
21 Correct 10 ms 14464 KB Output is correct
22 Correct 10 ms 14496 KB Output is correct
23 Correct 10 ms 14464 KB Output is correct
24 Correct 9 ms 14464 KB Output is correct
25 Correct 9 ms 14464 KB Output is correct
26 Correct 12 ms 15360 KB Output is correct
27 Correct 12 ms 15360 KB Output is correct
28 Correct 13 ms 15488 KB Output is correct
29 Correct 12 ms 15488 KB Output is correct
30 Correct 14 ms 15616 KB Output is correct
31 Correct 9 ms 14464 KB Output is correct
32 Correct 14 ms 15616 KB Output is correct
33 Correct 10 ms 14464 KB Output is correct
34 Correct 12 ms 15360 KB Output is correct
35 Correct 13 ms 15488 KB Output is correct
36 Correct 12 ms 15360 KB Output is correct
37 Correct 13 ms 15488 KB Output is correct
38 Correct 12 ms 14592 KB Output is correct
39 Correct 14 ms 15616 KB Output is correct
40 Correct 12 ms 15360 KB Output is correct
41 Correct 12 ms 15360 KB Output is correct
42 Correct 12 ms 15488 KB Output is correct
43 Correct 12 ms 15488 KB Output is correct
44 Correct 9 ms 14464 KB Output is correct
45 Correct 14 ms 15616 KB Output is correct
46 Correct 13 ms 15488 KB Output is correct
47 Correct 9 ms 14464 KB Output is correct
48 Correct 13 ms 15360 KB Output is correct
49 Correct 13 ms 15488 KB Output is correct
50 Correct 13 ms 15616 KB Output is correct
51 Correct 14 ms 15360 KB Output is correct
52 Correct 12 ms 15360 KB Output is correct
53 Correct 12 ms 15384 KB Output is correct
54 Correct 13 ms 15488 KB Output is correct
55 Correct 10 ms 14592 KB Output is correct
56 Correct 126 ms 44256 KB Output is correct
57 Correct 242 ms 46868 KB Output is correct
58 Correct 13 ms 15360 KB Output is correct
59 Correct 10 ms 14464 KB Output is correct
60 Correct 10 ms 14464 KB Output is correct
61 Correct 158 ms 44016 KB Output is correct
62 Correct 13 ms 15360 KB Output is correct
63 Correct 1245 ms 96668 KB Output is correct
64 Correct 221 ms 46188 KB Output is correct
65 Correct 12 ms 15360 KB Output is correct
66 Correct 192 ms 43876 KB Output is correct
67 Correct 13 ms 15360 KB Output is correct
68 Correct 12 ms 15360 KB Output is correct
69 Correct 129 ms 44132 KB Output is correct
70 Correct 13 ms 15488 KB Output is correct
71 Correct 142 ms 43876 KB Output is correct
72 Correct 743 ms 69168 KB Output is correct
73 Correct 10 ms 14464 KB Output is correct
74 Correct 193 ms 47844 KB Output is correct
75 Correct 736 ms 70884 KB Output is correct
76 Correct 12 ms 15360 KB Output is correct
77 Correct 14 ms 15488 KB Output is correct
78 Correct 128 ms 42716 KB Output is correct
79 Correct 156 ms 45968 KB Output is correct
80 Correct 13 ms 15360 KB Output is correct
81 Correct 13 ms 15360 KB Output is correct
82 Correct 10 ms 14464 KB Output is correct
83 Correct 10 ms 14464 KB Output is correct
84 Correct 12 ms 15360 KB Output is correct
85 Correct 252 ms 47972 KB Output is correct
86 Correct 13 ms 15360 KB Output is correct
87 Correct 160 ms 44388 KB Output is correct
88 Correct 10 ms 14464 KB Output is correct
89 Correct 177 ms 43748 KB Output is correct
90 Correct 213 ms 46180 KB Output is correct
91 Correct 194 ms 47364 KB Output is correct
92 Correct 129 ms 44136 KB Output is correct
93 Correct 14 ms 15488 KB Output is correct
94 Correct 10 ms 14464 KB Output is correct
95 Correct 161 ms 48100 KB Output is correct
96 Correct 235 ms 49256 KB Output is correct
97 Correct 14 ms 15488 KB Output is correct
98 Correct 10 ms 14464 KB Output is correct
99 Correct 160 ms 46720 KB Output is correct
100 Correct 13 ms 15360 KB Output is correct
101 Correct 175 ms 44520 KB Output is correct
102 Correct 232 ms 49124 KB Output is correct
103 Correct 13 ms 15488 KB Output is correct
104 Correct 13 ms 15488 KB Output is correct
105 Correct 13 ms 15360 KB Output is correct
106 Correct 12 ms 15360 KB Output is correct
107 Correct 13 ms 15488 KB Output is correct
108 Runtime error 53 ms 48380 KB Execution killed with signal 11
109 Halted 0 ms 0 KB -