#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for ( int i = a ; i <= b ; i++ )
#define FORD(i, a, b) for (int i = b; i >= a; i --)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPD(i, n) for (int i = n - 1; i >= 0; --i)
#define Lg(x) 31 - __builtin_clz(x)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int MAX = 2e5 + 5;
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void setupIO(){
#define name "Whisper"
//Phu Trong from Nguyen Tat Thanh High School for gifted student
srand(time(NULL));
cin.tie(nullptr)->sync_with_stdio(false); cout.tie(nullptr);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
cout << fixed << setprecision(10);
}
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
int numNode, numCity;
vector<int> G[MAX];
int C[MAX];
int sz[MAX], del[MAX];
void reSubsize(int u, int p = -1){
sz[u] = 1;
for (int &v : G[u])if (v ^ p){
if (del[v]) continue;
reSubsize(v, u);
sz[u] += sz[v];
}
}
int getCentroid(int u, int p, int siz){
for (int &v : G[u]) if(v ^ p){
if (2 * sz[v] > siz && !del[v]) return getCentroid(v, u, siz);
}
return u;
}
int cnt[MAX], vis[MAX], par[MAX];
vector<int> color[MAX];
void dfs(int u, int p, bool ok){
if (ok){
color[C[u]].push_back(u); par[u] = p;
}
else{
color[C[u]].clear(); vis[C[u]] = 0;
}
for (int&v : G[u]) if(v ^ p){
if (!del[v]) dfs(v, u, ok);
}
}
int ans = INF;
void decompose(int root){
reSubsize(root);
int siz = sz[root];
root = getCentroid(root, -1, siz);
dfs(root, root, 1);
del[root] = 1;
int ret = 0;
bool full = 1;
queue<int> q;
q.emplace(C[root]); vis[C[root]] = 1;
while (!q.empty()){
int current_color = q.front(); q.pop();
if ((int)color[current_color].size() != cnt[current_color]){full = 0; continue;}
for (int &node : color[current_color]){
int p = par[node];
if (!vis[C[p]]){
vis[C[p]] = 1;
q.emplace(C[p]); ++ ret;
}
}
}
dfs(root, root, 0);
if (full) minimize(ans, ret);
for (int &v : G[root]){
if (!del[v]) decompose(v);
}
}
void Whisper(){
cin >> numNode >> numCity;
for (int i = 1; i < numNode; ++i){
int u, v; cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
for (int i = 1; i <= numNode; ++i){
cin >> C[i];
cnt[C[i]]++;
}
decompose(1);
cout << ans;
}
signed main(){
setupIO();
int Test = 1;
// cin >> Test;
for ( int i = 1 ; i <= Test ; i++ ){
Whisper();
if (i < Test) cout << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
4 ms |
17244 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
3 ms |
17192 KB |
Output is correct |
7 |
Correct |
3 ms |
16988 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
4 ms |
19244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
4 ms |
17244 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
3 ms |
17192 KB |
Output is correct |
7 |
Correct |
3 ms |
16988 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
4 ms |
19244 KB |
Output is correct |
11 |
Correct |
6 ms |
17244 KB |
Output is correct |
12 |
Correct |
4 ms |
17244 KB |
Output is correct |
13 |
Correct |
5 ms |
17300 KB |
Output is correct |
14 |
Correct |
4 ms |
17244 KB |
Output is correct |
15 |
Correct |
5 ms |
17348 KB |
Output is correct |
16 |
Correct |
4 ms |
17244 KB |
Output is correct |
17 |
Correct |
4 ms |
17288 KB |
Output is correct |
18 |
Correct |
4 ms |
17244 KB |
Output is correct |
19 |
Correct |
4 ms |
17244 KB |
Output is correct |
20 |
Correct |
5 ms |
17244 KB |
Output is correct |
21 |
Correct |
6 ms |
17244 KB |
Output is correct |
22 |
Correct |
4 ms |
17412 KB |
Output is correct |
23 |
Correct |
6 ms |
17244 KB |
Output is correct |
24 |
Correct |
4 ms |
17244 KB |
Output is correct |
25 |
Correct |
5 ms |
17244 KB |
Output is correct |
26 |
Correct |
5 ms |
17276 KB |
Output is correct |
27 |
Correct |
5 ms |
17244 KB |
Output is correct |
28 |
Correct |
5 ms |
17204 KB |
Output is correct |
29 |
Correct |
5 ms |
17244 KB |
Output is correct |
30 |
Correct |
5 ms |
17408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
383 ms |
44368 KB |
Output is correct |
2 |
Correct |
154 ms |
44624 KB |
Output is correct |
3 |
Correct |
406 ms |
44328 KB |
Output is correct |
4 |
Correct |
156 ms |
44628 KB |
Output is correct |
5 |
Correct |
376 ms |
41952 KB |
Output is correct |
6 |
Correct |
162 ms |
44820 KB |
Output is correct |
7 |
Correct |
353 ms |
42072 KB |
Output is correct |
8 |
Correct |
153 ms |
44488 KB |
Output is correct |
9 |
Correct |
569 ms |
40020 KB |
Output is correct |
10 |
Correct |
498 ms |
37968 KB |
Output is correct |
11 |
Correct |
479 ms |
40272 KB |
Output is correct |
12 |
Correct |
517 ms |
42380 KB |
Output is correct |
13 |
Correct |
442 ms |
37460 KB |
Output is correct |
14 |
Correct |
460 ms |
42836 KB |
Output is correct |
15 |
Correct |
478 ms |
42604 KB |
Output is correct |
16 |
Correct |
472 ms |
38400 KB |
Output is correct |
17 |
Correct |
518 ms |
38976 KB |
Output is correct |
18 |
Correct |
474 ms |
38744 KB |
Output is correct |
19 |
Correct |
454 ms |
41496 KB |
Output is correct |
20 |
Correct |
479 ms |
43368 KB |
Output is correct |
21 |
Correct |
4 ms |
19292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
4 ms |
17244 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
3 ms |
17192 KB |
Output is correct |
7 |
Correct |
3 ms |
16988 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
4 ms |
19244 KB |
Output is correct |
11 |
Correct |
6 ms |
17244 KB |
Output is correct |
12 |
Correct |
4 ms |
17244 KB |
Output is correct |
13 |
Correct |
5 ms |
17300 KB |
Output is correct |
14 |
Correct |
4 ms |
17244 KB |
Output is correct |
15 |
Correct |
5 ms |
17348 KB |
Output is correct |
16 |
Correct |
4 ms |
17244 KB |
Output is correct |
17 |
Correct |
4 ms |
17288 KB |
Output is correct |
18 |
Correct |
4 ms |
17244 KB |
Output is correct |
19 |
Correct |
4 ms |
17244 KB |
Output is correct |
20 |
Correct |
5 ms |
17244 KB |
Output is correct |
21 |
Correct |
6 ms |
17244 KB |
Output is correct |
22 |
Correct |
4 ms |
17412 KB |
Output is correct |
23 |
Correct |
6 ms |
17244 KB |
Output is correct |
24 |
Correct |
4 ms |
17244 KB |
Output is correct |
25 |
Correct |
5 ms |
17244 KB |
Output is correct |
26 |
Correct |
5 ms |
17276 KB |
Output is correct |
27 |
Correct |
5 ms |
17244 KB |
Output is correct |
28 |
Correct |
5 ms |
17204 KB |
Output is correct |
29 |
Correct |
5 ms |
17244 KB |
Output is correct |
30 |
Correct |
5 ms |
17408 KB |
Output is correct |
31 |
Correct |
383 ms |
44368 KB |
Output is correct |
32 |
Correct |
154 ms |
44624 KB |
Output is correct |
33 |
Correct |
406 ms |
44328 KB |
Output is correct |
34 |
Correct |
156 ms |
44628 KB |
Output is correct |
35 |
Correct |
376 ms |
41952 KB |
Output is correct |
36 |
Correct |
162 ms |
44820 KB |
Output is correct |
37 |
Correct |
353 ms |
42072 KB |
Output is correct |
38 |
Correct |
153 ms |
44488 KB |
Output is correct |
39 |
Correct |
569 ms |
40020 KB |
Output is correct |
40 |
Correct |
498 ms |
37968 KB |
Output is correct |
41 |
Correct |
479 ms |
40272 KB |
Output is correct |
42 |
Correct |
517 ms |
42380 KB |
Output is correct |
43 |
Correct |
442 ms |
37460 KB |
Output is correct |
44 |
Correct |
460 ms |
42836 KB |
Output is correct |
45 |
Correct |
478 ms |
42604 KB |
Output is correct |
46 |
Correct |
472 ms |
38400 KB |
Output is correct |
47 |
Correct |
518 ms |
38976 KB |
Output is correct |
48 |
Correct |
474 ms |
38744 KB |
Output is correct |
49 |
Correct |
454 ms |
41496 KB |
Output is correct |
50 |
Correct |
479 ms |
43368 KB |
Output is correct |
51 |
Correct |
4 ms |
19292 KB |
Output is correct |
52 |
Correct |
286 ms |
31824 KB |
Output is correct |
53 |
Correct |
364 ms |
32044 KB |
Output is correct |
54 |
Correct |
357 ms |
32288 KB |
Output is correct |
55 |
Correct |
341 ms |
32068 KB |
Output is correct |
56 |
Correct |
345 ms |
32060 KB |
Output is correct |
57 |
Correct |
303 ms |
32072 KB |
Output is correct |
58 |
Correct |
318 ms |
35860 KB |
Output is correct |
59 |
Correct |
281 ms |
36140 KB |
Output is correct |
60 |
Correct |
407 ms |
35156 KB |
Output is correct |
61 |
Correct |
409 ms |
34892 KB |
Output is correct |
62 |
Correct |
159 ms |
44820 KB |
Output is correct |
63 |
Correct |
153 ms |
44624 KB |
Output is correct |
64 |
Correct |
153 ms |
44628 KB |
Output is correct |
65 |
Correct |
156 ms |
44628 KB |
Output is correct |
66 |
Correct |
318 ms |
38352 KB |
Output is correct |
67 |
Correct |
243 ms |
38084 KB |
Output is correct |
68 |
Correct |
250 ms |
38600 KB |
Output is correct |
69 |
Correct |
235 ms |
38336 KB |
Output is correct |
70 |
Correct |
258 ms |
38292 KB |
Output is correct |
71 |
Correct |
265 ms |
38300 KB |
Output is correct |
72 |
Correct |
251 ms |
38084 KB |
Output is correct |
73 |
Correct |
260 ms |
37828 KB |
Output is correct |
74 |
Correct |
259 ms |
38144 KB |
Output is correct |
75 |
Correct |
251 ms |
38340 KB |
Output is correct |
76 |
Correct |
335 ms |
38404 KB |
Output is correct |
77 |
Correct |
351 ms |
36692 KB |
Output is correct |
78 |
Correct |
455 ms |
38716 KB |
Output is correct |
79 |
Correct |
490 ms |
37204 KB |
Output is correct |
80 |
Correct |
500 ms |
42864 KB |
Output is correct |
81 |
Correct |
506 ms |
40240 KB |
Output is correct |
82 |
Correct |
480 ms |
40272 KB |
Output is correct |
83 |
Correct |
433 ms |
37388 KB |
Output is correct |
84 |
Correct |
467 ms |
42224 KB |
Output is correct |
85 |
Correct |
455 ms |
40928 KB |
Output is correct |
86 |
Correct |
514 ms |
37084 KB |
Output is correct |
87 |
Correct |
451 ms |
38528 KB |
Output is correct |
88 |
Correct |
448 ms |
39732 KB |
Output is correct |
89 |
Correct |
435 ms |
36732 KB |
Output is correct |
90 |
Correct |
395 ms |
36384 KB |
Output is correct |
91 |
Correct |
449 ms |
38376 KB |
Output is correct |
92 |
Correct |
428 ms |
37712 KB |
Output is correct |
93 |
Correct |
452 ms |
37200 KB |
Output is correct |
94 |
Correct |
422 ms |
36696 KB |
Output is correct |
95 |
Correct |
422 ms |
37876 KB |
Output is correct |
96 |
Correct |
460 ms |
36872 KB |
Output is correct |
97 |
Correct |
425 ms |
38452 KB |
Output is correct |