#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
//#define endl '\n'
#define all(x) begin(x), end(x)
int n,k,a,b,ts;
vector<int> grph[200069],towns[200069];
int col[200069],cnt[200069],curcnt[200069],done[200069];
int sz[200069],par[200069];
bool cent[200069];
void clear(int x, int lst=-1) {
curcnt[col[x]]=0;
done[col[x]]=0;
for (auto i : grph[x]) {
if (i==lst||cent[i]) {
continue;
}
clear(i,x);
}
}
int dfs(int x, int lst) {
sz[x]=1;
par[x]=lst;
curcnt[col[x]]++;
for (auto i : grph[x]) {
if (i==lst||cent[i]) {
continue;
}
sz[x]+=dfs(i,x);
}
return sz[x];
}
int fndcent(int x, int lst=-1) {
for (auto i : grph[x]) {
if (i==lst||cent[i]) {
continue;
}
if (sz[i]*2>ts) {
return fndcent(i,x);
}
}
return x;
}
int solve(int x) {
clear(x);
ts=dfs(x,x);
x=fndcent(x);
//x is now the centroid
clear(x);
dfs(x,x);
stack<int> sk;
sk.push(col[x]);
int ans=-1;
while (sk.size()) {
int tmp=sk.top();
sk.pop();
if (done[tmp]) {
continue;
}
done[tmp]=true;
if (curcnt[tmp]!=cnt[tmp]) {
ans=INT_MAX;
break;
}
ans++;
for (auto i:towns[tmp]) {
sk.push(col[par[i]]);
}
}
cent[x]=true;
for (auto i:grph[x]) {
if (!cent[i]) {
ans=min(ans,solve(i));
}
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>k;
for (int i = 0; i < n-1; i++) {
cin>>a>>b;
a--;b--;
grph[a].push_back(b);
grph[b].push_back(a);
}
for (int i = 0; i < n; i++) {
cin>>col[i];
col[i]--;
cnt[col[i]]++;
towns[col[i]].push_back(i);
}
cout<<solve(0)<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9724 KB |
Output is correct |
3 |
Correct |
7 ms |
9732 KB |
Output is correct |
4 |
Correct |
6 ms |
9728 KB |
Output is correct |
5 |
Correct |
6 ms |
9748 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9680 KB |
Output is correct |
10 |
Correct |
6 ms |
9724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9724 KB |
Output is correct |
3 |
Correct |
7 ms |
9732 KB |
Output is correct |
4 |
Correct |
6 ms |
9728 KB |
Output is correct |
5 |
Correct |
6 ms |
9748 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9680 KB |
Output is correct |
10 |
Correct |
6 ms |
9724 KB |
Output is correct |
11 |
Correct |
7 ms |
9812 KB |
Output is correct |
12 |
Correct |
7 ms |
9852 KB |
Output is correct |
13 |
Correct |
9 ms |
9812 KB |
Output is correct |
14 |
Correct |
8 ms |
9860 KB |
Output is correct |
15 |
Correct |
7 ms |
9940 KB |
Output is correct |
16 |
Correct |
8 ms |
9860 KB |
Output is correct |
17 |
Correct |
7 ms |
9940 KB |
Output is correct |
18 |
Correct |
9 ms |
9864 KB |
Output is correct |
19 |
Correct |
7 ms |
9940 KB |
Output is correct |
20 |
Correct |
8 ms |
9940 KB |
Output is correct |
21 |
Correct |
7 ms |
9968 KB |
Output is correct |
22 |
Correct |
7 ms |
9984 KB |
Output is correct |
23 |
Correct |
7 ms |
9940 KB |
Output is correct |
24 |
Correct |
7 ms |
9940 KB |
Output is correct |
25 |
Correct |
7 ms |
9940 KB |
Output is correct |
26 |
Correct |
8 ms |
9940 KB |
Output is correct |
27 |
Correct |
8 ms |
9920 KB |
Output is correct |
28 |
Correct |
8 ms |
9948 KB |
Output is correct |
29 |
Correct |
8 ms |
9860 KB |
Output is correct |
30 |
Correct |
10 ms |
9980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
745 ms |
35388 KB |
Output is correct |
2 |
Correct |
255 ms |
38956 KB |
Output is correct |
3 |
Correct |
710 ms |
38260 KB |
Output is correct |
4 |
Correct |
238 ms |
39012 KB |
Output is correct |
5 |
Correct |
651 ms |
35956 KB |
Output is correct |
6 |
Correct |
251 ms |
38984 KB |
Output is correct |
7 |
Correct |
803 ms |
36056 KB |
Output is correct |
8 |
Correct |
280 ms |
38488 KB |
Output is correct |
9 |
Correct |
987 ms |
34164 KB |
Output is correct |
10 |
Correct |
952 ms |
32248 KB |
Output is correct |
11 |
Correct |
921 ms |
34528 KB |
Output is correct |
12 |
Correct |
928 ms |
36740 KB |
Output is correct |
13 |
Correct |
904 ms |
31924 KB |
Output is correct |
14 |
Correct |
973 ms |
36900 KB |
Output is correct |
15 |
Correct |
924 ms |
36636 KB |
Output is correct |
16 |
Correct |
892 ms |
32656 KB |
Output is correct |
17 |
Correct |
969 ms |
33116 KB |
Output is correct |
18 |
Correct |
935 ms |
33240 KB |
Output is correct |
19 |
Correct |
1010 ms |
35752 KB |
Output is correct |
20 |
Correct |
938 ms |
37580 KB |
Output is correct |
21 |
Correct |
6 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9724 KB |
Output is correct |
3 |
Correct |
7 ms |
9732 KB |
Output is correct |
4 |
Correct |
6 ms |
9728 KB |
Output is correct |
5 |
Correct |
6 ms |
9748 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9680 KB |
Output is correct |
10 |
Correct |
6 ms |
9724 KB |
Output is correct |
11 |
Correct |
7 ms |
9812 KB |
Output is correct |
12 |
Correct |
7 ms |
9852 KB |
Output is correct |
13 |
Correct |
9 ms |
9812 KB |
Output is correct |
14 |
Correct |
8 ms |
9860 KB |
Output is correct |
15 |
Correct |
7 ms |
9940 KB |
Output is correct |
16 |
Correct |
8 ms |
9860 KB |
Output is correct |
17 |
Correct |
7 ms |
9940 KB |
Output is correct |
18 |
Correct |
9 ms |
9864 KB |
Output is correct |
19 |
Correct |
7 ms |
9940 KB |
Output is correct |
20 |
Correct |
8 ms |
9940 KB |
Output is correct |
21 |
Correct |
7 ms |
9968 KB |
Output is correct |
22 |
Correct |
7 ms |
9984 KB |
Output is correct |
23 |
Correct |
7 ms |
9940 KB |
Output is correct |
24 |
Correct |
7 ms |
9940 KB |
Output is correct |
25 |
Correct |
7 ms |
9940 KB |
Output is correct |
26 |
Correct |
8 ms |
9940 KB |
Output is correct |
27 |
Correct |
8 ms |
9920 KB |
Output is correct |
28 |
Correct |
8 ms |
9948 KB |
Output is correct |
29 |
Correct |
8 ms |
9860 KB |
Output is correct |
30 |
Correct |
10 ms |
9980 KB |
Output is correct |
31 |
Correct |
745 ms |
35388 KB |
Output is correct |
32 |
Correct |
255 ms |
38956 KB |
Output is correct |
33 |
Correct |
710 ms |
38260 KB |
Output is correct |
34 |
Correct |
238 ms |
39012 KB |
Output is correct |
35 |
Correct |
651 ms |
35956 KB |
Output is correct |
36 |
Correct |
251 ms |
38984 KB |
Output is correct |
37 |
Correct |
803 ms |
36056 KB |
Output is correct |
38 |
Correct |
280 ms |
38488 KB |
Output is correct |
39 |
Correct |
987 ms |
34164 KB |
Output is correct |
40 |
Correct |
952 ms |
32248 KB |
Output is correct |
41 |
Correct |
921 ms |
34528 KB |
Output is correct |
42 |
Correct |
928 ms |
36740 KB |
Output is correct |
43 |
Correct |
904 ms |
31924 KB |
Output is correct |
44 |
Correct |
973 ms |
36900 KB |
Output is correct |
45 |
Correct |
924 ms |
36636 KB |
Output is correct |
46 |
Correct |
892 ms |
32656 KB |
Output is correct |
47 |
Correct |
969 ms |
33116 KB |
Output is correct |
48 |
Correct |
935 ms |
33240 KB |
Output is correct |
49 |
Correct |
1010 ms |
35752 KB |
Output is correct |
50 |
Correct |
938 ms |
37580 KB |
Output is correct |
51 |
Correct |
6 ms |
9684 KB |
Output is correct |
52 |
Correct |
558 ms |
24548 KB |
Output is correct |
53 |
Correct |
624 ms |
24544 KB |
Output is correct |
54 |
Correct |
574 ms |
24544 KB |
Output is correct |
55 |
Correct |
613 ms |
24552 KB |
Output is correct |
56 |
Correct |
570 ms |
24544 KB |
Output is correct |
57 |
Correct |
583 ms |
24636 KB |
Output is correct |
58 |
Correct |
586 ms |
27812 KB |
Output is correct |
59 |
Correct |
507 ms |
28236 KB |
Output is correct |
60 |
Correct |
669 ms |
27636 KB |
Output is correct |
61 |
Correct |
765 ms |
27480 KB |
Output is correct |
62 |
Correct |
250 ms |
39024 KB |
Output is correct |
63 |
Correct |
258 ms |
39004 KB |
Output is correct |
64 |
Correct |
249 ms |
38656 KB |
Output is correct |
65 |
Correct |
242 ms |
38924 KB |
Output is correct |
66 |
Correct |
428 ms |
31852 KB |
Output is correct |
67 |
Correct |
381 ms |
31752 KB |
Output is correct |
68 |
Correct |
446 ms |
31880 KB |
Output is correct |
69 |
Correct |
406 ms |
31812 KB |
Output is correct |
70 |
Correct |
429 ms |
31652 KB |
Output is correct |
71 |
Correct |
469 ms |
31776 KB |
Output is correct |
72 |
Correct |
414 ms |
31844 KB |
Output is correct |
73 |
Correct |
427 ms |
31144 KB |
Output is correct |
74 |
Correct |
451 ms |
31816 KB |
Output is correct |
75 |
Correct |
402 ms |
31832 KB |
Output is correct |
76 |
Correct |
676 ms |
31132 KB |
Output is correct |
77 |
Correct |
622 ms |
29716 KB |
Output is correct |
78 |
Correct |
883 ms |
33080 KB |
Output is correct |
79 |
Correct |
883 ms |
31644 KB |
Output is correct |
80 |
Correct |
787 ms |
37228 KB |
Output is correct |
81 |
Correct |
847 ms |
34352 KB |
Output is correct |
82 |
Correct |
860 ms |
34484 KB |
Output is correct |
83 |
Correct |
867 ms |
31752 KB |
Output is correct |
84 |
Correct |
864 ms |
36500 KB |
Output is correct |
85 |
Correct |
868 ms |
35196 KB |
Output is correct |
86 |
Correct |
883 ms |
31432 KB |
Output is correct |
87 |
Correct |
798 ms |
32860 KB |
Output is correct |
88 |
Correct |
722 ms |
33852 KB |
Output is correct |
89 |
Correct |
739 ms |
30580 KB |
Output is correct |
90 |
Correct |
666 ms |
30452 KB |
Output is correct |
91 |
Correct |
751 ms |
32296 KB |
Output is correct |
92 |
Correct |
700 ms |
31308 KB |
Output is correct |
93 |
Correct |
743 ms |
31016 KB |
Output is correct |
94 |
Correct |
700 ms |
30540 KB |
Output is correct |
95 |
Correct |
751 ms |
31820 KB |
Output is correct |
96 |
Correct |
727 ms |
30664 KB |
Output is correct |
97 |
Correct |
715 ms |
32312 KB |
Output is correct |