답안 #164414

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
164414 2019-11-20T13:39:07 Z tneluccus Mergers (JOI19_mergers) C++14
100 / 100
1880 ms 136932 KB
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+2;
const int LOG=20;
int color[N],tin[N],tout[N],now=0,level[N],ancestor[N][LOG],head[N],cnt[N],deg[N];
vector<int> adj[N],lis[N];
int findd(int x){
	if(head[x]<0){
		return x;
	}
	int y=findd(head[x]);
	head[x]=y;
	return y;
}
void unionn(int x,int y){
	x=findd(x),y=findd(y);
	head[x]+=head[y];
	head[y]=x;
}
bool samee(int x,int y){
	return findd(x)==findd(y);
}
void dfs(int x,int p){
	now++;
	tin[x]=now;
	for(int i=1;i<LOG;i++){
		ancestor[x][i]=ancestor[ancestor[x][i-1]][i-1];
	}
	for(int i:adj[x]){
		if(i!=p){
			level[i]=level[x]+1;
			ancestor[i][0]=x;
			dfs(i,x);
		}
	}
	tout[x]=now;
}
int fin(int x,int y){
	while(y){
		int k=__lg(y);
		x=ancestor[x][k];
		y-=(1<<k);
	}
	return x;
}
int LCA(int x,int y){
	if(level[x]>level[y]){
		x=fin(x,level[x]-level[y]);
	}
	y=fin(y,level[y]-level[x]);
	if(x==y){
		return x;
	}
	for(int i=LOG-1;i>-1;i--){
		if(ancestor[x][i]!=ancestor[y][i]){
			x=ancestor[x][i],y=ancestor[y][i];
		}
	}
	return ancestor[x][0];
}
bool cmp(int x,int y){
	return tin[x]<tin[y];
}
void dfs1(int x,int p){
	for(int i:adj[x]){
		if(i!=p){
			dfs1(i,x);
			cnt[x]+=cnt[i];
		}
	}
	if(cnt[x]&&x!=p){
		unionn(x,p);
	}
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,m,i,j,k,l,num,ans=0;
	cin>>n>>num;
	for(i=1;i<n;i++){
		cin>>j>>k;
		adj[j].push_back(k);
		adj[k].push_back(j);
	}
	for(i=1;i<=n;i++){
		head[i]=-1;
		cin>>color[i];
		lis[color[i]].push_back(i);
	}
	dfs(1,1);
	for(i=1;i<=num;i++){
		sort(lis[i].begin(),lis[i].end(),cmp);
		int sz=lis[i].size();
		for(j=1;j<sz;j++){
			k=LCA(lis[i][j-1],lis[i][j]);
			lis[i].push_back(k);
		}
		sort(lis[i].begin(),lis[i].end(),cmp);
		lis[i].erase(unique(lis[i].begin(),lis[i].end()),lis[i].end());
		for(j=1;j<lis[i].size();j++){
			k=LCA(lis[i][j-1],lis[i][j]);
			cnt[lis[i][j]]++;
			cnt[k]--;
		}
	}
	dfs1(1,1);
	for(i=1;i<=n;i++){
		for(j=0;j<adj[i].size();j++){
			if(findd(i)!=findd(adj[i][j])){
				deg[findd(i)]++;
				deg[findd(adj[i][j])]++;
			}
		}
	}
	for(i=1;i<=n;i++){
		if(findd(i)==i){
			if(deg[i]==2){
				ans++;
			}
		}
	}
	cout<<(ans+1)/2;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:100:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(j=1;j<lis[i].size();j++){
           ~^~~~~~~~~~~~~~
mergers.cpp:108:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(j=0;j<adj[i].size();j++){
           ~^~~~~~~~~~~~~~
mergers.cpp:78:8: warning: unused variable 'm' [-Wunused-variable]
  int n,m,i,j,k,l,num,ans=0;
        ^
mergers.cpp:78:16: warning: unused variable 'l' [-Wunused-variable]
  int n,m,i,j,k,l,num,ans=0;
                ^
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 23928 KB Output is correct
2 Correct 26 ms 23924 KB Output is correct
3 Correct 56 ms 23916 KB Output is correct
4 Correct 25 ms 23928 KB Output is correct
5 Correct 25 ms 23840 KB Output is correct
6 Correct 26 ms 23928 KB Output is correct
7 Correct 25 ms 23932 KB Output is correct
8 Correct 26 ms 23932 KB Output is correct
9 Correct 25 ms 24056 KB Output is correct
10 Correct 25 ms 23932 KB Output is correct
11 Correct 25 ms 23928 KB Output is correct
12 Correct 24 ms 23928 KB Output is correct
13 Correct 25 ms 23932 KB Output is correct
14 Correct 25 ms 23928 KB Output is correct
15 Correct 26 ms 23928 KB Output is correct
16 Correct 25 ms 23928 KB Output is correct
17 Correct 26 ms 23928 KB Output is correct
18 Correct 25 ms 23928 KB Output is correct
19 Correct 25 ms 23928 KB Output is correct
20 Correct 25 ms 23928 KB Output is correct
21 Correct 26 ms 23944 KB Output is correct
22 Correct 25 ms 23900 KB Output is correct
23 Correct 27 ms 23928 KB Output is correct
24 Correct 26 ms 23928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 23928 KB Output is correct
2 Correct 26 ms 23924 KB Output is correct
3 Correct 56 ms 23916 KB Output is correct
4 Correct 25 ms 23928 KB Output is correct
5 Correct 25 ms 23840 KB Output is correct
6 Correct 26 ms 23928 KB Output is correct
7 Correct 25 ms 23932 KB Output is correct
8 Correct 26 ms 23932 KB Output is correct
9 Correct 25 ms 24056 KB Output is correct
10 Correct 25 ms 23932 KB Output is correct
11 Correct 25 ms 23928 KB Output is correct
12 Correct 24 ms 23928 KB Output is correct
13 Correct 25 ms 23932 KB Output is correct
14 Correct 25 ms 23928 KB Output is correct
15 Correct 26 ms 23928 KB Output is correct
16 Correct 25 ms 23928 KB Output is correct
17 Correct 26 ms 23928 KB Output is correct
18 Correct 25 ms 23928 KB Output is correct
19 Correct 25 ms 23928 KB Output is correct
20 Correct 25 ms 23928 KB Output is correct
21 Correct 26 ms 23944 KB Output is correct
22 Correct 25 ms 23900 KB Output is correct
23 Correct 27 ms 23928 KB Output is correct
24 Correct 26 ms 23928 KB Output is correct
25 Correct 26 ms 23912 KB Output is correct
26 Correct 29 ms 24440 KB Output is correct
27 Correct 43 ms 24312 KB Output is correct
28 Correct 29 ms 24560 KB Output is correct
29 Correct 29 ms 24440 KB Output is correct
30 Correct 29 ms 24440 KB Output is correct
31 Correct 26 ms 23928 KB Output is correct
32 Correct 29 ms 24568 KB Output is correct
33 Correct 26 ms 23928 KB Output is correct
34 Correct 28 ms 24440 KB Output is correct
35 Correct 28 ms 24440 KB Output is correct
36 Correct 30 ms 24440 KB Output is correct
37 Correct 29 ms 24440 KB Output is correct
38 Correct 25 ms 23928 KB Output is correct
39 Correct 29 ms 24440 KB Output is correct
40 Correct 29 ms 24412 KB Output is correct
41 Correct 29 ms 24440 KB Output is correct
42 Correct 29 ms 24440 KB Output is correct
43 Correct 37 ms 24568 KB Output is correct
44 Correct 26 ms 23928 KB Output is correct
45 Correct 29 ms 24516 KB Output is correct
46 Correct 29 ms 24440 KB Output is correct
47 Correct 25 ms 23928 KB Output is correct
48 Correct 27 ms 24380 KB Output is correct
49 Correct 28 ms 24440 KB Output is correct
50 Correct 30 ms 24444 KB Output is correct
51 Correct 29 ms 24440 KB Output is correct
52 Correct 28 ms 24440 KB Output is correct
53 Correct 28 ms 24360 KB Output is correct
54 Correct 30 ms 24312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 23928 KB Output is correct
2 Correct 26 ms 23924 KB Output is correct
3 Correct 56 ms 23916 KB Output is correct
4 Correct 25 ms 23928 KB Output is correct
5 Correct 25 ms 23840 KB Output is correct
6 Correct 26 ms 23928 KB Output is correct
7 Correct 25 ms 23932 KB Output is correct
8 Correct 26 ms 23932 KB Output is correct
9 Correct 25 ms 24056 KB Output is correct
10 Correct 25 ms 23932 KB Output is correct
11 Correct 25 ms 23928 KB Output is correct
12 Correct 24 ms 23928 KB Output is correct
13 Correct 25 ms 23932 KB Output is correct
14 Correct 25 ms 23928 KB Output is correct
15 Correct 26 ms 23928 KB Output is correct
16 Correct 25 ms 23928 KB Output is correct
17 Correct 26 ms 23928 KB Output is correct
18 Correct 25 ms 23928 KB Output is correct
19 Correct 25 ms 23928 KB Output is correct
20 Correct 25 ms 23928 KB Output is correct
21 Correct 26 ms 23944 KB Output is correct
22 Correct 25 ms 23900 KB Output is correct
23 Correct 27 ms 23928 KB Output is correct
24 Correct 26 ms 23928 KB Output is correct
25 Correct 27 ms 23928 KB Output is correct
26 Correct 181 ms 40476 KB Output is correct
27 Correct 222 ms 39672 KB Output is correct
28 Correct 28 ms 24356 KB Output is correct
29 Correct 25 ms 23928 KB Output is correct
30 Correct 26 ms 23936 KB Output is correct
31 Correct 218 ms 39800 KB Output is correct
32 Correct 29 ms 24356 KB Output is correct
33 Correct 255 ms 45468 KB Output is correct
34 Correct 238 ms 39672 KB Output is correct
35 Correct 29 ms 24440 KB Output is correct
36 Correct 230 ms 40440 KB Output is correct
37 Correct 30 ms 24440 KB Output is correct
38 Correct 29 ms 24312 KB Output is correct
39 Correct 231 ms 40560 KB Output is correct
40 Correct 30 ms 24568 KB Output is correct
41 Correct 240 ms 39588 KB Output is correct
42 Correct 238 ms 41600 KB Output is correct
43 Correct 26 ms 23928 KB Output is correct
44 Correct 269 ms 45544 KB Output is correct
45 Correct 253 ms 42680 KB Output is correct
46 Correct 29 ms 24440 KB Output is correct
47 Correct 29 ms 24316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 166 ms 40432 KB Output is correct
2 Correct 136 ms 42480 KB Output is correct
3 Correct 28 ms 24440 KB Output is correct
4 Correct 28 ms 24312 KB Output is correct
5 Correct 25 ms 23928 KB Output is correct
6 Correct 25 ms 23928 KB Output is correct
7 Correct 28 ms 24440 KB Output is correct
8 Correct 289 ms 41520 KB Output is correct
9 Correct 28 ms 24440 KB Output is correct
10 Correct 197 ms 40440 KB Output is correct
11 Correct 63 ms 23928 KB Output is correct
12 Correct 186 ms 40196 KB Output is correct
13 Correct 207 ms 41428 KB Output is correct
14 Correct 180 ms 42752 KB Output is correct
15 Correct 172 ms 40688 KB Output is correct
16 Correct 30 ms 24440 KB Output is correct
17 Correct 25 ms 23928 KB Output is correct
18 Correct 153 ms 42732 KB Output is correct
19 Correct 198 ms 45816 KB Output is correct
20 Correct 29 ms 24436 KB Output is correct
21 Correct 25 ms 23928 KB Output is correct
22 Correct 191 ms 42828 KB Output is correct
23 Correct 28 ms 24440 KB Output is correct
24 Correct 209 ms 40824 KB Output is correct
25 Correct 186 ms 44780 KB Output is correct
26 Correct 28 ms 24440 KB Output is correct
27 Correct 29 ms 24440 KB Output is correct
28 Correct 29 ms 24432 KB Output is correct
29 Correct 30 ms 24440 KB Output is correct
30 Correct 28 ms 24440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 23928 KB Output is correct
2 Correct 26 ms 23924 KB Output is correct
3 Correct 56 ms 23916 KB Output is correct
4 Correct 25 ms 23928 KB Output is correct
5 Correct 25 ms 23840 KB Output is correct
6 Correct 26 ms 23928 KB Output is correct
7 Correct 25 ms 23932 KB Output is correct
8 Correct 26 ms 23932 KB Output is correct
9 Correct 25 ms 24056 KB Output is correct
10 Correct 25 ms 23932 KB Output is correct
11 Correct 25 ms 23928 KB Output is correct
12 Correct 24 ms 23928 KB Output is correct
13 Correct 25 ms 23932 KB Output is correct
14 Correct 25 ms 23928 KB Output is correct
15 Correct 26 ms 23928 KB Output is correct
16 Correct 25 ms 23928 KB Output is correct
17 Correct 26 ms 23928 KB Output is correct
18 Correct 25 ms 23928 KB Output is correct
19 Correct 25 ms 23928 KB Output is correct
20 Correct 25 ms 23928 KB Output is correct
21 Correct 26 ms 23944 KB Output is correct
22 Correct 25 ms 23900 KB Output is correct
23 Correct 27 ms 23928 KB Output is correct
24 Correct 26 ms 23928 KB Output is correct
25 Correct 26 ms 23912 KB Output is correct
26 Correct 29 ms 24440 KB Output is correct
27 Correct 43 ms 24312 KB Output is correct
28 Correct 29 ms 24560 KB Output is correct
29 Correct 29 ms 24440 KB Output is correct
30 Correct 29 ms 24440 KB Output is correct
31 Correct 26 ms 23928 KB Output is correct
32 Correct 29 ms 24568 KB Output is correct
33 Correct 26 ms 23928 KB Output is correct
34 Correct 28 ms 24440 KB Output is correct
35 Correct 28 ms 24440 KB Output is correct
36 Correct 30 ms 24440 KB Output is correct
37 Correct 29 ms 24440 KB Output is correct
38 Correct 25 ms 23928 KB Output is correct
39 Correct 29 ms 24440 KB Output is correct
40 Correct 29 ms 24412 KB Output is correct
41 Correct 29 ms 24440 KB Output is correct
42 Correct 29 ms 24440 KB Output is correct
43 Correct 37 ms 24568 KB Output is correct
44 Correct 26 ms 23928 KB Output is correct
45 Correct 29 ms 24516 KB Output is correct
46 Correct 29 ms 24440 KB Output is correct
47 Correct 25 ms 23928 KB Output is correct
48 Correct 27 ms 24380 KB Output is correct
49 Correct 28 ms 24440 KB Output is correct
50 Correct 30 ms 24444 KB Output is correct
51 Correct 29 ms 24440 KB Output is correct
52 Correct 28 ms 24440 KB Output is correct
53 Correct 28 ms 24360 KB Output is correct
54 Correct 30 ms 24312 KB Output is correct
55 Correct 27 ms 23928 KB Output is correct
56 Correct 181 ms 40476 KB Output is correct
57 Correct 222 ms 39672 KB Output is correct
58 Correct 28 ms 24356 KB Output is correct
59 Correct 25 ms 23928 KB Output is correct
60 Correct 26 ms 23936 KB Output is correct
61 Correct 218 ms 39800 KB Output is correct
62 Correct 29 ms 24356 KB Output is correct
63 Correct 255 ms 45468 KB Output is correct
64 Correct 238 ms 39672 KB Output is correct
65 Correct 29 ms 24440 KB Output is correct
66 Correct 230 ms 40440 KB Output is correct
67 Correct 30 ms 24440 KB Output is correct
68 Correct 29 ms 24312 KB Output is correct
69 Correct 231 ms 40560 KB Output is correct
70 Correct 30 ms 24568 KB Output is correct
71 Correct 240 ms 39588 KB Output is correct
72 Correct 238 ms 41600 KB Output is correct
73 Correct 26 ms 23928 KB Output is correct
74 Correct 269 ms 45544 KB Output is correct
75 Correct 253 ms 42680 KB Output is correct
76 Correct 29 ms 24440 KB Output is correct
77 Correct 29 ms 24316 KB Output is correct
78 Correct 166 ms 40432 KB Output is correct
79 Correct 136 ms 42480 KB Output is correct
80 Correct 28 ms 24440 KB Output is correct
81 Correct 28 ms 24312 KB Output is correct
82 Correct 25 ms 23928 KB Output is correct
83 Correct 25 ms 23928 KB Output is correct
84 Correct 28 ms 24440 KB Output is correct
85 Correct 289 ms 41520 KB Output is correct
86 Correct 28 ms 24440 KB Output is correct
87 Correct 197 ms 40440 KB Output is correct
88 Correct 63 ms 23928 KB Output is correct
89 Correct 186 ms 40196 KB Output is correct
90 Correct 207 ms 41428 KB Output is correct
91 Correct 180 ms 42752 KB Output is correct
92 Correct 172 ms 40688 KB Output is correct
93 Correct 30 ms 24440 KB Output is correct
94 Correct 25 ms 23928 KB Output is correct
95 Correct 153 ms 42732 KB Output is correct
96 Correct 198 ms 45816 KB Output is correct
97 Correct 29 ms 24436 KB Output is correct
98 Correct 25 ms 23928 KB Output is correct
99 Correct 191 ms 42828 KB Output is correct
100 Correct 28 ms 24440 KB Output is correct
101 Correct 209 ms 40824 KB Output is correct
102 Correct 186 ms 44780 KB Output is correct
103 Correct 28 ms 24440 KB Output is correct
104 Correct 29 ms 24440 KB Output is correct
105 Correct 29 ms 24432 KB Output is correct
106 Correct 30 ms 24440 KB Output is correct
107 Correct 28 ms 24440 KB Output is correct
108 Correct 1179 ms 116836 KB Output is correct
109 Correct 1824 ms 116940 KB Output is correct
110 Correct 1880 ms 125972 KB Output is correct
111 Correct 1408 ms 136932 KB Output is correct
112 Correct 1440 ms 129304 KB Output is correct
113 Correct 969 ms 119336 KB Output is correct
114 Correct 1568 ms 102800 KB Output is correct
115 Correct 1645 ms 102604 KB Output is correct
116 Correct 1446 ms 109732 KB Output is correct
117 Correct 1214 ms 118496 KB Output is correct
118 Correct 1299 ms 105496 KB Output is correct
119 Correct 1140 ms 118492 KB Output is correct
120 Correct 1315 ms 129528 KB Output is correct
121 Correct 1169 ms 118564 KB Output is correct
122 Correct 1352 ms 113984 KB Output is correct
123 Correct 847 ms 118092 KB Output is correct
124 Correct 1185 ms 116432 KB Output is correct