#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
#define all(x) x.begin(),x.end()
#define pb push_back
#define for1(i,x,n) for(int i=x;i<=n;i++)
#define for2(i,x,n) for(int i=x;i>=n;i--)
//#define int ll
typedef long long ll;
typedef pair<int,int> pii;
const ll maxn=2e5+69;
const ll mod=1e9+7;
const ll inf=1e18;
int n,k,c[maxn];
vector<int> adj[maxn],L[maxn];
int sz[maxn],par[maxn];
bool flag[maxn],vis[maxn],used[maxn],dd[maxn];
void dfs_sz(int u,int pre)
{
sz[u]=1;
for(int v: adj[u])
{
if (v==pre||used[v]) continue;
dfs_sz(v,u);
sz[u]+=sz[v];
}
}
int Find_cen(int u,int pre,int ts)
{
for(int v:adj[u])
{
if (v==pre||used[v]) continue;
if (sz[v]*2>ts) return Find_cen(v,u,ts);
}
return u;
}
void dfs_on(int u,int pre)
{
flag[u]=1;
par[u]=pre;
vis[u]=0;
for(int v:adj[u])
{
if (v==pre||used[v]) continue;
dfs_on(v,u);
}
}
void dfs_off(int u,int pre)
{
flag[u]=0;
vis[u]=0;
dd[c[u]]=0;
for(int v:adj[u])
{
if (v==pre||used[v]) continue;
dfs_off(v,u);
}
}
vector<int>Q;
int centroid(int u)
{
dfs_sz(u,-1);
u=Find_cen(u,-1,sz[u]);
used[u]=1;
dfs_on(u,-1);
Q.clear();
for(int v:L[c[u]]) Q.pb(v);
dd[c[u]]=1;
int res=0;
while (!Q.empty())
{
int u=Q.back();
Q.pop_back();
if (vis[u]) continue;
if (!flag[u])
{
res=n;
break;
}
vis[u]=1;
if (par[u]==-1) continue;
if (!dd[c[par[u]]])
{
for(int v:L[c[par[u]]]) Q.pb(v);
dd[c[par[u]]]=1;
res++;
}
}
// Q.clear();
dfs_off(u,-1);
for(int v: adj[u]) if (!used[v]) res=min(res,centroid(v));
return res;
}
void sol()
{
cin >> n>>k;
for1(i,1,n-1)
{
int u,v; cin >> u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
for1(i,1,n)
{
cin >>c[i];
L[c[i]].pb(i);
}
cout << centroid(1);
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("wall.inp","r",stdin);
// freopen("wall.out","w",stdout);
int t=1;//cin >> t;
while (t--)
{
// cerr<<"avdsava "<< t<<'\n';
sol();
}
}
/*
1
3
3 2 3 1 1 2
4 1
6 1
3 5
5 1
2 4
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9864 KB |
Output is correct |
3 |
Correct |
5 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9848 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9656 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9864 KB |
Output is correct |
3 |
Correct |
5 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9848 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9656 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
6 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9820 KB |
Output is correct |
13 |
Correct |
5 ms |
9896 KB |
Output is correct |
14 |
Correct |
5 ms |
9816 KB |
Output is correct |
15 |
Correct |
5 ms |
9820 KB |
Output is correct |
16 |
Correct |
5 ms |
9820 KB |
Output is correct |
17 |
Correct |
7 ms |
9820 KB |
Output is correct |
18 |
Correct |
6 ms |
10020 KB |
Output is correct |
19 |
Correct |
6 ms |
9820 KB |
Output is correct |
20 |
Correct |
6 ms |
9904 KB |
Output is correct |
21 |
Correct |
8 ms |
10064 KB |
Output is correct |
22 |
Correct |
5 ms |
9856 KB |
Output is correct |
23 |
Correct |
6 ms |
10072 KB |
Output is correct |
24 |
Correct |
5 ms |
9820 KB |
Output is correct |
25 |
Correct |
5 ms |
10076 KB |
Output is correct |
26 |
Correct |
6 ms |
9880 KB |
Output is correct |
27 |
Correct |
6 ms |
10076 KB |
Output is correct |
28 |
Correct |
6 ms |
9820 KB |
Output is correct |
29 |
Correct |
6 ms |
9820 KB |
Output is correct |
30 |
Correct |
6 ms |
9816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
302 ms |
32240 KB |
Output is correct |
2 |
Correct |
129 ms |
32252 KB |
Output is correct |
3 |
Correct |
317 ms |
32104 KB |
Output is correct |
4 |
Correct |
128 ms |
32340 KB |
Output is correct |
5 |
Correct |
556 ms |
30804 KB |
Output is correct |
6 |
Correct |
137 ms |
32284 KB |
Output is correct |
7 |
Correct |
1104 ms |
30960 KB |
Output is correct |
8 |
Correct |
1372 ms |
32376 KB |
Output is correct |
9 |
Correct |
1719 ms |
29916 KB |
Output is correct |
10 |
Correct |
2068 ms |
28876 KB |
Output is correct |
11 |
Correct |
1682 ms |
30156 KB |
Output is correct |
12 |
Correct |
1488 ms |
31052 KB |
Output is correct |
13 |
Correct |
1331 ms |
28624 KB |
Output is correct |
14 |
Correct |
1771 ms |
31320 KB |
Output is correct |
15 |
Correct |
1075 ms |
31168 KB |
Output is correct |
16 |
Correct |
1542 ms |
29136 KB |
Output is correct |
17 |
Correct |
1344 ms |
29416 KB |
Output is correct |
18 |
Correct |
1495 ms |
29460 KB |
Output is correct |
19 |
Correct |
992 ms |
30672 KB |
Output is correct |
20 |
Correct |
2589 ms |
31696 KB |
Output is correct |
21 |
Correct |
5 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9864 KB |
Output is correct |
3 |
Correct |
5 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9848 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9656 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
6 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9820 KB |
Output is correct |
13 |
Correct |
5 ms |
9896 KB |
Output is correct |
14 |
Correct |
5 ms |
9816 KB |
Output is correct |
15 |
Correct |
5 ms |
9820 KB |
Output is correct |
16 |
Correct |
5 ms |
9820 KB |
Output is correct |
17 |
Correct |
7 ms |
9820 KB |
Output is correct |
18 |
Correct |
6 ms |
10020 KB |
Output is correct |
19 |
Correct |
6 ms |
9820 KB |
Output is correct |
20 |
Correct |
6 ms |
9904 KB |
Output is correct |
21 |
Correct |
8 ms |
10064 KB |
Output is correct |
22 |
Correct |
5 ms |
9856 KB |
Output is correct |
23 |
Correct |
6 ms |
10072 KB |
Output is correct |
24 |
Correct |
5 ms |
9820 KB |
Output is correct |
25 |
Correct |
5 ms |
10076 KB |
Output is correct |
26 |
Correct |
6 ms |
9880 KB |
Output is correct |
27 |
Correct |
6 ms |
10076 KB |
Output is correct |
28 |
Correct |
6 ms |
9820 KB |
Output is correct |
29 |
Correct |
6 ms |
9820 KB |
Output is correct |
30 |
Correct |
6 ms |
9816 KB |
Output is correct |
31 |
Correct |
302 ms |
32240 KB |
Output is correct |
32 |
Correct |
129 ms |
32252 KB |
Output is correct |
33 |
Correct |
317 ms |
32104 KB |
Output is correct |
34 |
Correct |
128 ms |
32340 KB |
Output is correct |
35 |
Correct |
556 ms |
30804 KB |
Output is correct |
36 |
Correct |
137 ms |
32284 KB |
Output is correct |
37 |
Correct |
1104 ms |
30960 KB |
Output is correct |
38 |
Correct |
1372 ms |
32376 KB |
Output is correct |
39 |
Correct |
1719 ms |
29916 KB |
Output is correct |
40 |
Correct |
2068 ms |
28876 KB |
Output is correct |
41 |
Correct |
1682 ms |
30156 KB |
Output is correct |
42 |
Correct |
1488 ms |
31052 KB |
Output is correct |
43 |
Correct |
1331 ms |
28624 KB |
Output is correct |
44 |
Correct |
1771 ms |
31320 KB |
Output is correct |
45 |
Correct |
1075 ms |
31168 KB |
Output is correct |
46 |
Correct |
1542 ms |
29136 KB |
Output is correct |
47 |
Correct |
1344 ms |
29416 KB |
Output is correct |
48 |
Correct |
1495 ms |
29460 KB |
Output is correct |
49 |
Correct |
992 ms |
30672 KB |
Output is correct |
50 |
Correct |
2589 ms |
31696 KB |
Output is correct |
51 |
Correct |
5 ms |
9820 KB |
Output is correct |
52 |
Correct |
220 ms |
25112 KB |
Output is correct |
53 |
Correct |
236 ms |
25084 KB |
Output is correct |
54 |
Correct |
240 ms |
25264 KB |
Output is correct |
55 |
Correct |
236 ms |
25272 KB |
Output is correct |
56 |
Correct |
271 ms |
25080 KB |
Output is correct |
57 |
Correct |
225 ms |
25032 KB |
Output is correct |
58 |
Correct |
237 ms |
26576 KB |
Output is correct |
59 |
Correct |
207 ms |
26400 KB |
Output is correct |
60 |
Correct |
236 ms |
26452 KB |
Output is correct |
61 |
Correct |
271 ms |
26448 KB |
Output is correct |
62 |
Correct |
124 ms |
32304 KB |
Output is correct |
63 |
Correct |
125 ms |
32336 KB |
Output is correct |
64 |
Correct |
848 ms |
32460 KB |
Output is correct |
65 |
Correct |
141 ms |
32340 KB |
Output is correct |
66 |
Execution timed out |
3044 ms |
29060 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |