/* nya */
#pragma GCC optimize("O4,unroll-loops,no-stack-protector")
#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma")
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = a; i <= b; i++)
#define Forr(i, a, b) for (int i = a; i >= b; i--)
#define Fors(i, a, b, s) for (int i = a; i <= b; i += s)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
#define eb emplace_back
#define int LL
#define INF (int)(1e18)
#define MOD (int)(1000000007)
// #define MOD (int)(998244353)
using namespace std;
using LL = long long;
// using LLL = __int128_t;
using pii = pair<int, int>;
#ifdef NYAOWO
#include "library/debug.hpp"
inline void USACO(const string &s) {
cerr << "USACO: " << s << "\n";
}
#else
#define debug(...)
inline void timer() {}
inline void USACO(const string &s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
#endif
const int MAXN = 200020;
mt19937_64 rng(48763);
vector<int> adj[MAXN];
int vis[MAXN]; // -1 for expired nodes
int par[MAXN];
int ssz[MAXN];
int city[MAXN]; // town -> city
vector<int> town[MAXN]; // city -> towns
int vist[MAXN];
int dfs(int n, int p, int tag) {
vis[n] = tag;
par[n] = p;
int sz = 1;
for(auto &i:adj[n]) if(vis[i] != -1 && vis[i] != tag) {
sz += dfs(i, n, tag);
}
ssz[n] = sz;
return sz;
}
// {size, index}
pii cent(int n, int p, int N) {
pii res = {INF, -1};
int mx = 0;
for(auto &i:adj[n]) if(i != p && vis[i] >= 0) {
res = min(res, cent(i, n, N));
mx = max(mx, ssz[i]);
}
mx = max(mx, N - ssz[n]);
res = min(res, pii(mx, n));
return res;
}
int nya(int n, int siz) {
int tag = rng();
n = cent(n, n, siz).S;
dfs(n, n, tag);
queue<int> que;
int res = 0;
auto mark = [&](int t) {
// cout << "mark: " << t << "\n";
if(vis[t] != (~tag) && vis[t] != tag) return false;
while(vis[t] == tag) {
vis[t] = (~tag);
if(vist[city[t]] != tag) {
vist[city[t]] = tag;
que.emplace(city[t]);
}
if(t == n) break;
t = par[t];
}
return true;
};
mark(n);
while(!que.empty()) {
int c = que.front(); que.pop();
// cout << "city: " << c << "\n";
res++;
for(auto &i:town[c]) if(vis[i] != (~tag)) {
if(!mark(i)) {
res = INF;
break;
}
}
}
vis[n] = -1;
for(auto &i:adj[n]) if(vis[i] != -1) {
res = min(res, nya(i, dfs(i, i, rng())));
}
return res;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
// OAO
int n, m; cin >> n >> m;
For(i, 1, n - 1) {
int a, b; cin >> a >> b;
adj[a].eb(b);
adj[b].eb(a);
}
For(i, 1, n) {
cin >> city[i];
town[city[i]].eb(i);
}
dfs(1, 1, 49);
cout << (nya(1, n) - 1) << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9732 KB |
Output is correct |
3 |
Correct |
7 ms |
9684 KB |
Output is correct |
4 |
Correct |
7 ms |
9732 KB |
Output is correct |
5 |
Correct |
6 ms |
9732 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9744 KB |
Output is correct |
9 |
Correct |
6 ms |
9732 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9732 KB |
Output is correct |
3 |
Correct |
7 ms |
9684 KB |
Output is correct |
4 |
Correct |
7 ms |
9732 KB |
Output is correct |
5 |
Correct |
6 ms |
9732 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9744 KB |
Output is correct |
9 |
Correct |
6 ms |
9732 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
8 ms |
9928 KB |
Output is correct |
12 |
Correct |
7 ms |
9840 KB |
Output is correct |
13 |
Correct |
7 ms |
9876 KB |
Output is correct |
14 |
Correct |
8 ms |
9940 KB |
Output is correct |
15 |
Correct |
8 ms |
10000 KB |
Output is correct |
16 |
Correct |
8 ms |
9940 KB |
Output is correct |
17 |
Correct |
7 ms |
10000 KB |
Output is correct |
18 |
Correct |
10 ms |
10032 KB |
Output is correct |
19 |
Correct |
13 ms |
10112 KB |
Output is correct |
20 |
Correct |
8 ms |
10044 KB |
Output is correct |
21 |
Correct |
7 ms |
9940 KB |
Output is correct |
22 |
Correct |
11 ms |
10144 KB |
Output is correct |
23 |
Correct |
8 ms |
9940 KB |
Output is correct |
24 |
Correct |
7 ms |
9996 KB |
Output is correct |
25 |
Correct |
10 ms |
9996 KB |
Output is correct |
26 |
Correct |
9 ms |
9996 KB |
Output is correct |
27 |
Correct |
9 ms |
10068 KB |
Output is correct |
28 |
Correct |
9 ms |
9940 KB |
Output is correct |
29 |
Correct |
8 ms |
10000 KB |
Output is correct |
30 |
Correct |
8 ms |
9996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
842 ms |
48080 KB |
Output is correct |
2 |
Correct |
270 ms |
48616 KB |
Output is correct |
3 |
Correct |
811 ms |
47640 KB |
Output is correct |
4 |
Correct |
270 ms |
48660 KB |
Output is correct |
5 |
Correct |
847 ms |
44252 KB |
Output is correct |
6 |
Correct |
266 ms |
48588 KB |
Output is correct |
7 |
Correct |
770 ms |
44680 KB |
Output is correct |
8 |
Correct |
259 ms |
48348 KB |
Output is correct |
9 |
Correct |
1004 ms |
42800 KB |
Output is correct |
10 |
Correct |
975 ms |
39792 KB |
Output is correct |
11 |
Correct |
962 ms |
43404 KB |
Output is correct |
12 |
Correct |
1038 ms |
46412 KB |
Output is correct |
13 |
Correct |
1020 ms |
39272 KB |
Output is correct |
14 |
Correct |
1051 ms |
46872 KB |
Output is correct |
15 |
Correct |
1146 ms |
46428 KB |
Output is correct |
16 |
Correct |
1077 ms |
40436 KB |
Output is correct |
17 |
Correct |
1016 ms |
41248 KB |
Output is correct |
18 |
Correct |
1069 ms |
41408 KB |
Output is correct |
19 |
Correct |
1040 ms |
45172 KB |
Output is correct |
20 |
Correct |
1064 ms |
47704 KB |
Output is correct |
21 |
Correct |
6 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9732 KB |
Output is correct |
3 |
Correct |
7 ms |
9684 KB |
Output is correct |
4 |
Correct |
7 ms |
9732 KB |
Output is correct |
5 |
Correct |
6 ms |
9732 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9744 KB |
Output is correct |
9 |
Correct |
6 ms |
9732 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
8 ms |
9928 KB |
Output is correct |
12 |
Correct |
7 ms |
9840 KB |
Output is correct |
13 |
Correct |
7 ms |
9876 KB |
Output is correct |
14 |
Correct |
8 ms |
9940 KB |
Output is correct |
15 |
Correct |
8 ms |
10000 KB |
Output is correct |
16 |
Correct |
8 ms |
9940 KB |
Output is correct |
17 |
Correct |
7 ms |
10000 KB |
Output is correct |
18 |
Correct |
10 ms |
10032 KB |
Output is correct |
19 |
Correct |
13 ms |
10112 KB |
Output is correct |
20 |
Correct |
8 ms |
10044 KB |
Output is correct |
21 |
Correct |
7 ms |
9940 KB |
Output is correct |
22 |
Correct |
11 ms |
10144 KB |
Output is correct |
23 |
Correct |
8 ms |
9940 KB |
Output is correct |
24 |
Correct |
7 ms |
9996 KB |
Output is correct |
25 |
Correct |
10 ms |
9996 KB |
Output is correct |
26 |
Correct |
9 ms |
9996 KB |
Output is correct |
27 |
Correct |
9 ms |
10068 KB |
Output is correct |
28 |
Correct |
9 ms |
9940 KB |
Output is correct |
29 |
Correct |
8 ms |
10000 KB |
Output is correct |
30 |
Correct |
8 ms |
9996 KB |
Output is correct |
31 |
Correct |
842 ms |
48080 KB |
Output is correct |
32 |
Correct |
270 ms |
48616 KB |
Output is correct |
33 |
Correct |
811 ms |
47640 KB |
Output is correct |
34 |
Correct |
270 ms |
48660 KB |
Output is correct |
35 |
Correct |
847 ms |
44252 KB |
Output is correct |
36 |
Correct |
266 ms |
48588 KB |
Output is correct |
37 |
Correct |
770 ms |
44680 KB |
Output is correct |
38 |
Correct |
259 ms |
48348 KB |
Output is correct |
39 |
Correct |
1004 ms |
42800 KB |
Output is correct |
40 |
Correct |
975 ms |
39792 KB |
Output is correct |
41 |
Correct |
962 ms |
43404 KB |
Output is correct |
42 |
Correct |
1038 ms |
46412 KB |
Output is correct |
43 |
Correct |
1020 ms |
39272 KB |
Output is correct |
44 |
Correct |
1051 ms |
46872 KB |
Output is correct |
45 |
Correct |
1146 ms |
46428 KB |
Output is correct |
46 |
Correct |
1077 ms |
40436 KB |
Output is correct |
47 |
Correct |
1016 ms |
41248 KB |
Output is correct |
48 |
Correct |
1069 ms |
41408 KB |
Output is correct |
49 |
Correct |
1040 ms |
45172 KB |
Output is correct |
50 |
Correct |
1064 ms |
47704 KB |
Output is correct |
51 |
Correct |
6 ms |
9684 KB |
Output is correct |
52 |
Correct |
526 ms |
30096 KB |
Output is correct |
53 |
Correct |
586 ms |
30156 KB |
Output is correct |
54 |
Correct |
567 ms |
29876 KB |
Output is correct |
55 |
Correct |
577 ms |
30028 KB |
Output is correct |
56 |
Correct |
620 ms |
30000 KB |
Output is correct |
57 |
Correct |
545 ms |
29960 KB |
Output is correct |
58 |
Correct |
568 ms |
34448 KB |
Output is correct |
59 |
Correct |
622 ms |
34968 KB |
Output is correct |
60 |
Correct |
768 ms |
33300 KB |
Output is correct |
61 |
Correct |
817 ms |
32936 KB |
Output is correct |
62 |
Correct |
268 ms |
48524 KB |
Output is correct |
63 |
Correct |
248 ms |
48608 KB |
Output is correct |
64 |
Correct |
264 ms |
48420 KB |
Output is correct |
65 |
Correct |
259 ms |
48700 KB |
Output is correct |
66 |
Correct |
542 ms |
39212 KB |
Output is correct |
67 |
Correct |
557 ms |
39096 KB |
Output is correct |
68 |
Correct |
543 ms |
39244 KB |
Output is correct |
69 |
Correct |
534 ms |
39228 KB |
Output is correct |
70 |
Correct |
590 ms |
39116 KB |
Output is correct |
71 |
Correct |
500 ms |
39000 KB |
Output is correct |
72 |
Correct |
521 ms |
39124 KB |
Output is correct |
73 |
Correct |
527 ms |
38124 KB |
Output is correct |
74 |
Correct |
542 ms |
39268 KB |
Output is correct |
75 |
Correct |
594 ms |
39168 KB |
Output is correct |
76 |
Correct |
776 ms |
38216 KB |
Output is correct |
77 |
Correct |
778 ms |
36080 KB |
Output is correct |
78 |
Correct |
1079 ms |
41152 KB |
Output is correct |
79 |
Correct |
1037 ms |
38672 KB |
Output is correct |
80 |
Correct |
1083 ms |
47212 KB |
Output is correct |
81 |
Correct |
999 ms |
42908 KB |
Output is correct |
82 |
Correct |
1049 ms |
43212 KB |
Output is correct |
83 |
Correct |
1020 ms |
39124 KB |
Output is correct |
84 |
Correct |
959 ms |
46156 KB |
Output is correct |
85 |
Correct |
1129 ms |
44332 KB |
Output is correct |
86 |
Correct |
1124 ms |
38544 KB |
Output is correct |
87 |
Correct |
1044 ms |
40804 KB |
Output is correct |
88 |
Correct |
895 ms |
42596 KB |
Output is correct |
89 |
Correct |
867 ms |
37676 KB |
Output is correct |
90 |
Correct |
988 ms |
37428 KB |
Output is correct |
91 |
Correct |
922 ms |
40280 KB |
Output is correct |
92 |
Correct |
906 ms |
38700 KB |
Output is correct |
93 |
Correct |
912 ms |
38316 KB |
Output is correct |
94 |
Correct |
864 ms |
37544 KB |
Output is correct |
95 |
Correct |
920 ms |
39356 KB |
Output is correct |
96 |
Correct |
836 ms |
37876 KB |
Output is correct |
97 |
Correct |
885 ms |
40392 KB |
Output is correct |