#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
int n, m;
vector<vector<int>> g, g2, r2;
vector<int> C;
vector<int> top, id, sz, dpt, pr;
vector<int> gv;
vector<vector<int>> cv;
vector<bool> vst;
int hldid = 1;
vector<int> hld;
vector<int> scc, cnt;
int sccid = 1;
void init(){
cv.resize(m + 1);
C.resize(n + 1);
g.resize(n + 1);
top.resize(n + 1);
id.resize(n + 1);
sz.resize(n + 1);
dpt.resize(n + 1);
pr.resize(n + 1);
gv.resize(4 * n);
g2.resize(1 + m + 4 * n);
r2.resize(1 + m + 4 * n);
vst.resize(1 + m + 4 * n);
hld.resize(n + 1);
scc.resize(1 + m + 4 * n);
cnt.resize(1 + m + 4 * n);
}
void dfssz(int now, int p){
sz[now] = 1;
dpt[now] = dpt[p] + 1;
pr[now] = p;
for(int i : g[now]){
if(i == p) continue;
dfssz(i, now);
sz[now] += sz[i];
}
}
void dfstop(int now, int p, int t){
if(t == -1) t = now;
top[now] = t;
int mx = -1;
id[now] = hldid++;
hld[id[now]] = now;
for(int i : g[now]){
if(i == p) continue;
if(mx == -1 || sz[i] > sz[mx]) mx = i;
}
if(mx != -1) dfstop(mx, now, t);
for(int i : g[now]){
if(i == p || i == mx) continue;
dfstop(i, now, -1);
}
}
#define lc (2 * id + 1)
#define rc (2 * id + 2)
void buildsgt(int L = 1, int R = n, int id = 0){
if(L == R){
gv[id] = C[hld[L]];
return;
}
gv[id] = m + 1 + id;
int M = (L + R) / 2;
buildsgt(L, M, lc);
buildsgt(M + 1, R, rc);
g2[gv[id]].eb(gv[lc]);
g2[gv[id]].eb(gv[rc]);
}
void modify(int l, int r, int v, int L = 1, int R = n, int id = 0){
if(l <= L && R <= r){
g2[v].eb(gv[id]);
return;
}
int M = (L + R) / 2;
if(r <= M) modify(l, r, v, L, M, lc);
else if(l > M) modify(l, r, v, M + 1, R, rc);
else{
modify(l, r, v, L, M, lc);
modify(l, r, v, M + 1, R, rc);
}
}
void solve(int a, int b, int c){
//cerr << "solve2 " << a << " " << b << " " << c << "\n";
while(top[a] != top[b]){
if(dpt[top[a]] < dpt[top[b]]) swap(a, b);
modify(id[top[a]], id[a], c);
//cerr << top[a] << " " << a << "\n";
a = pr[top[a]];
}
if(dpt[a] < dpt[b]) swap(a, b);
modify(id[b], id[a], c);
//cerr << b << " " << a << "\n";
}
void solve(int c){
//cerr << "solve " << c << "\n";
int lst = -1;
for(int i : cv[c]){
if(lst == -1){
lst = i;
continue;
}
solve(lst, i, c);
lst = i;
}
}
vector<int> tmp;
void dfs1(int now){
vst[now] = true;
for(int i : r2[now]){
if(vst[i]) continue;
dfs1(i);
}
tmp.eb(now);
}
void dfs2(int now, int id){
scc[now] = id;
for(int i : g2[now]){
if(scc[i]) continue;
dfs2(i, id);
}
}
int main(){
StarBurstStream
cin >> n >> m;
init();
for(int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
g[u].eb(v);
g[v].eb(u);
}
for(int i = 1; i <= n; i++) cin >> C[i], cv[C[i]].eb(i);
dfssz(1, 1);
dfstop(1, 1, -1);
buildsgt();
//cerr << "top "; printv(top, cerr);
//cerr << "id "; printv(id, cerr);
for(int i = 1; i <= m; i++) solve(i);
for(int i = 1; i <= m + 4 * n; i++){
for(int j : g2[i]) r2[j].eb(i);
}
for(int i = 1; i <= m; i++) if(!vst[i]) dfs1(i);
reverse(iter(tmp));
for(int i : tmp) if(!scc[i]) dfs2(i, sccid++);
for(int i = 1; i <= m; i++){
cnt[scc[i]]++;
}
//printv(scc, cerr);
vector<bool> ok(1 + m + 4 * n, true);
for(int i = 1; i <= m + 4 * n; i++){
for(int j : g2[i]){
if(scc[i] != scc[j]) ok[scc[i]] = false;
}
}
int ans = m;
for(int i = 1; i <= m; i++){
if(!ok[scc[i]]) continue;
ans = min(ans, cnt[scc[i]] - 1);
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
4 ms |
1364 KB |
Output is correct |
12 |
Correct |
4 ms |
1364 KB |
Output is correct |
13 |
Correct |
4 ms |
1364 KB |
Output is correct |
14 |
Correct |
4 ms |
1364 KB |
Output is correct |
15 |
Correct |
4 ms |
1364 KB |
Output is correct |
16 |
Correct |
4 ms |
1364 KB |
Output is correct |
17 |
Correct |
3 ms |
1364 KB |
Output is correct |
18 |
Correct |
3 ms |
1380 KB |
Output is correct |
19 |
Correct |
3 ms |
1352 KB |
Output is correct |
20 |
Correct |
3 ms |
1364 KB |
Output is correct |
21 |
Correct |
4 ms |
1352 KB |
Output is correct |
22 |
Correct |
4 ms |
1492 KB |
Output is correct |
23 |
Correct |
3 ms |
1484 KB |
Output is correct |
24 |
Correct |
3 ms |
1236 KB |
Output is correct |
25 |
Correct |
4 ms |
1364 KB |
Output is correct |
26 |
Correct |
3 ms |
1364 KB |
Output is correct |
27 |
Correct |
3 ms |
1396 KB |
Output is correct |
28 |
Correct |
3 ms |
1364 KB |
Output is correct |
29 |
Correct |
3 ms |
1364 KB |
Output is correct |
30 |
Correct |
3 ms |
1320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
554 ms |
125792 KB |
Output is correct |
2 |
Correct |
283 ms |
125004 KB |
Output is correct |
3 |
Correct |
552 ms |
123736 KB |
Output is correct |
4 |
Correct |
289 ms |
125112 KB |
Output is correct |
5 |
Correct |
507 ms |
122000 KB |
Output is correct |
6 |
Correct |
283 ms |
124580 KB |
Output is correct |
7 |
Correct |
460 ms |
121532 KB |
Output is correct |
8 |
Correct |
285 ms |
119404 KB |
Output is correct |
9 |
Correct |
400 ms |
112196 KB |
Output is correct |
10 |
Correct |
439 ms |
109152 KB |
Output is correct |
11 |
Correct |
457 ms |
112904 KB |
Output is correct |
12 |
Correct |
459 ms |
113260 KB |
Output is correct |
13 |
Correct |
401 ms |
108768 KB |
Output is correct |
14 |
Correct |
441 ms |
114352 KB |
Output is correct |
15 |
Correct |
444 ms |
113044 KB |
Output is correct |
16 |
Correct |
403 ms |
110372 KB |
Output is correct |
17 |
Correct |
453 ms |
110324 KB |
Output is correct |
18 |
Correct |
406 ms |
112144 KB |
Output is correct |
19 |
Correct |
511 ms |
113092 KB |
Output is correct |
20 |
Correct |
429 ms |
114504 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
4 ms |
1364 KB |
Output is correct |
12 |
Correct |
4 ms |
1364 KB |
Output is correct |
13 |
Correct |
4 ms |
1364 KB |
Output is correct |
14 |
Correct |
4 ms |
1364 KB |
Output is correct |
15 |
Correct |
4 ms |
1364 KB |
Output is correct |
16 |
Correct |
4 ms |
1364 KB |
Output is correct |
17 |
Correct |
3 ms |
1364 KB |
Output is correct |
18 |
Correct |
3 ms |
1380 KB |
Output is correct |
19 |
Correct |
3 ms |
1352 KB |
Output is correct |
20 |
Correct |
3 ms |
1364 KB |
Output is correct |
21 |
Correct |
4 ms |
1352 KB |
Output is correct |
22 |
Correct |
4 ms |
1492 KB |
Output is correct |
23 |
Correct |
3 ms |
1484 KB |
Output is correct |
24 |
Correct |
3 ms |
1236 KB |
Output is correct |
25 |
Correct |
4 ms |
1364 KB |
Output is correct |
26 |
Correct |
3 ms |
1364 KB |
Output is correct |
27 |
Correct |
3 ms |
1396 KB |
Output is correct |
28 |
Correct |
3 ms |
1364 KB |
Output is correct |
29 |
Correct |
3 ms |
1364 KB |
Output is correct |
30 |
Correct |
3 ms |
1320 KB |
Output is correct |
31 |
Correct |
554 ms |
125792 KB |
Output is correct |
32 |
Correct |
283 ms |
125004 KB |
Output is correct |
33 |
Correct |
552 ms |
123736 KB |
Output is correct |
34 |
Correct |
289 ms |
125112 KB |
Output is correct |
35 |
Correct |
507 ms |
122000 KB |
Output is correct |
36 |
Correct |
283 ms |
124580 KB |
Output is correct |
37 |
Correct |
460 ms |
121532 KB |
Output is correct |
38 |
Correct |
285 ms |
119404 KB |
Output is correct |
39 |
Correct |
400 ms |
112196 KB |
Output is correct |
40 |
Correct |
439 ms |
109152 KB |
Output is correct |
41 |
Correct |
457 ms |
112904 KB |
Output is correct |
42 |
Correct |
459 ms |
113260 KB |
Output is correct |
43 |
Correct |
401 ms |
108768 KB |
Output is correct |
44 |
Correct |
441 ms |
114352 KB |
Output is correct |
45 |
Correct |
444 ms |
113044 KB |
Output is correct |
46 |
Correct |
403 ms |
110372 KB |
Output is correct |
47 |
Correct |
453 ms |
110324 KB |
Output is correct |
48 |
Correct |
406 ms |
112144 KB |
Output is correct |
49 |
Correct |
511 ms |
113092 KB |
Output is correct |
50 |
Correct |
429 ms |
114504 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
642 ms |
113828 KB |
Output is correct |
53 |
Correct |
641 ms |
114616 KB |
Output is correct |
54 |
Correct |
700 ms |
114812 KB |
Output is correct |
55 |
Correct |
681 ms |
114328 KB |
Output is correct |
56 |
Correct |
764 ms |
114724 KB |
Output is correct |
57 |
Correct |
666 ms |
114112 KB |
Output is correct |
58 |
Correct |
480 ms |
108984 KB |
Output is correct |
59 |
Correct |
482 ms |
109712 KB |
Output is correct |
60 |
Correct |
549 ms |
115244 KB |
Output is correct |
61 |
Correct |
569 ms |
117396 KB |
Output is correct |
62 |
Correct |
291 ms |
125796 KB |
Output is correct |
63 |
Correct |
287 ms |
125760 KB |
Output is correct |
64 |
Correct |
308 ms |
121652 KB |
Output is correct |
65 |
Correct |
275 ms |
125160 KB |
Output is correct |
66 |
Correct |
416 ms |
114604 KB |
Output is correct |
67 |
Correct |
383 ms |
114016 KB |
Output is correct |
68 |
Correct |
410 ms |
114620 KB |
Output is correct |
69 |
Correct |
410 ms |
114584 KB |
Output is correct |
70 |
Correct |
374 ms |
114412 KB |
Output is correct |
71 |
Correct |
409 ms |
114548 KB |
Output is correct |
72 |
Correct |
369 ms |
114648 KB |
Output is correct |
73 |
Correct |
444 ms |
114804 KB |
Output is correct |
74 |
Correct |
369 ms |
114620 KB |
Output is correct |
75 |
Correct |
445 ms |
114120 KB |
Output is correct |
76 |
Correct |
339 ms |
103476 KB |
Output is correct |
77 |
Correct |
411 ms |
102004 KB |
Output is correct |
78 |
Correct |
431 ms |
111188 KB |
Output is correct |
79 |
Correct |
436 ms |
108528 KB |
Output is correct |
80 |
Correct |
478 ms |
114892 KB |
Output is correct |
81 |
Correct |
438 ms |
113516 KB |
Output is correct |
82 |
Correct |
429 ms |
112304 KB |
Output is correct |
83 |
Correct |
419 ms |
110120 KB |
Output is correct |
84 |
Correct |
461 ms |
114036 KB |
Output is correct |
85 |
Correct |
455 ms |
113664 KB |
Output is correct |
86 |
Correct |
398 ms |
108528 KB |
Output is correct |
87 |
Correct |
459 ms |
110944 KB |
Output is correct |
88 |
Correct |
451 ms |
112932 KB |
Output is correct |
89 |
Correct |
424 ms |
110284 KB |
Output is correct |
90 |
Correct |
453 ms |
111424 KB |
Output is correct |
91 |
Correct |
439 ms |
112940 KB |
Output is correct |
92 |
Correct |
480 ms |
110840 KB |
Output is correct |
93 |
Correct |
398 ms |
110564 KB |
Output is correct |
94 |
Correct |
471 ms |
110156 KB |
Output is correct |
95 |
Correct |
419 ms |
111092 KB |
Output is correct |
96 |
Correct |
456 ms |
110136 KB |
Output is correct |
97 |
Correct |
477 ms |
115972 KB |
Output is correct |