#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;
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2")
#define setp(x) cout<<fixed<<setprecision(x)
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define forn(i,a,b) for(int i=(a);i<(b);i++)
#define fore(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
#define F first
#define S second
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
//template<typename T>
//using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void SD(int t=0){ cout<<"PASSED "<<t<<endl; }
template<typename T> void amax(T &a, T b){ a=max(a,b); }
template<typename T> void amin(T &a, T b){ a=min(a,b); }
struct Hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
const ll INF = ll(1e18);
const int MOD = 998244353;
const bool DEBUG = 0;
const int MAXN = 100005;
const int LG = 21;
int n,K;
vector<vector<int>> adj;
vector<int> c, sub, cnt;
vector<bool> cvst;
void dfs_sz(int u, int p)
{
sub[u] = 1;
for (const auto v : adj[u])
{
if (v == p || cvst[v]) continue;
dfs_sz(v, u);
sub[u] += sub[v];
}
}
int centroid(int u, int p, int r)
{
for (int v : adj[u])
{
if (v == p || cvst[v]) continue;
if (sub[v] * 2 > sub[r]) return centroid(v, u, r);
}
return u;
}
vector<int> s;
vector<int> scnt;
vector<bool> need;
vector<int> needv;
vector<vector<int>> prt;
void dfs(int u, int p)
{
s.pb(c[u]);
scnt[c[u]]++;
if (p != -1) prt[c[u]].pb(c[p]);
for (const auto v : adj[u])
{
if (v == p || cvst[v]) continue;
dfs(v, u);
}
}
int ans = 1e9;
void solve(int u)
{
dfs_sz(u, -1);
u = centroid(u, -1, u);
for (const auto x : s)
{
scnt[x] = 0;
prt[x].clear();
need[x] = 0;
}
s.clear();
needv.clear();
dfs(u, -1);
queue<int> q;
q.push(c[u]);
needv.pb(c[u]);
need[c[u]] = 1;
while (!q.empty())
{
int curc = q.front(); q.pop();
for (const auto x : prt[curc])
{
if (need[x]) continue;
need[x] = 1;
needv.pb(x);
q.push(x);
}
}
bool ok = 1;
for (const auto x : needv)
{
if (cnt[x] != scnt[x])
{
ok = 0;
break;
}
}
if (ok) amin(ans, sz(needv) - 1);
cvst[u] = 1;
for (const auto v : adj[u])
{
if (cvst[v]) continue;
solve(v);
}
}
void init()
{
adj.resize(n);
c.resize(n);
cvst.resize(n);
sub.resize(n);
cnt.resize(K);
scnt.resize(K);
prt.resize(K);
need.resize(K);
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin>>n>>K;
init();
forn(i,0,n-1)
{
int u,v; cin>>u>>v; u--; v--;
adj[u].pb(v);
adj[v].pb(u);
}
forn(i,0,n)
{
cin>>c[i];
c[i]--;
cnt[c[i]]++;
}
solve(0);
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
255 ms |
32204 KB |
Output is correct |
2 |
Correct |
130 ms |
32712 KB |
Output is correct |
3 |
Correct |
273 ms |
31992 KB |
Output is correct |
4 |
Correct |
126 ms |
32712 KB |
Output is correct |
5 |
Correct |
252 ms |
29384 KB |
Output is correct |
6 |
Correct |
132 ms |
32712 KB |
Output is correct |
7 |
Correct |
250 ms |
29384 KB |
Output is correct |
8 |
Correct |
140 ms |
31692 KB |
Output is correct |
9 |
Correct |
298 ms |
26564 KB |
Output is correct |
10 |
Correct |
300 ms |
24776 KB |
Output is correct |
11 |
Correct |
309 ms |
26984 KB |
Output is correct |
12 |
Correct |
304 ms |
29184 KB |
Output is correct |
13 |
Correct |
299 ms |
24516 KB |
Output is correct |
14 |
Correct |
304 ms |
29380 KB |
Output is correct |
15 |
Correct |
302 ms |
29124 KB |
Output is correct |
16 |
Correct |
307 ms |
25028 KB |
Output is correct |
17 |
Correct |
295 ms |
25544 KB |
Output is correct |
18 |
Correct |
299 ms |
25700 KB |
Output is correct |
19 |
Correct |
306 ms |
28356 KB |
Output is correct |
20 |
Correct |
294 ms |
30048 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
255 ms |
32204 KB |
Output is correct |
32 |
Correct |
130 ms |
32712 KB |
Output is correct |
33 |
Correct |
273 ms |
31992 KB |
Output is correct |
34 |
Correct |
126 ms |
32712 KB |
Output is correct |
35 |
Correct |
252 ms |
29384 KB |
Output is correct |
36 |
Correct |
132 ms |
32712 KB |
Output is correct |
37 |
Correct |
250 ms |
29384 KB |
Output is correct |
38 |
Correct |
140 ms |
31692 KB |
Output is correct |
39 |
Correct |
298 ms |
26564 KB |
Output is correct |
40 |
Correct |
300 ms |
24776 KB |
Output is correct |
41 |
Correct |
309 ms |
26984 KB |
Output is correct |
42 |
Correct |
304 ms |
29184 KB |
Output is correct |
43 |
Correct |
299 ms |
24516 KB |
Output is correct |
44 |
Correct |
304 ms |
29380 KB |
Output is correct |
45 |
Correct |
302 ms |
29124 KB |
Output is correct |
46 |
Correct |
307 ms |
25028 KB |
Output is correct |
47 |
Correct |
295 ms |
25544 KB |
Output is correct |
48 |
Correct |
299 ms |
25700 KB |
Output is correct |
49 |
Correct |
306 ms |
28356 KB |
Output is correct |
50 |
Correct |
294 ms |
30048 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
203 ms |
16328 KB |
Output is correct |
53 |
Correct |
217 ms |
16328 KB |
Output is correct |
54 |
Correct |
220 ms |
16184 KB |
Output is correct |
55 |
Correct |
207 ms |
16528 KB |
Output is correct |
56 |
Correct |
218 ms |
16376 KB |
Output is correct |
57 |
Correct |
211 ms |
16324 KB |
Output is correct |
58 |
Correct |
197 ms |
20724 KB |
Output is correct |
59 |
Correct |
205 ms |
21188 KB |
Output is correct |
60 |
Correct |
253 ms |
21028 KB |
Output is correct |
61 |
Correct |
280 ms |
20664 KB |
Output is correct |
62 |
Correct |
162 ms |
32716 KB |
Output is correct |
63 |
Correct |
124 ms |
32712 KB |
Output is correct |
64 |
Correct |
138 ms |
31948 KB |
Output is correct |
65 |
Correct |
127 ms |
32736 KB |
Output is correct |
66 |
Correct |
163 ms |
24260 KB |
Output is correct |
67 |
Correct |
171 ms |
24112 KB |
Output is correct |
68 |
Correct |
152 ms |
24192 KB |
Output is correct |
69 |
Correct |
162 ms |
24268 KB |
Output is correct |
70 |
Correct |
172 ms |
24192 KB |
Output is correct |
71 |
Correct |
153 ms |
24188 KB |
Output is correct |
72 |
Correct |
147 ms |
28236 KB |
Output is correct |
73 |
Correct |
148 ms |
27336 KB |
Output is correct |
74 |
Correct |
155 ms |
28108 KB |
Output is correct |
75 |
Correct |
152 ms |
27852 KB |
Output is correct |
76 |
Correct |
222 ms |
28360 KB |
Output is correct |
77 |
Correct |
216 ms |
26784 KB |
Output is correct |
78 |
Correct |
307 ms |
29380 KB |
Output is correct |
79 |
Correct |
282 ms |
27592 KB |
Output is correct |
80 |
Correct |
291 ms |
33220 KB |
Output is correct |
81 |
Correct |
295 ms |
30408 KB |
Output is correct |
82 |
Correct |
303 ms |
30660 KB |
Output is correct |
83 |
Correct |
289 ms |
27844 KB |
Output is correct |
84 |
Correct |
313 ms |
32712 KB |
Output is correct |
85 |
Correct |
303 ms |
31432 KB |
Output is correct |
86 |
Correct |
270 ms |
27848 KB |
Output is correct |
87 |
Correct |
293 ms |
29020 KB |
Output is correct |
88 |
Correct |
247 ms |
29844 KB |
Output is correct |
89 |
Correct |
254 ms |
26564 KB |
Output is correct |
90 |
Correct |
260 ms |
26832 KB |
Output is correct |
91 |
Correct |
275 ms |
28356 KB |
Output is correct |
92 |
Correct |
271 ms |
27152 KB |
Output is correct |
93 |
Correct |
287 ms |
27080 KB |
Output is correct |
94 |
Correct |
265 ms |
26468 KB |
Output is correct |
95 |
Correct |
283 ms |
28048 KB |
Output is correct |
96 |
Correct |
272 ms |
26600 KB |
Output is correct |
97 |
Correct |
273 ms |
28356 KB |
Output is correct |