#include <bits/stdc++.h>
using namespace std;
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define st first
#define nd second
#define sp " "
#define endl "\n"
#define pii pair<int, int>
#define N 2000005
const int modulo = 1e9 + 7;
int dist[N], ans[N], dep[N], arr[N], cnt[N], res;
vector<int> adj[N];
vector<int> s;
void del(int x)
{
while(!s.empty() && dist[s.back()] >= x)
{
int curr = s.back();
cnt[arr[curr]]--;
if (cnt[arr[curr]] == 0) res--;
s.pop_back();
}
//for (auto i : s) cerr<<arr[i]<<sp;
//cerr<<"->"<<res<<endl;
}
void add(int node)
{
if (s.empty() || s.back() != node)
{
s.pb(node);
cnt[arr[node]]++;
if (cnt[arr[node]] == 1) res++;
}
//for (auto i : s) cerr<<arr[i]<<sp;
//cerr<<"->"<<res<<endl;
}
void dfs(int node, int root, int d)
{
dist[node] = d;
dep[node] = d;
for (auto i : adj[node])
{
if (i == root) continue;
dfs(i, node, d + 1);
dep[node] = max(dep[node], dep[i]);
}
}
void dfs2(int node, int root)
{
pair<pii, pii> maks = {{0, 0}, {0, 0}};
for (int i = 0; i < adj[node].size(); i++)
{
int curr = adj[node][i];
if (curr == root) continue;
if (dep[curr] > maks.st.st)
{
maks.nd = maks.st;
maks.st = {dep[curr], curr};
}
else maks.nd = max(maks.nd, {dep[curr], curr});
}
int diff = maks.nd.st - dist[node];
int to = max(0, dist[node] - diff);
del(to);
int top = maks.st.nd;
add(node);
vector<int> ord;
if (top != 0) ord.pb(top), dfs2(top, node);
diff = maks.st.st - dist[node];
to = max(0, dist[node] - diff);
del(to);
for (int i = 0; i < adj[node].size(); i++)
{
int curr = adj[node][i];
if (curr != root && curr != top)
{
ord.pb(curr);
add(node);
dfs2(curr, node);
}
}
del(dist[node]);
ans[node] = max(ans[node], res);
//cout<<node<<" : ";
////for (auto i : ord) cout<<i<<sp;
////cout<<"||| ";
//for (auto i : s) cout<<i<<sp;
//cout<<endl;
}
int32_t main()
{
fastio();
int n, m;
cin>>n>>m;
for (int i = 1; i < n; i++)
{
int u, v;
cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
for (int i = 1; i <= n; i++)
cin>>arr[i];
dfs(1, 0, 1);
pii maks = {0, 0};
for (int i = 1; i <= n; i++) maks = max(maks, {dist[i], i});
int root = maks.nd;
dfs(root, 0, 1);
dfs2(root, 0);
maks = {0, 0};
for (int i = 1; i <= n; i++) maks = max(maks, {dist[i], i});
for (int i = 1; i <= n; i++) cnt[arr[i]] = 0;
s.clear();
res = 0;
root = maks.nd;
dfs(root, 0, 1);
dfs2(root, 0);
for (int i = 1; i <= n; i++)
cout<<ans[i]<<endl;
cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n";
}
Compilation message
joi2019_ho_t5.cpp: In function 'void dfs2(int, int)':
joi2019_ho_t5.cpp:61:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int i = 0; i < adj[node].size(); i++)
| ~~^~~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:83:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for (int i = 0; i < adj[node].size(); i++)
| ~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
47320 KB |
Output is correct |
2 |
Correct |
22 ms |
47436 KB |
Output is correct |
3 |
Correct |
22 ms |
47448 KB |
Output is correct |
4 |
Correct |
22 ms |
47568 KB |
Output is correct |
5 |
Correct |
23 ms |
47412 KB |
Output is correct |
6 |
Correct |
24 ms |
47732 KB |
Output is correct |
7 |
Correct |
26 ms |
47572 KB |
Output is correct |
8 |
Correct |
23 ms |
47420 KB |
Output is correct |
9 |
Correct |
24 ms |
47468 KB |
Output is correct |
10 |
Correct |
28 ms |
47444 KB |
Output is correct |
11 |
Correct |
24 ms |
47476 KB |
Output is correct |
12 |
Correct |
24 ms |
47440 KB |
Output is correct |
13 |
Correct |
23 ms |
47604 KB |
Output is correct |
14 |
Correct |
25 ms |
47464 KB |
Output is correct |
15 |
Correct |
24 ms |
47560 KB |
Output is correct |
16 |
Correct |
24 ms |
47392 KB |
Output is correct |
17 |
Correct |
24 ms |
47600 KB |
Output is correct |
18 |
Correct |
26 ms |
47580 KB |
Output is correct |
19 |
Correct |
24 ms |
47444 KB |
Output is correct |
20 |
Correct |
24 ms |
47704 KB |
Output is correct |
21 |
Correct |
24 ms |
47560 KB |
Output is correct |
22 |
Correct |
24 ms |
47440 KB |
Output is correct |
23 |
Correct |
27 ms |
47432 KB |
Output is correct |
24 |
Correct |
26 ms |
47424 KB |
Output is correct |
25 |
Correct |
23 ms |
47404 KB |
Output is correct |
26 |
Correct |
23 ms |
47436 KB |
Output is correct |
27 |
Correct |
24 ms |
47560 KB |
Output is correct |
28 |
Correct |
23 ms |
47572 KB |
Output is correct |
29 |
Correct |
23 ms |
47572 KB |
Output is correct |
30 |
Correct |
25 ms |
47492 KB |
Output is correct |
31 |
Correct |
24 ms |
47628 KB |
Output is correct |
32 |
Correct |
24 ms |
47492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
52964 KB |
Output is correct |
2 |
Correct |
180 ms |
69592 KB |
Output is correct |
3 |
Correct |
48 ms |
50636 KB |
Output is correct |
4 |
Correct |
302 ms |
57200 KB |
Output is correct |
5 |
Correct |
400 ms |
86076 KB |
Output is correct |
6 |
Correct |
375 ms |
71316 KB |
Output is correct |
7 |
Correct |
266 ms |
57384 KB |
Output is correct |
8 |
Correct |
299 ms |
59992 KB |
Output is correct |
9 |
Correct |
285 ms |
59084 KB |
Output is correct |
10 |
Correct |
288 ms |
59088 KB |
Output is correct |
11 |
Correct |
169 ms |
59276 KB |
Output is correct |
12 |
Correct |
348 ms |
74988 KB |
Output is correct |
13 |
Correct |
365 ms |
71108 KB |
Output is correct |
14 |
Correct |
327 ms |
70344 KB |
Output is correct |
15 |
Correct |
145 ms |
59288 KB |
Output is correct |
16 |
Correct |
330 ms |
76780 KB |
Output is correct |
17 |
Correct |
325 ms |
71716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
234 ms |
55772 KB |
Output is correct |
2 |
Correct |
377 ms |
85888 KB |
Output is correct |
3 |
Correct |
47 ms |
51188 KB |
Output is correct |
4 |
Correct |
316 ms |
57972 KB |
Output is correct |
5 |
Correct |
400 ms |
87640 KB |
Output is correct |
6 |
Correct |
378 ms |
72404 KB |
Output is correct |
7 |
Correct |
280 ms |
58176 KB |
Output is correct |
8 |
Correct |
334 ms |
63824 KB |
Output is correct |
9 |
Correct |
310 ms |
61848 KB |
Output is correct |
10 |
Correct |
326 ms |
60244 KB |
Output is correct |
11 |
Correct |
236 ms |
58684 KB |
Output is correct |
12 |
Correct |
379 ms |
81580 KB |
Output is correct |
13 |
Correct |
327 ms |
70516 KB |
Output is correct |
14 |
Correct |
334 ms |
70768 KB |
Output is correct |
15 |
Correct |
150 ms |
60064 KB |
Output is correct |
16 |
Correct |
333 ms |
77960 KB |
Output is correct |
17 |
Correct |
358 ms |
72824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
47320 KB |
Output is correct |
2 |
Correct |
22 ms |
47436 KB |
Output is correct |
3 |
Correct |
22 ms |
47448 KB |
Output is correct |
4 |
Correct |
22 ms |
47568 KB |
Output is correct |
5 |
Correct |
23 ms |
47412 KB |
Output is correct |
6 |
Correct |
24 ms |
47732 KB |
Output is correct |
7 |
Correct |
26 ms |
47572 KB |
Output is correct |
8 |
Correct |
23 ms |
47420 KB |
Output is correct |
9 |
Correct |
24 ms |
47468 KB |
Output is correct |
10 |
Correct |
28 ms |
47444 KB |
Output is correct |
11 |
Correct |
24 ms |
47476 KB |
Output is correct |
12 |
Correct |
24 ms |
47440 KB |
Output is correct |
13 |
Correct |
23 ms |
47604 KB |
Output is correct |
14 |
Correct |
25 ms |
47464 KB |
Output is correct |
15 |
Correct |
24 ms |
47560 KB |
Output is correct |
16 |
Correct |
24 ms |
47392 KB |
Output is correct |
17 |
Correct |
24 ms |
47600 KB |
Output is correct |
18 |
Correct |
26 ms |
47580 KB |
Output is correct |
19 |
Correct |
24 ms |
47444 KB |
Output is correct |
20 |
Correct |
24 ms |
47704 KB |
Output is correct |
21 |
Correct |
24 ms |
47560 KB |
Output is correct |
22 |
Correct |
24 ms |
47440 KB |
Output is correct |
23 |
Correct |
27 ms |
47432 KB |
Output is correct |
24 |
Correct |
26 ms |
47424 KB |
Output is correct |
25 |
Correct |
23 ms |
47404 KB |
Output is correct |
26 |
Correct |
23 ms |
47436 KB |
Output is correct |
27 |
Correct |
24 ms |
47560 KB |
Output is correct |
28 |
Correct |
23 ms |
47572 KB |
Output is correct |
29 |
Correct |
23 ms |
47572 KB |
Output is correct |
30 |
Correct |
25 ms |
47492 KB |
Output is correct |
31 |
Correct |
24 ms |
47628 KB |
Output is correct |
32 |
Correct |
24 ms |
47492 KB |
Output is correct |
33 |
Correct |
143 ms |
52964 KB |
Output is correct |
34 |
Correct |
180 ms |
69592 KB |
Output is correct |
35 |
Correct |
48 ms |
50636 KB |
Output is correct |
36 |
Correct |
302 ms |
57200 KB |
Output is correct |
37 |
Correct |
400 ms |
86076 KB |
Output is correct |
38 |
Correct |
375 ms |
71316 KB |
Output is correct |
39 |
Correct |
266 ms |
57384 KB |
Output is correct |
40 |
Correct |
299 ms |
59992 KB |
Output is correct |
41 |
Correct |
285 ms |
59084 KB |
Output is correct |
42 |
Correct |
288 ms |
59088 KB |
Output is correct |
43 |
Correct |
169 ms |
59276 KB |
Output is correct |
44 |
Correct |
348 ms |
74988 KB |
Output is correct |
45 |
Correct |
365 ms |
71108 KB |
Output is correct |
46 |
Correct |
327 ms |
70344 KB |
Output is correct |
47 |
Correct |
145 ms |
59288 KB |
Output is correct |
48 |
Correct |
330 ms |
76780 KB |
Output is correct |
49 |
Correct |
325 ms |
71716 KB |
Output is correct |
50 |
Correct |
234 ms |
55772 KB |
Output is correct |
51 |
Correct |
377 ms |
85888 KB |
Output is correct |
52 |
Correct |
47 ms |
51188 KB |
Output is correct |
53 |
Correct |
316 ms |
57972 KB |
Output is correct |
54 |
Correct |
400 ms |
87640 KB |
Output is correct |
55 |
Correct |
378 ms |
72404 KB |
Output is correct |
56 |
Correct |
280 ms |
58176 KB |
Output is correct |
57 |
Correct |
334 ms |
63824 KB |
Output is correct |
58 |
Correct |
310 ms |
61848 KB |
Output is correct |
59 |
Correct |
326 ms |
60244 KB |
Output is correct |
60 |
Correct |
236 ms |
58684 KB |
Output is correct |
61 |
Correct |
379 ms |
81580 KB |
Output is correct |
62 |
Correct |
327 ms |
70516 KB |
Output is correct |
63 |
Correct |
334 ms |
70768 KB |
Output is correct |
64 |
Correct |
150 ms |
60064 KB |
Output is correct |
65 |
Correct |
333 ms |
77960 KB |
Output is correct |
66 |
Correct |
358 ms |
72824 KB |
Output is correct |
67 |
Correct |
48 ms |
49100 KB |
Output is correct |
68 |
Correct |
131 ms |
66044 KB |
Output is correct |
69 |
Correct |
219 ms |
64640 KB |
Output is correct |
70 |
Correct |
289 ms |
60148 KB |
Output is correct |
71 |
Correct |
402 ms |
89064 KB |
Output is correct |
72 |
Correct |
359 ms |
74316 KB |
Output is correct |
73 |
Correct |
294 ms |
60444 KB |
Output is correct |
74 |
Correct |
333 ms |
65508 KB |
Output is correct |
75 |
Correct |
322 ms |
62680 KB |
Output is correct |
76 |
Correct |
309 ms |
62288 KB |
Output is correct |
77 |
Correct |
228 ms |
60928 KB |
Output is correct |
78 |
Correct |
367 ms |
81044 KB |
Output is correct |
79 |
Correct |
329 ms |
77508 KB |
Output is correct |
80 |
Correct |
335 ms |
71700 KB |
Output is correct |
81 |
Correct |
155 ms |
61832 KB |
Output is correct |
82 |
Correct |
335 ms |
79548 KB |
Output is correct |
83 |
Correct |
338 ms |
74672 KB |
Output is correct |
84 |
Correct |
307 ms |
60444 KB |
Output is correct |
85 |
Correct |
406 ms |
89608 KB |
Output is correct |
86 |
Correct |
414 ms |
74756 KB |
Output is correct |
87 |
Correct |
297 ms |
60476 KB |
Output is correct |
88 |
Correct |
348 ms |
66312 KB |
Output is correct |
89 |
Correct |
313 ms |
63480 KB |
Output is correct |
90 |
Correct |
316 ms |
62808 KB |
Output is correct |
91 |
Correct |
238 ms |
61320 KB |
Output is correct |
92 |
Correct |
438 ms |
88624 KB |
Output is correct |
93 |
Correct |
332 ms |
69820 KB |
Output is correct |
94 |
Correct |
371 ms |
67444 KB |
Output is correct |
95 |
Correct |
148 ms |
62444 KB |
Output is correct |
96 |
Correct |
329 ms |
80216 KB |
Output is correct |
97 |
Correct |
338 ms |
75144 KB |
Output is correct |
98 |
Correct |
317 ms |
61856 KB |
Output is correct |
99 |
Correct |
403 ms |
90044 KB |
Output is correct |
100 |
Correct |
397 ms |
75944 KB |
Output is correct |
101 |
Correct |
285 ms |
61060 KB |
Output is correct |
102 |
Correct |
341 ms |
65592 KB |
Output is correct |
103 |
Correct |
315 ms |
63836 KB |
Output is correct |
104 |
Correct |
323 ms |
63204 KB |
Output is correct |
105 |
Correct |
226 ms |
61784 KB |
Output is correct |
106 |
Correct |
372 ms |
77640 KB |
Output is correct |
107 |
Correct |
354 ms |
78096 KB |
Output is correct |
108 |
Correct |
359 ms |
70704 KB |
Output is correct |
109 |
Correct |
149 ms |
63384 KB |
Output is correct |
110 |
Correct |
335 ms |
80944 KB |
Output is correct |
111 |
Correct |
372 ms |
75972 KB |
Output is correct |