/// In The Name Of God
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define pp pop_back
#define mp make_pair
#define sz(x) (int)x.size()
#define sqr(x) ((x) * 1ll * (x))
#define all(x) x.begin(), x.end()
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, l, r) for (int i = (l); i >= (r); i--)
#define Kazakhstan ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define nl '\n'
#define ioi exit(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = (int)2e5 + 7;
const int inf = (int)1e9 + 7;
const int mod = (int)1e9 + 7;
const ll linf = (ll)1e18 + 7;
const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
using namespace std;
int n, m;
int a[N];
vector <int> g[N];
struct S {
int sz, df;
int st[N], cnt[N];
void add(int c) {
df += cnt[c]++ == 0;
}
void del(int c) {
df -= --cnt[c] == 0;
}
void push(int v) {
st[++sz] = v;
add(a[v]);
}
void pop() {
del(a[st[sz--]]);
}
bool empty() {
return sz == 0;
}
int back() {
return st[sz];
}
} T;
void dfs(int v, int p, vector <int> &dis) {
dis[v] = dis[p] + 1;
for (auto to : g[v]) {
if (to != p) dfs(to, v, dis);
}
}
int d[N], ans[N];
void prec(int v, int p) {
d[v] = 1;
for (auto to : g[v]) {
if (to != p) {
prec(to, v);
d[v] = max(d[v], d[to] + 1);
}
}
}
void solve(int v, int p, vector <int> &dep) {
vector < pair <int, int > > ch;
for (auto to : g[v]) {
if (to != p) {
ch.pb({d[to], to});
}
}
if (sz(ch)) swap(ch[0], *max_element(all(ch)));
int len = (sz(ch) > 1) ? max_element(ch.begin() + 1, ch.end()) -> f : 0;
if (sz(ch)) {
for (auto to : ch) {
while (!T.empty() && dep[T.back()] >= dep[v] - len) T.pop();
T.push(v);
solve(to.s, v, dep);
if (!T.empty() && T.back() == v) T.pop();
len = max(len, to.f);
}
}
while (!T.empty() && dep[T.back()] >= dep[v] - len) T.pop();
// if (v == 1) {
// cerr << T.back() << ' ' << dep[T.back()] << ' ' << dep[v] << nl;
// cerr << len << " -> " << sz(ch) << " -> " << T.sz << " -> ";
// rep(i, 1, T.sz) cerr << T.st[i] << ' ';
// cerr << nl;
// }
ans[v] = max(ans[v], T.df);
}
int main() {
#ifdef IOI
freopen ("in.txt", "r", stdin);
freopen ("res.txt", "w", stdout);
#endif
Kazakhstan
cin >> n >> m;
rep(i, 2, n) {
int v, u;
cin >> v >> u;
g[v].pb(u);
g[u].pb(v);
}
rep(i, 1, n) {
cin >> a[i];
}
vector <int> d1(n + 1);
dfs(1, 0, d1);
int A = max_element(all(d1)) - d1.begin();
prec(A, 0);
dfs(A, 0, d1);
solve(A, 0, d1);
int B = max_element(all(d1)) - d1.begin();
prec(B, 0);
dfs(B, 0, d1);
solve(B, 0, d1);
rep(i, 1, n) {
cout << ans[i] << nl;
}
ioi
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4992 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5248 KB |
Output is correct |
4 |
Correct |
7 ms |
5376 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5504 KB |
Output is correct |
7 |
Correct |
8 ms |
5376 KB |
Output is correct |
8 |
Correct |
8 ms |
5248 KB |
Output is correct |
9 |
Correct |
7 ms |
5248 KB |
Output is correct |
10 |
Correct |
7 ms |
5248 KB |
Output is correct |
11 |
Correct |
8 ms |
5248 KB |
Output is correct |
12 |
Correct |
6 ms |
5248 KB |
Output is correct |
13 |
Correct |
9 ms |
5504 KB |
Output is correct |
14 |
Correct |
8 ms |
5248 KB |
Output is correct |
15 |
Correct |
8 ms |
5248 KB |
Output is correct |
16 |
Correct |
7 ms |
5248 KB |
Output is correct |
17 |
Correct |
9 ms |
5376 KB |
Output is correct |
18 |
Correct |
10 ms |
5376 KB |
Output is correct |
19 |
Correct |
8 ms |
5248 KB |
Output is correct |
20 |
Correct |
8 ms |
5504 KB |
Output is correct |
21 |
Correct |
11 ms |
5376 KB |
Output is correct |
22 |
Correct |
11 ms |
5120 KB |
Output is correct |
23 |
Correct |
9 ms |
5248 KB |
Output is correct |
24 |
Correct |
7 ms |
5248 KB |
Output is correct |
25 |
Correct |
7 ms |
5248 KB |
Output is correct |
26 |
Correct |
7 ms |
5248 KB |
Output is correct |
27 |
Correct |
7 ms |
5504 KB |
Output is correct |
28 |
Correct |
7 ms |
5376 KB |
Output is correct |
29 |
Correct |
7 ms |
5376 KB |
Output is correct |
30 |
Correct |
7 ms |
5248 KB |
Output is correct |
31 |
Correct |
8 ms |
5504 KB |
Output is correct |
32 |
Correct |
7 ms |
5376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
237 ms |
12424 KB |
Output is correct |
2 |
Correct |
341 ms |
30704 KB |
Output is correct |
3 |
Correct |
43 ms |
9080 KB |
Output is correct |
4 |
Correct |
515 ms |
17884 KB |
Output is correct |
5 |
Correct |
688 ms |
50160 KB |
Output is correct |
6 |
Correct |
644 ms |
33736 KB |
Output is correct |
7 |
Correct |
485 ms |
18380 KB |
Output is correct |
8 |
Correct |
525 ms |
21220 KB |
Output is correct |
9 |
Correct |
523 ms |
20020 KB |
Output is correct |
10 |
Correct |
505 ms |
19964 KB |
Output is correct |
11 |
Correct |
204 ms |
21792 KB |
Output is correct |
12 |
Correct |
712 ms |
37880 KB |
Output is correct |
13 |
Correct |
595 ms |
33452 KB |
Output is correct |
14 |
Correct |
614 ms |
32668 KB |
Output is correct |
15 |
Correct |
191 ms |
21792 KB |
Output is correct |
16 |
Correct |
633 ms |
39768 KB |
Output is correct |
17 |
Correct |
642 ms |
33876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
471 ms |
16588 KB |
Output is correct |
2 |
Correct |
790 ms |
50456 KB |
Output is correct |
3 |
Correct |
58 ms |
9848 KB |
Output is correct |
4 |
Correct |
580 ms |
19576 KB |
Output is correct |
5 |
Correct |
711 ms |
52476 KB |
Output is correct |
6 |
Correct |
690 ms |
35560 KB |
Output is correct |
7 |
Correct |
517 ms |
19764 KB |
Output is correct |
8 |
Correct |
580 ms |
26488 KB |
Output is correct |
9 |
Correct |
565 ms |
24224 KB |
Output is correct |
10 |
Correct |
617 ms |
22084 KB |
Output is correct |
11 |
Correct |
412 ms |
21496 KB |
Output is correct |
12 |
Correct |
699 ms |
45688 KB |
Output is correct |
13 |
Correct |
581 ms |
32968 KB |
Output is correct |
14 |
Correct |
711 ms |
33436 KB |
Output is correct |
15 |
Correct |
201 ms |
22848 KB |
Output is correct |
16 |
Correct |
573 ms |
41376 KB |
Output is correct |
17 |
Correct |
605 ms |
35660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4992 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5248 KB |
Output is correct |
4 |
Correct |
7 ms |
5376 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5504 KB |
Output is correct |
7 |
Correct |
8 ms |
5376 KB |
Output is correct |
8 |
Correct |
8 ms |
5248 KB |
Output is correct |
9 |
Correct |
7 ms |
5248 KB |
Output is correct |
10 |
Correct |
7 ms |
5248 KB |
Output is correct |
11 |
Correct |
8 ms |
5248 KB |
Output is correct |
12 |
Correct |
6 ms |
5248 KB |
Output is correct |
13 |
Correct |
9 ms |
5504 KB |
Output is correct |
14 |
Correct |
8 ms |
5248 KB |
Output is correct |
15 |
Correct |
8 ms |
5248 KB |
Output is correct |
16 |
Correct |
7 ms |
5248 KB |
Output is correct |
17 |
Correct |
9 ms |
5376 KB |
Output is correct |
18 |
Correct |
10 ms |
5376 KB |
Output is correct |
19 |
Correct |
8 ms |
5248 KB |
Output is correct |
20 |
Correct |
8 ms |
5504 KB |
Output is correct |
21 |
Correct |
11 ms |
5376 KB |
Output is correct |
22 |
Correct |
11 ms |
5120 KB |
Output is correct |
23 |
Correct |
9 ms |
5248 KB |
Output is correct |
24 |
Correct |
7 ms |
5248 KB |
Output is correct |
25 |
Correct |
7 ms |
5248 KB |
Output is correct |
26 |
Correct |
7 ms |
5248 KB |
Output is correct |
27 |
Correct |
7 ms |
5504 KB |
Output is correct |
28 |
Correct |
7 ms |
5376 KB |
Output is correct |
29 |
Correct |
7 ms |
5376 KB |
Output is correct |
30 |
Correct |
7 ms |
5248 KB |
Output is correct |
31 |
Correct |
8 ms |
5504 KB |
Output is correct |
32 |
Correct |
7 ms |
5376 KB |
Output is correct |
33 |
Correct |
237 ms |
12424 KB |
Output is correct |
34 |
Correct |
341 ms |
30704 KB |
Output is correct |
35 |
Correct |
43 ms |
9080 KB |
Output is correct |
36 |
Correct |
515 ms |
17884 KB |
Output is correct |
37 |
Correct |
688 ms |
50160 KB |
Output is correct |
38 |
Correct |
644 ms |
33736 KB |
Output is correct |
39 |
Correct |
485 ms |
18380 KB |
Output is correct |
40 |
Correct |
525 ms |
21220 KB |
Output is correct |
41 |
Correct |
523 ms |
20020 KB |
Output is correct |
42 |
Correct |
505 ms |
19964 KB |
Output is correct |
43 |
Correct |
204 ms |
21792 KB |
Output is correct |
44 |
Correct |
712 ms |
37880 KB |
Output is correct |
45 |
Correct |
595 ms |
33452 KB |
Output is correct |
46 |
Correct |
614 ms |
32668 KB |
Output is correct |
47 |
Correct |
191 ms |
21792 KB |
Output is correct |
48 |
Correct |
633 ms |
39768 KB |
Output is correct |
49 |
Correct |
642 ms |
33876 KB |
Output is correct |
50 |
Correct |
471 ms |
16588 KB |
Output is correct |
51 |
Correct |
790 ms |
50456 KB |
Output is correct |
52 |
Correct |
58 ms |
9848 KB |
Output is correct |
53 |
Correct |
580 ms |
19576 KB |
Output is correct |
54 |
Correct |
711 ms |
52476 KB |
Output is correct |
55 |
Correct |
690 ms |
35560 KB |
Output is correct |
56 |
Correct |
517 ms |
19764 KB |
Output is correct |
57 |
Correct |
580 ms |
26488 KB |
Output is correct |
58 |
Correct |
565 ms |
24224 KB |
Output is correct |
59 |
Correct |
617 ms |
22084 KB |
Output is correct |
60 |
Correct |
412 ms |
21496 KB |
Output is correct |
61 |
Correct |
699 ms |
45688 KB |
Output is correct |
62 |
Correct |
581 ms |
32968 KB |
Output is correct |
63 |
Correct |
711 ms |
33436 KB |
Output is correct |
64 |
Correct |
201 ms |
22848 KB |
Output is correct |
65 |
Correct |
573 ms |
41376 KB |
Output is correct |
66 |
Correct |
605 ms |
35660 KB |
Output is correct |
67 |
Correct |
32 ms |
7032 KB |
Output is correct |
68 |
Correct |
228 ms |
25208 KB |
Output is correct |
69 |
Correct |
392 ms |
23400 KB |
Output is correct |
70 |
Correct |
553 ms |
18168 KB |
Output is correct |
71 |
Correct |
781 ms |
49912 KB |
Output is correct |
72 |
Correct |
686 ms |
33784 KB |
Output is correct |
73 |
Correct |
566 ms |
18120 KB |
Output is correct |
74 |
Correct |
646 ms |
24284 KB |
Output is correct |
75 |
Correct |
561 ms |
20900 KB |
Output is correct |
76 |
Correct |
519 ms |
20292 KB |
Output is correct |
77 |
Correct |
364 ms |
19796 KB |
Output is correct |
78 |
Correct |
635 ms |
41336 KB |
Output is correct |
79 |
Correct |
556 ms |
37260 KB |
Output is correct |
80 |
Correct |
630 ms |
30688 KB |
Output is correct |
81 |
Correct |
233 ms |
21544 KB |
Output is correct |
82 |
Correct |
599 ms |
39532 KB |
Output is correct |
83 |
Correct |
565 ms |
33776 KB |
Output is correct |
84 |
Correct |
586 ms |
18284 KB |
Output is correct |
85 |
Correct |
819 ms |
50680 KB |
Output is correct |
86 |
Correct |
719 ms |
34296 KB |
Output is correct |
87 |
Correct |
541 ms |
18432 KB |
Output is correct |
88 |
Correct |
624 ms |
25080 KB |
Output is correct |
89 |
Correct |
591 ms |
21868 KB |
Output is correct |
90 |
Correct |
612 ms |
20840 KB |
Output is correct |
91 |
Correct |
413 ms |
20200 KB |
Output is correct |
92 |
Correct |
695 ms |
49752 KB |
Output is correct |
93 |
Correct |
595 ms |
29156 KB |
Output is correct |
94 |
Correct |
638 ms |
26088 KB |
Output is correct |
95 |
Correct |
242 ms |
21956 KB |
Output is correct |
96 |
Correct |
632 ms |
39916 KB |
Output is correct |
97 |
Correct |
620 ms |
34160 KB |
Output is correct |
98 |
Correct |
528 ms |
19576 KB |
Output is correct |
99 |
Correct |
719 ms |
51192 KB |
Output is correct |
100 |
Correct |
695 ms |
35376 KB |
Output is correct |
101 |
Correct |
450 ms |
19260 KB |
Output is correct |
102 |
Correct |
595 ms |
24056 KB |
Output is correct |
103 |
Correct |
658 ms |
21860 KB |
Output is correct |
104 |
Correct |
600 ms |
21392 KB |
Output is correct |
105 |
Correct |
349 ms |
20624 KB |
Output is correct |
106 |
Correct |
791 ms |
37212 KB |
Output is correct |
107 |
Correct |
588 ms |
37360 KB |
Output is correct |
108 |
Correct |
626 ms |
28864 KB |
Output is correct |
109 |
Correct |
216 ms |
22588 KB |
Output is correct |
110 |
Correct |
622 ms |
40636 KB |
Output is correct |
111 |
Correct |
713 ms |
35024 KB |
Output is correct |