#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll, ll>
#define f first
#define s second
#define SZ(x) (int)((x).size())
#define ALL(x) (x).begin(), (x).end()
#define pb push_back
#define MX(a,b) a = max(a,b)
#define MN(a,b) a = min(a,b)
#define FOR(i,a,b) for (int i = a; i<b; ++i)
#define REP(i,n) for (int i = 0; i<n; ++i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<"- "<<#__VA_ARGS__<<": ", _do(__VA_ARGS__)
template<typename T> void _do(T && x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S && ...y){cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT
const int maxn = 2e5+5;
const int mod = 1e9+7;
bool dead[maxn];
bool colorok[maxn];
int color[maxn];
int sz[maxn];
vector<int> g[maxn];
vector<int> where[maxn]; // where are the nodes of this color?
void predfs(int v, int p) {
sz[v] = 1;
for (int u : g[v]) {
if (dead[u]) continue;
if (u != p) {
predfs(u,v);
sz[v] += sz[u];
}
}
}
int allsz = -1;
int getcen(int v, int p) {
for (int u : g[v]) {
if (dead[u]) continue;
if (u != p && sz[u] > (allsz)/2) {
return getcen(u,v);
}
}
return v;
}
int dep[maxn];
bool done[maxn]; // make sure to clear!!!
bool inque[maxn]; // make sure to clear!!!
int par[maxn];
vector<int> colorshere;
int subtree[maxn]; // which subtree is it?????
void dfs(int v, int p) {
done[v] = 0; inque[color[v]] = 0;
colorshere.pb(color[v]);
for (int u : g[v]) {
if (dead[u]) continue;
if (u!=p) {
dep[u] = dep[v] + 1; par[u] = v;
if (p == -1) {
subtree[u] = u;
}else{
subtree[u] = subtree[v];
}
dfs(u,v);
}
}
}
int re = maxn;
void decomp(int stv, int lay) {
predfs(stv, -1); allsz = sz[stv];
int cen = getcen(stv, -1);
bug(stv, lay, cen);
stv = -1;
dep[cen] = 0;
colorshere.clear();
dfs(cen, -1);
sort(ALL(colorshere)); colorshere.resize(unique(ALL(colorshere)) - colorshere.begin());
done[cen] = 1;
if (colorok[color[cen]]) {
queue<int> q; q.push(color[cen]);
inque[color[cen]] = 1;
int nmerge = 0;
bool bad = 0;
while (!q.empty()) {
int cc = q.front(); q.pop();
for (int x : where[cc]) {
for (int at = x; !done[at]; at= par[at]) {
done[at] = 1;
int c = color[at];
if (!inque[c] && colorok[c]) {
inque[c] = 1; q.push(c); ++nmerge;
}
if (!colorok[c]) {
bad = 1; break;
}
}
if (bad) break;
}
}
if (!bad) {
bug(nmerge);
MN(re, nmerge);
}
}
dead[cen] = 1; colorok[color[cen]] = 0;
for (int t : colorshere) {
if (colorok[t]) {
int mn = maxn, mx = -1;
for (int x : where[t]) {
MN(mn, subtree[x]); MX(mx, subtree[x]);
}
if (mn < mx) {
colorok[t] = 0;
}
}
}
for (int u : g[cen]) {
if (!dead[u]) {
decomp(u, lay+1);
}
}
}
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
bug(1,2);
int n,c; cin>>n>>c;
REP(i,n-1) {
int a,b; cin>>a>>b; --a; --b;
g[a].pb(b); g[b].pb(a);
}
REP(i,n) {
cin>>color[i]; colorok[color[i]] = 1;
where[color[i]].pb(i);
}
decomp(0, 0);
cout<<re<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9732 KB |
Output is correct |
3 |
Correct |
5 ms |
9692 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 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 |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9732 KB |
Output is correct |
3 |
Correct |
5 ms |
9692 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 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 |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
7 ms |
9924 KB |
Output is correct |
12 |
Correct |
7 ms |
9812 KB |
Output is correct |
13 |
Correct |
7 ms |
9812 KB |
Output is correct |
14 |
Correct |
7 ms |
9872 KB |
Output is correct |
15 |
Correct |
8 ms |
9924 KB |
Output is correct |
16 |
Correct |
8 ms |
9940 KB |
Output is correct |
17 |
Correct |
8 ms |
9940 KB |
Output is correct |
18 |
Correct |
6 ms |
9944 KB |
Output is correct |
19 |
Correct |
6 ms |
9940 KB |
Output is correct |
20 |
Correct |
7 ms |
9912 KB |
Output is correct |
21 |
Correct |
6 ms |
10000 KB |
Output is correct |
22 |
Correct |
6 ms |
9992 KB |
Output is correct |
23 |
Correct |
8 ms |
9940 KB |
Output is correct |
24 |
Correct |
7 ms |
9872 KB |
Output is correct |
25 |
Correct |
8 ms |
9996 KB |
Output is correct |
26 |
Correct |
8 ms |
10000 KB |
Output is correct |
27 |
Correct |
9 ms |
9940 KB |
Output is correct |
28 |
Correct |
7 ms |
9940 KB |
Output is correct |
29 |
Correct |
8 ms |
9940 KB |
Output is correct |
30 |
Correct |
7 ms |
9920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
684 ms |
43336 KB |
Output is correct |
2 |
Correct |
262 ms |
43840 KB |
Output is correct |
3 |
Correct |
630 ms |
43080 KB |
Output is correct |
4 |
Correct |
260 ms |
43800 KB |
Output is correct |
5 |
Correct |
660 ms |
40048 KB |
Output is correct |
6 |
Correct |
264 ms |
43888 KB |
Output is correct |
7 |
Correct |
633 ms |
40428 KB |
Output is correct |
8 |
Correct |
270 ms |
43444 KB |
Output is correct |
9 |
Correct |
806 ms |
38464 KB |
Output is correct |
10 |
Correct |
814 ms |
36060 KB |
Output is correct |
11 |
Correct |
776 ms |
39008 KB |
Output is correct |
12 |
Correct |
847 ms |
41572 KB |
Output is correct |
13 |
Correct |
805 ms |
35624 KB |
Output is correct |
14 |
Correct |
795 ms |
41948 KB |
Output is correct |
15 |
Correct |
793 ms |
41620 KB |
Output is correct |
16 |
Correct |
782 ms |
36612 KB |
Output is correct |
17 |
Correct |
812 ms |
37240 KB |
Output is correct |
18 |
Correct |
795 ms |
37316 KB |
Output is correct |
19 |
Correct |
790 ms |
40516 KB |
Output is correct |
20 |
Correct |
835 ms |
42628 KB |
Output is correct |
21 |
Correct |
5 ms |
9688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9732 KB |
Output is correct |
3 |
Correct |
5 ms |
9692 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 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 |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
7 ms |
9924 KB |
Output is correct |
12 |
Correct |
7 ms |
9812 KB |
Output is correct |
13 |
Correct |
7 ms |
9812 KB |
Output is correct |
14 |
Correct |
7 ms |
9872 KB |
Output is correct |
15 |
Correct |
8 ms |
9924 KB |
Output is correct |
16 |
Correct |
8 ms |
9940 KB |
Output is correct |
17 |
Correct |
8 ms |
9940 KB |
Output is correct |
18 |
Correct |
6 ms |
9944 KB |
Output is correct |
19 |
Correct |
6 ms |
9940 KB |
Output is correct |
20 |
Correct |
7 ms |
9912 KB |
Output is correct |
21 |
Correct |
6 ms |
10000 KB |
Output is correct |
22 |
Correct |
6 ms |
9992 KB |
Output is correct |
23 |
Correct |
8 ms |
9940 KB |
Output is correct |
24 |
Correct |
7 ms |
9872 KB |
Output is correct |
25 |
Correct |
8 ms |
9996 KB |
Output is correct |
26 |
Correct |
8 ms |
10000 KB |
Output is correct |
27 |
Correct |
9 ms |
9940 KB |
Output is correct |
28 |
Correct |
7 ms |
9940 KB |
Output is correct |
29 |
Correct |
8 ms |
9940 KB |
Output is correct |
30 |
Correct |
7 ms |
9920 KB |
Output is correct |
31 |
Correct |
684 ms |
43336 KB |
Output is correct |
32 |
Correct |
262 ms |
43840 KB |
Output is correct |
33 |
Correct |
630 ms |
43080 KB |
Output is correct |
34 |
Correct |
260 ms |
43800 KB |
Output is correct |
35 |
Correct |
660 ms |
40048 KB |
Output is correct |
36 |
Correct |
264 ms |
43888 KB |
Output is correct |
37 |
Correct |
633 ms |
40428 KB |
Output is correct |
38 |
Correct |
270 ms |
43444 KB |
Output is correct |
39 |
Correct |
806 ms |
38464 KB |
Output is correct |
40 |
Correct |
814 ms |
36060 KB |
Output is correct |
41 |
Correct |
776 ms |
39008 KB |
Output is correct |
42 |
Correct |
847 ms |
41572 KB |
Output is correct |
43 |
Correct |
805 ms |
35624 KB |
Output is correct |
44 |
Correct |
795 ms |
41948 KB |
Output is correct |
45 |
Correct |
793 ms |
41620 KB |
Output is correct |
46 |
Correct |
782 ms |
36612 KB |
Output is correct |
47 |
Correct |
812 ms |
37240 KB |
Output is correct |
48 |
Correct |
795 ms |
37316 KB |
Output is correct |
49 |
Correct |
790 ms |
40516 KB |
Output is correct |
50 |
Correct |
835 ms |
42628 KB |
Output is correct |
51 |
Correct |
5 ms |
9688 KB |
Output is correct |
52 |
Correct |
498 ms |
26520 KB |
Output is correct |
53 |
Correct |
502 ms |
26568 KB |
Output is correct |
54 |
Correct |
525 ms |
26436 KB |
Output is correct |
55 |
Correct |
559 ms |
26520 KB |
Output is correct |
56 |
Correct |
474 ms |
26456 KB |
Output is correct |
57 |
Correct |
541 ms |
26588 KB |
Output is correct |
58 |
Correct |
630 ms |
30240 KB |
Output is correct |
59 |
Correct |
564 ms |
30608 KB |
Output is correct |
60 |
Correct |
671 ms |
29944 KB |
Output is correct |
61 |
Correct |
723 ms |
29504 KB |
Output is correct |
62 |
Correct |
273 ms |
43844 KB |
Output is correct |
63 |
Correct |
256 ms |
43832 KB |
Output is correct |
64 |
Correct |
304 ms |
43392 KB |
Output is correct |
65 |
Correct |
250 ms |
43768 KB |
Output is correct |
66 |
Correct |
366 ms |
35280 KB |
Output is correct |
67 |
Correct |
363 ms |
35132 KB |
Output is correct |
68 |
Correct |
338 ms |
35200 KB |
Output is correct |
69 |
Correct |
396 ms |
35208 KB |
Output is correct |
70 |
Correct |
346 ms |
35200 KB |
Output is correct |
71 |
Correct |
384 ms |
35120 KB |
Output is correct |
72 |
Correct |
349 ms |
35180 KB |
Output is correct |
73 |
Correct |
364 ms |
34428 KB |
Output is correct |
74 |
Correct |
345 ms |
35332 KB |
Output is correct |
75 |
Correct |
354 ms |
35216 KB |
Output is correct |
76 |
Correct |
683 ms |
33832 KB |
Output is correct |
77 |
Correct |
688 ms |
32004 KB |
Output is correct |
78 |
Correct |
774 ms |
37128 KB |
Output is correct |
79 |
Correct |
758 ms |
35068 KB |
Output is correct |
80 |
Correct |
780 ms |
42220 KB |
Output is correct |
81 |
Correct |
767 ms |
38584 KB |
Output is correct |
82 |
Correct |
779 ms |
38848 KB |
Output is correct |
83 |
Correct |
782 ms |
35472 KB |
Output is correct |
84 |
Correct |
815 ms |
41404 KB |
Output is correct |
85 |
Correct |
767 ms |
39784 KB |
Output is correct |
86 |
Correct |
780 ms |
35020 KB |
Output is correct |
87 |
Correct |
760 ms |
36784 KB |
Output is correct |
88 |
Correct |
689 ms |
37936 KB |
Output is correct |
89 |
Correct |
655 ms |
33952 KB |
Output is correct |
90 |
Correct |
677 ms |
33784 KB |
Output is correct |
91 |
Correct |
648 ms |
36164 KB |
Output is correct |
92 |
Correct |
708 ms |
34900 KB |
Output is correct |
93 |
Correct |
665 ms |
34508 KB |
Output is correct |
94 |
Correct |
673 ms |
33904 KB |
Output is correct |
95 |
Correct |
658 ms |
35292 KB |
Output is correct |
96 |
Correct |
716 ms |
34040 KB |
Output is correct |
97 |
Correct |
678 ms |
36140 KB |
Output is correct |