# |
제출 시각 |
아이디 |
문제 |
언어 |
결과 |
실행 시간 |
메모리 |
120637 |
2019-06-25T06:41:44 Z |
임유진(#2961) |
Mergers (JOI19_mergers) |
C++14 |
|
1433 ms |
145784 KB |
#include<stdio.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
#define MAXN 500005
int A[MAXN], B[MAXN], S[MAXN];
vector<int> e[MAXN];
int par[20][MAXN], dep[MAXN];
vector<int> ss[MAXN];
bool chkn[MAXN], chks[MAXN];
queue<int> q;
vector<int> ge[MAXN];
int r[MAXN];
void dfs(int x){
for(auto a:e[x]) if(a!=par[0][x]){
par[0][a]=x;
dep[a]=dep[x]+1;
dfs(a);
}
}
int anc(int x, int d){
if(d>dep[x]) return 0;
for(int i=19; i>=0; i--) if((dep[x]-d)&(1<<i)) x=par[i][x];
return x;
}
int lca(int x, int y){
if(dep[x]>dep[y]) x=anc(x, dep[y]);
else y=anc(y, dep[x]);
if(x==y) return x;
for(int i=19; i>=0; i--) if(par[i][x]!=par[i][y]){
x=par[i][x];
y=par[i][y];
}
return par[0][x];
}
int uni(int x){
if(r[x]==x) return x;
return r[x]=uni(r[x]);
}
int main(){
int N, K;
int ans=0;
scanf("%d%d", &N, &K);
for(int i=0; i<N-1; i++) scanf("%d%d", A+i, B+i);
for(int i=1; i<=N; i++) scanf("%d", S+i);
for(int i=0; i<N-1; i++){
e[A[i]].push_back(B[i]);
e[B[i]].push_back(A[i]);
}
par[0][1]=1;
dfs(1);
for(int i=1; i<20; i++) for(int j=1; j<=N; j++) par[i][j]=par[i-1][par[i-1][j]];
//for(int i=1; i<=N; i++) printf("%d %d %d\n", par[0][i], par[1][i], par[2][i]);
for(int i=1; i<=N; i++) ss[S[i]].push_back(i);
//for(int i=1; i<=K; i++) printf("%d\n", ss[i].size());
for(int i=1; i<=N; i++) r[i]=i;
for(int i=1; i<=K; i++) if(!chks[i]){
chks[i]=true;
q.push(i);
//printf("[%d]\n", i);
while(!q.empty()){
int t=q.front();
q.pop();
int l=ss[t][0];
for(auto a:ss[t]) l=lca(l, a);
//printf("lca=%d\n", l);
for(auto a:ss[t]){
chkn[a]=true;
for(int j=uni(a); dep[j]>dep[l]; j=uni(j)){
chkn[par[0][j]]=true;
r[uni(j)]=uni(par[0][uni(j)]);
}
}
}
}
//for(int i=1; i<=K; i++) printf("%d ", gs[i]);
for(int i=0; i<N-1; i++) if(uni(A[i])!=uni(B[i])){
ge[uni(A[i])].push_back(uni(B[i]));
ge[uni(B[i])].push_back(uni(A[i]));
//printf("(%d %d)\n", S[A[i]], S[B[i]]);
}
for(int i=1; i<=N; i++){
sort(ge[i].begin(), ge[i].end());
if(distance(ge[i].begin(), unique(ge[i].begin(), ge[i].end()))==1) ans++;
}
//printf("\n%d\n", ans);
printf("%d", (ans+1)/2);
return 0;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:53:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &K);
~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:54:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=0; i<N-1; i++) scanf("%d%d", A+i, B+i);
~~~~~^~~~~~~~~~~~~~~~~~
mergers.cpp:55:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1; i<=N; i++) scanf("%d", S+i);
~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
35712 KB |
Output is correct |
2 |
Correct |
32 ms |
35712 KB |
Output is correct |
3 |
Correct |
32 ms |
35712 KB |
Output is correct |
4 |
Correct |
33 ms |
35684 KB |
Output is correct |
5 |
Correct |
32 ms |
35712 KB |
Output is correct |
6 |
Correct |
32 ms |
35704 KB |
Output is correct |
7 |
Correct |
32 ms |
35708 KB |
Output is correct |
8 |
Correct |
34 ms |
35684 KB |
Output is correct |
9 |
Correct |
33 ms |
35704 KB |
Output is correct |
10 |
Correct |
32 ms |
35808 KB |
Output is correct |
11 |
Correct |
30 ms |
35712 KB |
Output is correct |
12 |
Correct |
31 ms |
35684 KB |
Output is correct |
13 |
Correct |
33 ms |
35684 KB |
Output is correct |
14 |
Correct |
33 ms |
35712 KB |
Output is correct |
15 |
Correct |
32 ms |
35704 KB |
Output is correct |
16 |
Correct |
32 ms |
35772 KB |
Output is correct |
17 |
Correct |
32 ms |
35704 KB |
Output is correct |
18 |
Correct |
32 ms |
35704 KB |
Output is correct |
19 |
Correct |
31 ms |
35712 KB |
Output is correct |
20 |
Correct |
31 ms |
35768 KB |
Output is correct |
21 |
Correct |
31 ms |
35704 KB |
Output is correct |
22 |
Correct |
32 ms |
35712 KB |
Output is correct |
23 |
Correct |
31 ms |
35712 KB |
Output is correct |
24 |
Correct |
30 ms |
35712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
35712 KB |
Output is correct |
2 |
Correct |
32 ms |
35712 KB |
Output is correct |
3 |
Correct |
32 ms |
35712 KB |
Output is correct |
4 |
Correct |
33 ms |
35684 KB |
Output is correct |
5 |
Correct |
32 ms |
35712 KB |
Output is correct |
6 |
Correct |
32 ms |
35704 KB |
Output is correct |
7 |
Correct |
32 ms |
35708 KB |
Output is correct |
8 |
Correct |
34 ms |
35684 KB |
Output is correct |
9 |
Correct |
33 ms |
35704 KB |
Output is correct |
10 |
Correct |
32 ms |
35808 KB |
Output is correct |
11 |
Correct |
30 ms |
35712 KB |
Output is correct |
12 |
Correct |
31 ms |
35684 KB |
Output is correct |
13 |
Correct |
33 ms |
35684 KB |
Output is correct |
14 |
Correct |
33 ms |
35712 KB |
Output is correct |
15 |
Correct |
32 ms |
35704 KB |
Output is correct |
16 |
Correct |
32 ms |
35772 KB |
Output is correct |
17 |
Correct |
32 ms |
35704 KB |
Output is correct |
18 |
Correct |
32 ms |
35704 KB |
Output is correct |
19 |
Correct |
31 ms |
35712 KB |
Output is correct |
20 |
Correct |
31 ms |
35768 KB |
Output is correct |
21 |
Correct |
31 ms |
35704 KB |
Output is correct |
22 |
Correct |
32 ms |
35712 KB |
Output is correct |
23 |
Correct |
31 ms |
35712 KB |
Output is correct |
24 |
Correct |
30 ms |
35712 KB |
Output is correct |
25 |
Correct |
31 ms |
35704 KB |
Output is correct |
26 |
Correct |
33 ms |
36224 KB |
Output is correct |
27 |
Correct |
35 ms |
36120 KB |
Output is correct |
28 |
Correct |
36 ms |
36316 KB |
Output is correct |
29 |
Correct |
35 ms |
36304 KB |
Output is correct |
30 |
Correct |
39 ms |
36168 KB |
Output is correct |
31 |
Correct |
33 ms |
35712 KB |
Output is correct |
32 |
Correct |
34 ms |
36352 KB |
Output is correct |
33 |
Correct |
32 ms |
35816 KB |
Output is correct |
34 |
Correct |
33 ms |
36132 KB |
Output is correct |
35 |
Correct |
35 ms |
36312 KB |
Output is correct |
36 |
Correct |
35 ms |
36224 KB |
Output is correct |
37 |
Correct |
36 ms |
36268 KB |
Output is correct |
38 |
Correct |
33 ms |
35804 KB |
Output is correct |
39 |
Correct |
36 ms |
36196 KB |
Output is correct |
40 |
Correct |
35 ms |
36204 KB |
Output is correct |
41 |
Correct |
34 ms |
36224 KB |
Output is correct |
42 |
Correct |
35 ms |
36188 KB |
Output is correct |
43 |
Correct |
35 ms |
36344 KB |
Output is correct |
44 |
Correct |
32 ms |
35704 KB |
Output is correct |
45 |
Correct |
34 ms |
36216 KB |
Output is correct |
46 |
Correct |
35 ms |
36216 KB |
Output is correct |
47 |
Correct |
31 ms |
35712 KB |
Output is correct |
48 |
Correct |
36 ms |
36216 KB |
Output is correct |
49 |
Correct |
34 ms |
36344 KB |
Output is correct |
50 |
Correct |
36 ms |
36344 KB |
Output is correct |
51 |
Correct |
34 ms |
36224 KB |
Output is correct |
52 |
Correct |
34 ms |
36152 KB |
Output is correct |
53 |
Correct |
33 ms |
36216 KB |
Output is correct |
54 |
Correct |
34 ms |
36216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
35712 KB |
Output is correct |
2 |
Correct |
32 ms |
35712 KB |
Output is correct |
3 |
Correct |
32 ms |
35712 KB |
Output is correct |
4 |
Correct |
33 ms |
35684 KB |
Output is correct |
5 |
Correct |
32 ms |
35712 KB |
Output is correct |
6 |
Correct |
32 ms |
35704 KB |
Output is correct |
7 |
Correct |
32 ms |
35708 KB |
Output is correct |
8 |
Correct |
34 ms |
35684 KB |
Output is correct |
9 |
Correct |
33 ms |
35704 KB |
Output is correct |
10 |
Correct |
32 ms |
35808 KB |
Output is correct |
11 |
Correct |
30 ms |
35712 KB |
Output is correct |
12 |
Correct |
31 ms |
35684 KB |
Output is correct |
13 |
Correct |
33 ms |
35684 KB |
Output is correct |
14 |
Correct |
33 ms |
35712 KB |
Output is correct |
15 |
Correct |
32 ms |
35704 KB |
Output is correct |
16 |
Correct |
32 ms |
35772 KB |
Output is correct |
17 |
Correct |
32 ms |
35704 KB |
Output is correct |
18 |
Correct |
32 ms |
35704 KB |
Output is correct |
19 |
Correct |
31 ms |
35712 KB |
Output is correct |
20 |
Correct |
31 ms |
35768 KB |
Output is correct |
21 |
Correct |
31 ms |
35704 KB |
Output is correct |
22 |
Correct |
32 ms |
35712 KB |
Output is correct |
23 |
Correct |
31 ms |
35712 KB |
Output is correct |
24 |
Correct |
30 ms |
35712 KB |
Output is correct |
25 |
Correct |
32 ms |
35708 KB |
Output is correct |
26 |
Correct |
91 ms |
49904 KB |
Output is correct |
27 |
Correct |
122 ms |
49528 KB |
Output is correct |
28 |
Correct |
33 ms |
36144 KB |
Output is correct |
29 |
Correct |
31 ms |
35712 KB |
Output is correct |
30 |
Correct |
31 ms |
35712 KB |
Output is correct |
31 |
Correct |
118 ms |
49532 KB |
Output is correct |
32 |
Correct |
35 ms |
36216 KB |
Output is correct |
33 |
Correct |
170 ms |
54000 KB |
Output is correct |
34 |
Correct |
126 ms |
49528 KB |
Output is correct |
35 |
Correct |
34 ms |
36216 KB |
Output is correct |
36 |
Correct |
148 ms |
49784 KB |
Output is correct |
37 |
Correct |
36 ms |
36216 KB |
Output is correct |
38 |
Correct |
35 ms |
36224 KB |
Output is correct |
39 |
Correct |
97 ms |
49776 KB |
Output is correct |
40 |
Correct |
41 ms |
36344 KB |
Output is correct |
41 |
Correct |
115 ms |
49524 KB |
Output is correct |
42 |
Correct |
158 ms |
50780 KB |
Output is correct |
43 |
Correct |
31 ms |
35704 KB |
Output is correct |
44 |
Correct |
167 ms |
54084 KB |
Output is correct |
45 |
Correct |
138 ms |
51832 KB |
Output is correct |
46 |
Correct |
38 ms |
36224 KB |
Output is correct |
47 |
Correct |
33 ms |
36216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
49776 KB |
Output is correct |
2 |
Correct |
148 ms |
55916 KB |
Output is correct |
3 |
Correct |
34 ms |
36216 KB |
Output is correct |
4 |
Correct |
34 ms |
36216 KB |
Output is correct |
5 |
Correct |
31 ms |
35704 KB |
Output is correct |
6 |
Correct |
32 ms |
35712 KB |
Output is correct |
7 |
Correct |
35 ms |
36344 KB |
Output is correct |
8 |
Correct |
153 ms |
51064 KB |
Output is correct |
9 |
Correct |
35 ms |
36216 KB |
Output is correct |
10 |
Correct |
118 ms |
49656 KB |
Output is correct |
11 |
Correct |
34 ms |
35748 KB |
Output is correct |
12 |
Correct |
152 ms |
49696 KB |
Output is correct |
13 |
Correct |
164 ms |
51528 KB |
Output is correct |
14 |
Correct |
189 ms |
55672 KB |
Output is correct |
15 |
Correct |
99 ms |
50032 KB |
Output is correct |
16 |
Correct |
35 ms |
36272 KB |
Output is correct |
17 |
Correct |
32 ms |
35704 KB |
Output is correct |
18 |
Correct |
157 ms |
55248 KB |
Output is correct |
19 |
Correct |
204 ms |
57800 KB |
Output is correct |
20 |
Correct |
34 ms |
36216 KB |
Output is correct |
21 |
Correct |
31 ms |
35704 KB |
Output is correct |
22 |
Correct |
126 ms |
51756 KB |
Output is correct |
23 |
Correct |
34 ms |
36216 KB |
Output is correct |
24 |
Correct |
147 ms |
50296 KB |
Output is correct |
25 |
Correct |
181 ms |
57080 KB |
Output is correct |
26 |
Correct |
36 ms |
36344 KB |
Output is correct |
27 |
Correct |
35 ms |
36344 KB |
Output is correct |
28 |
Correct |
37 ms |
36260 KB |
Output is correct |
29 |
Correct |
36 ms |
36216 KB |
Output is correct |
30 |
Correct |
78 ms |
36216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
35712 KB |
Output is correct |
2 |
Correct |
32 ms |
35712 KB |
Output is correct |
3 |
Correct |
32 ms |
35712 KB |
Output is correct |
4 |
Correct |
33 ms |
35684 KB |
Output is correct |
5 |
Correct |
32 ms |
35712 KB |
Output is correct |
6 |
Correct |
32 ms |
35704 KB |
Output is correct |
7 |
Correct |
32 ms |
35708 KB |
Output is correct |
8 |
Correct |
34 ms |
35684 KB |
Output is correct |
9 |
Correct |
33 ms |
35704 KB |
Output is correct |
10 |
Correct |
32 ms |
35808 KB |
Output is correct |
11 |
Correct |
30 ms |
35712 KB |
Output is correct |
12 |
Correct |
31 ms |
35684 KB |
Output is correct |
13 |
Correct |
33 ms |
35684 KB |
Output is correct |
14 |
Correct |
33 ms |
35712 KB |
Output is correct |
15 |
Correct |
32 ms |
35704 KB |
Output is correct |
16 |
Correct |
32 ms |
35772 KB |
Output is correct |
17 |
Correct |
32 ms |
35704 KB |
Output is correct |
18 |
Correct |
32 ms |
35704 KB |
Output is correct |
19 |
Correct |
31 ms |
35712 KB |
Output is correct |
20 |
Correct |
31 ms |
35768 KB |
Output is correct |
21 |
Correct |
31 ms |
35704 KB |
Output is correct |
22 |
Correct |
32 ms |
35712 KB |
Output is correct |
23 |
Correct |
31 ms |
35712 KB |
Output is correct |
24 |
Correct |
30 ms |
35712 KB |
Output is correct |
25 |
Correct |
31 ms |
35704 KB |
Output is correct |
26 |
Correct |
33 ms |
36224 KB |
Output is correct |
27 |
Correct |
35 ms |
36120 KB |
Output is correct |
28 |
Correct |
36 ms |
36316 KB |
Output is correct |
29 |
Correct |
35 ms |
36304 KB |
Output is correct |
30 |
Correct |
39 ms |
36168 KB |
Output is correct |
31 |
Correct |
33 ms |
35712 KB |
Output is correct |
32 |
Correct |
34 ms |
36352 KB |
Output is correct |
33 |
Correct |
32 ms |
35816 KB |
Output is correct |
34 |
Correct |
33 ms |
36132 KB |
Output is correct |
35 |
Correct |
35 ms |
36312 KB |
Output is correct |
36 |
Correct |
35 ms |
36224 KB |
Output is correct |
37 |
Correct |
36 ms |
36268 KB |
Output is correct |
38 |
Correct |
33 ms |
35804 KB |
Output is correct |
39 |
Correct |
36 ms |
36196 KB |
Output is correct |
40 |
Correct |
35 ms |
36204 KB |
Output is correct |
41 |
Correct |
34 ms |
36224 KB |
Output is correct |
42 |
Correct |
35 ms |
36188 KB |
Output is correct |
43 |
Correct |
35 ms |
36344 KB |
Output is correct |
44 |
Correct |
32 ms |
35704 KB |
Output is correct |
45 |
Correct |
34 ms |
36216 KB |
Output is correct |
46 |
Correct |
35 ms |
36216 KB |
Output is correct |
47 |
Correct |
31 ms |
35712 KB |
Output is correct |
48 |
Correct |
36 ms |
36216 KB |
Output is correct |
49 |
Correct |
34 ms |
36344 KB |
Output is correct |
50 |
Correct |
36 ms |
36344 KB |
Output is correct |
51 |
Correct |
34 ms |
36224 KB |
Output is correct |
52 |
Correct |
34 ms |
36152 KB |
Output is correct |
53 |
Correct |
33 ms |
36216 KB |
Output is correct |
54 |
Correct |
34 ms |
36216 KB |
Output is correct |
55 |
Correct |
32 ms |
35708 KB |
Output is correct |
56 |
Correct |
91 ms |
49904 KB |
Output is correct |
57 |
Correct |
122 ms |
49528 KB |
Output is correct |
58 |
Correct |
33 ms |
36144 KB |
Output is correct |
59 |
Correct |
31 ms |
35712 KB |
Output is correct |
60 |
Correct |
31 ms |
35712 KB |
Output is correct |
61 |
Correct |
118 ms |
49532 KB |
Output is correct |
62 |
Correct |
35 ms |
36216 KB |
Output is correct |
63 |
Correct |
170 ms |
54000 KB |
Output is correct |
64 |
Correct |
126 ms |
49528 KB |
Output is correct |
65 |
Correct |
34 ms |
36216 KB |
Output is correct |
66 |
Correct |
148 ms |
49784 KB |
Output is correct |
67 |
Correct |
36 ms |
36216 KB |
Output is correct |
68 |
Correct |
35 ms |
36224 KB |
Output is correct |
69 |
Correct |
97 ms |
49776 KB |
Output is correct |
70 |
Correct |
41 ms |
36344 KB |
Output is correct |
71 |
Correct |
115 ms |
49524 KB |
Output is correct |
72 |
Correct |
158 ms |
50780 KB |
Output is correct |
73 |
Correct |
31 ms |
35704 KB |
Output is correct |
74 |
Correct |
167 ms |
54084 KB |
Output is correct |
75 |
Correct |
138 ms |
51832 KB |
Output is correct |
76 |
Correct |
38 ms |
36224 KB |
Output is correct |
77 |
Correct |
33 ms |
36216 KB |
Output is correct |
78 |
Correct |
146 ms |
49776 KB |
Output is correct |
79 |
Correct |
148 ms |
55916 KB |
Output is correct |
80 |
Correct |
34 ms |
36216 KB |
Output is correct |
81 |
Correct |
34 ms |
36216 KB |
Output is correct |
82 |
Correct |
31 ms |
35704 KB |
Output is correct |
83 |
Correct |
32 ms |
35712 KB |
Output is correct |
84 |
Correct |
35 ms |
36344 KB |
Output is correct |
85 |
Correct |
153 ms |
51064 KB |
Output is correct |
86 |
Correct |
35 ms |
36216 KB |
Output is correct |
87 |
Correct |
118 ms |
49656 KB |
Output is correct |
88 |
Correct |
34 ms |
35748 KB |
Output is correct |
89 |
Correct |
152 ms |
49696 KB |
Output is correct |
90 |
Correct |
164 ms |
51528 KB |
Output is correct |
91 |
Correct |
189 ms |
55672 KB |
Output is correct |
92 |
Correct |
99 ms |
50032 KB |
Output is correct |
93 |
Correct |
35 ms |
36272 KB |
Output is correct |
94 |
Correct |
32 ms |
35704 KB |
Output is correct |
95 |
Correct |
157 ms |
55248 KB |
Output is correct |
96 |
Correct |
204 ms |
57800 KB |
Output is correct |
97 |
Correct |
34 ms |
36216 KB |
Output is correct |
98 |
Correct |
31 ms |
35704 KB |
Output is correct |
99 |
Correct |
126 ms |
51756 KB |
Output is correct |
100 |
Correct |
34 ms |
36216 KB |
Output is correct |
101 |
Correct |
147 ms |
50296 KB |
Output is correct |
102 |
Correct |
181 ms |
57080 KB |
Output is correct |
103 |
Correct |
36 ms |
36344 KB |
Output is correct |
104 |
Correct |
35 ms |
36344 KB |
Output is correct |
105 |
Correct |
37 ms |
36260 KB |
Output is correct |
106 |
Correct |
36 ms |
36216 KB |
Output is correct |
107 |
Correct |
78 ms |
36216 KB |
Output is correct |
108 |
Correct |
824 ms |
111132 KB |
Output is correct |
109 |
Correct |
1109 ms |
114152 KB |
Output is correct |
110 |
Correct |
1241 ms |
120944 KB |
Output is correct |
111 |
Correct |
1433 ms |
145784 KB |
Output is correct |
112 |
Correct |
1298 ms |
134716 KB |
Output is correct |
113 |
Correct |
940 ms |
131888 KB |
Output is correct |
114 |
Correct |
657 ms |
103632 KB |
Output is correct |
115 |
Correct |
681 ms |
103656 KB |
Output is correct |
116 |
Correct |
1110 ms |
106592 KB |
Output is correct |
117 |
Correct |
1324 ms |
133868 KB |
Output is correct |
118 |
Correct |
796 ms |
103360 KB |
Output is correct |
119 |
Correct |
1299 ms |
133952 KB |
Output is correct |
120 |
Correct |
1363 ms |
141400 KB |
Output is correct |
121 |
Correct |
1304 ms |
133824 KB |
Output is correct |
122 |
Correct |
1138 ms |
114144 KB |
Output is correct |
123 |
Correct |
907 ms |
136644 KB |
Output is correct |
124 |
Correct |
1340 ms |
125964 KB |
Output is correct |