#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a,b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a,b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
read the solution a long time ago, remember the solution idea
*/
const int MOD = 1e9 + 7;
const int N = 2e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
vector<int> adj[N];
vector<int> a(N);
vector<int> subsiz(N);
vector<bool> rem(N);
vector<int> par(N);
vector<int> nodes;
void dfs1(int u, int p){
nodes.pb(u);
subsiz[u] = 1;
par[u] = p;
trav(v,adj[u]){
if(v == p or rem[v]) conts;
dfs1(v,u);
subsiz[u] += subsiz[v];
}
}
int dfs2(int u, int p, int nodes){
trav(v,adj[u]){
if(v == p or rem[v]) conts;
if(subsiz[v] > nodes/2){
return dfs2(v,u,nodes);
}
}
return u;
}
vector<int> here[N];
vector<bool> vis(N), done(N);
vector<int> tot_cnt(N);
int go(int u){
nodes.clear();
dfs1(u,-1);
int c = dfs2(u,-1,sz(nodes));
nodes.clear();
dfs1(c,-1);
trav(v,nodes){
here[a[v]].pb(v);
}
queue<int> q;
q.push(a[c]);
vis[a[c]] = 1;
int cols = 0;
bool ok = true;
while(!q.empty()){
int col = q.front();
q.pop();
cols++;
if(sz(here[col]) != tot_cnt[col]){
ok = false;
}
trav(v,here[col]){
int w = v;
while(w != -1 and !done[w]){
if(!vis[a[w]]){
q.push(a[w]);
vis[a[w]] = 1;
}
done[w] = 1;
w = par[w];
}
}
}
int ans = inf1;
if(ok){
ans = cols-1;
}
trav(v,nodes){
here[a[v]].clear();
vis[a[v]] = done[v] = 0;
}
rem[c] = 1;
trav(v,adj[c]){
if(rem[v]) conts;
amin(ans,go(v));
}
return ans;
}
void solve(int test_case)
{
int n,k; cin >> n >> k;
rep1(i,n-1){
int u,v; cin >> u >> v;
adj[u].pb(v), adj[v].pb(u);
}
rep1(i,n) cin >> a[i];
rep1(i,n) tot_cnt[a[i]]++;
int ans = go(1);
cout << ans << endl;
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12888 KB |
Output is correct |
2 |
Correct |
5 ms |
12892 KB |
Output is correct |
3 |
Correct |
5 ms |
12892 KB |
Output is correct |
4 |
Correct |
4 ms |
12892 KB |
Output is correct |
5 |
Correct |
4 ms |
12888 KB |
Output is correct |
6 |
Correct |
4 ms |
12892 KB |
Output is correct |
7 |
Correct |
5 ms |
12892 KB |
Output is correct |
8 |
Correct |
5 ms |
12892 KB |
Output is correct |
9 |
Correct |
4 ms |
12892 KB |
Output is correct |
10 |
Correct |
4 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12888 KB |
Output is correct |
2 |
Correct |
5 ms |
12892 KB |
Output is correct |
3 |
Correct |
5 ms |
12892 KB |
Output is correct |
4 |
Correct |
4 ms |
12892 KB |
Output is correct |
5 |
Correct |
4 ms |
12888 KB |
Output is correct |
6 |
Correct |
4 ms |
12892 KB |
Output is correct |
7 |
Correct |
5 ms |
12892 KB |
Output is correct |
8 |
Correct |
5 ms |
12892 KB |
Output is correct |
9 |
Correct |
4 ms |
12892 KB |
Output is correct |
10 |
Correct |
4 ms |
12892 KB |
Output is correct |
11 |
Correct |
6 ms |
13088 KB |
Output is correct |
12 |
Correct |
5 ms |
13148 KB |
Output is correct |
13 |
Correct |
5 ms |
12992 KB |
Output is correct |
14 |
Correct |
5 ms |
13148 KB |
Output is correct |
15 |
Correct |
5 ms |
13148 KB |
Output is correct |
16 |
Correct |
6 ms |
13148 KB |
Output is correct |
17 |
Correct |
5 ms |
13148 KB |
Output is correct |
18 |
Correct |
5 ms |
13148 KB |
Output is correct |
19 |
Correct |
5 ms |
13148 KB |
Output is correct |
20 |
Correct |
5 ms |
13148 KB |
Output is correct |
21 |
Correct |
5 ms |
13148 KB |
Output is correct |
22 |
Correct |
5 ms |
13148 KB |
Output is correct |
23 |
Correct |
5 ms |
13148 KB |
Output is correct |
24 |
Correct |
5 ms |
13148 KB |
Output is correct |
25 |
Correct |
5 ms |
13148 KB |
Output is correct |
26 |
Correct |
5 ms |
13148 KB |
Output is correct |
27 |
Correct |
5 ms |
13148 KB |
Output is correct |
28 |
Correct |
6 ms |
13148 KB |
Output is correct |
29 |
Correct |
5 ms |
13096 KB |
Output is correct |
30 |
Correct |
5 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
350 ms |
35172 KB |
Output is correct |
2 |
Correct |
181 ms |
39372 KB |
Output is correct |
3 |
Correct |
332 ms |
38852 KB |
Output is correct |
4 |
Correct |
179 ms |
39184 KB |
Output is correct |
5 |
Correct |
350 ms |
36388 KB |
Output is correct |
6 |
Correct |
173 ms |
39428 KB |
Output is correct |
7 |
Correct |
344 ms |
36500 KB |
Output is correct |
8 |
Correct |
188 ms |
39092 KB |
Output is correct |
9 |
Correct |
440 ms |
35052 KB |
Output is correct |
10 |
Correct |
477 ms |
33228 KB |
Output is correct |
11 |
Correct |
526 ms |
35272 KB |
Output is correct |
12 |
Correct |
468 ms |
37688 KB |
Output is correct |
13 |
Correct |
398 ms |
32604 KB |
Output is correct |
14 |
Correct |
408 ms |
37576 KB |
Output is correct |
15 |
Correct |
474 ms |
37808 KB |
Output is correct |
16 |
Correct |
424 ms |
33308 KB |
Output is correct |
17 |
Correct |
433 ms |
34004 KB |
Output is correct |
18 |
Correct |
430 ms |
34104 KB |
Output is correct |
19 |
Correct |
396 ms |
36528 KB |
Output is correct |
20 |
Correct |
431 ms |
38412 KB |
Output is correct |
21 |
Correct |
4 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12888 KB |
Output is correct |
2 |
Correct |
5 ms |
12892 KB |
Output is correct |
3 |
Correct |
5 ms |
12892 KB |
Output is correct |
4 |
Correct |
4 ms |
12892 KB |
Output is correct |
5 |
Correct |
4 ms |
12888 KB |
Output is correct |
6 |
Correct |
4 ms |
12892 KB |
Output is correct |
7 |
Correct |
5 ms |
12892 KB |
Output is correct |
8 |
Correct |
5 ms |
12892 KB |
Output is correct |
9 |
Correct |
4 ms |
12892 KB |
Output is correct |
10 |
Correct |
4 ms |
12892 KB |
Output is correct |
11 |
Correct |
6 ms |
13088 KB |
Output is correct |
12 |
Correct |
5 ms |
13148 KB |
Output is correct |
13 |
Correct |
5 ms |
12992 KB |
Output is correct |
14 |
Correct |
5 ms |
13148 KB |
Output is correct |
15 |
Correct |
5 ms |
13148 KB |
Output is correct |
16 |
Correct |
6 ms |
13148 KB |
Output is correct |
17 |
Correct |
5 ms |
13148 KB |
Output is correct |
18 |
Correct |
5 ms |
13148 KB |
Output is correct |
19 |
Correct |
5 ms |
13148 KB |
Output is correct |
20 |
Correct |
5 ms |
13148 KB |
Output is correct |
21 |
Correct |
5 ms |
13148 KB |
Output is correct |
22 |
Correct |
5 ms |
13148 KB |
Output is correct |
23 |
Correct |
5 ms |
13148 KB |
Output is correct |
24 |
Correct |
5 ms |
13148 KB |
Output is correct |
25 |
Correct |
5 ms |
13148 KB |
Output is correct |
26 |
Correct |
5 ms |
13148 KB |
Output is correct |
27 |
Correct |
5 ms |
13148 KB |
Output is correct |
28 |
Correct |
6 ms |
13148 KB |
Output is correct |
29 |
Correct |
5 ms |
13096 KB |
Output is correct |
30 |
Correct |
5 ms |
13148 KB |
Output is correct |
31 |
Correct |
350 ms |
35172 KB |
Output is correct |
32 |
Correct |
181 ms |
39372 KB |
Output is correct |
33 |
Correct |
332 ms |
38852 KB |
Output is correct |
34 |
Correct |
179 ms |
39184 KB |
Output is correct |
35 |
Correct |
350 ms |
36388 KB |
Output is correct |
36 |
Correct |
173 ms |
39428 KB |
Output is correct |
37 |
Correct |
344 ms |
36500 KB |
Output is correct |
38 |
Correct |
188 ms |
39092 KB |
Output is correct |
39 |
Correct |
440 ms |
35052 KB |
Output is correct |
40 |
Correct |
477 ms |
33228 KB |
Output is correct |
41 |
Correct |
526 ms |
35272 KB |
Output is correct |
42 |
Correct |
468 ms |
37688 KB |
Output is correct |
43 |
Correct |
398 ms |
32604 KB |
Output is correct |
44 |
Correct |
408 ms |
37576 KB |
Output is correct |
45 |
Correct |
474 ms |
37808 KB |
Output is correct |
46 |
Correct |
424 ms |
33308 KB |
Output is correct |
47 |
Correct |
433 ms |
34004 KB |
Output is correct |
48 |
Correct |
430 ms |
34104 KB |
Output is correct |
49 |
Correct |
396 ms |
36528 KB |
Output is correct |
50 |
Correct |
431 ms |
38412 KB |
Output is correct |
51 |
Correct |
4 ms |
12892 KB |
Output is correct |
52 |
Correct |
329 ms |
25284 KB |
Output is correct |
53 |
Correct |
287 ms |
25440 KB |
Output is correct |
54 |
Correct |
259 ms |
25320 KB |
Output is correct |
55 |
Correct |
301 ms |
25436 KB |
Output is correct |
56 |
Correct |
262 ms |
25444 KB |
Output is correct |
57 |
Correct |
316 ms |
25440 KB |
Output is correct |
58 |
Correct |
260 ms |
28516 KB |
Output is correct |
59 |
Correct |
262 ms |
28612 KB |
Output is correct |
60 |
Correct |
350 ms |
28100 KB |
Output is correct |
61 |
Correct |
394 ms |
28012 KB |
Output is correct |
62 |
Correct |
170 ms |
39632 KB |
Output is correct |
63 |
Correct |
169 ms |
39372 KB |
Output is correct |
64 |
Correct |
167 ms |
39044 KB |
Output is correct |
65 |
Correct |
187 ms |
39408 KB |
Output is correct |
66 |
Correct |
208 ms |
32844 KB |
Output is correct |
67 |
Correct |
221 ms |
32580 KB |
Output is correct |
68 |
Correct |
237 ms |
32924 KB |
Output is correct |
69 |
Correct |
227 ms |
32836 KB |
Output is correct |
70 |
Correct |
225 ms |
32740 KB |
Output is correct |
71 |
Correct |
236 ms |
32524 KB |
Output is correct |
72 |
Correct |
243 ms |
32584 KB |
Output is correct |
73 |
Correct |
228 ms |
32108 KB |
Output is correct |
74 |
Correct |
229 ms |
32680 KB |
Output is correct |
75 |
Correct |
225 ms |
32792 KB |
Output is correct |
76 |
Correct |
311 ms |
31676 KB |
Output is correct |
77 |
Correct |
279 ms |
29900 KB |
Output is correct |
78 |
Correct |
427 ms |
33960 KB |
Output is correct |
79 |
Correct |
493 ms |
32448 KB |
Output is correct |
80 |
Correct |
459 ms |
37784 KB |
Output is correct |
81 |
Correct |
454 ms |
35216 KB |
Output is correct |
82 |
Correct |
442 ms |
35276 KB |
Output is correct |
83 |
Correct |
456 ms |
32972 KB |
Output is correct |
84 |
Correct |
506 ms |
37576 KB |
Output is correct |
85 |
Correct |
454 ms |
36088 KB |
Output is correct |
86 |
Correct |
454 ms |
32296 KB |
Output is correct |
87 |
Correct |
453 ms |
33480 KB |
Output is correct |
88 |
Correct |
398 ms |
34592 KB |
Output is correct |
89 |
Correct |
354 ms |
31372 KB |
Output is correct |
90 |
Correct |
369 ms |
31248 KB |
Output is correct |
91 |
Correct |
370 ms |
33152 KB |
Output is correct |
92 |
Correct |
363 ms |
32324 KB |
Output is correct |
93 |
Correct |
398 ms |
31684 KB |
Output is correct |
94 |
Correct |
360 ms |
31292 KB |
Output is correct |
95 |
Correct |
340 ms |
32456 KB |
Output is correct |
96 |
Correct |
347 ms |
31428 KB |
Output is correct |
97 |
Correct |
372 ms |
33088 KB |
Output is correct |