#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2e5;
int N, M, A[MAXN+10];
vector<int> adj[MAXN+10];
int P, Q;
int DP[MAXN+10], DQ[MAXN+10];
void dfs(int now, int bef, int *D, int dep)
{
D[now]=dep;
for(int nxt : adj[now])
{
if(nxt==bef) continue;
dfs(nxt, now, D, dep+1);
}
}
int H[MAXN+10];
vector<int> chd[MAXN+10];
void dfs2(int now, int bef)
{
H[now]=0;
chd[now].clear();
for(int nxt : adj[now])
{
if(nxt==bef) continue;
chd[now].push_back(nxt);
dfs2(nxt, now);
H[now]=max(H[now], H[nxt]+1);
}
sort(chd[now].begin(), chd[now].end(), [&](const int &p, const int &q) { return H[p]>H[q]; });
}
int cnt[MAXN+10], chk[MAXN+10], ans[MAXN+10];
vector<pii> V;
void dfs3(int now, int bef, int dep)
{
int i, j;
if(chd[now].size()==0)
{
if(chk[now]) ans[now]=V.size();
}
else if(chd[now].size()==1)
{
if((V.empty() || V.back().first!=dep) && !cnt[A[now]]) V.push_back({dep, A[now]}), cnt[A[now]]++;
dfs3(chd[now][0], now, dep+1);
while(!V.empty() && V.back().first>=dep-H[now]) cnt[V.back().second]--, V.pop_back();
if(chk[now]) ans[now]=V.size();
}
else
{
while(!V.empty() && V.back().first>=dep-H[chd[now][1]]-1) cnt[V.back().second]--, V.pop_back();
if((V.empty() || V.back().first!=dep) && !cnt[A[now]]) V.push_back({dep, A[now]}), cnt[A[now]]++;
dfs3(chd[now][0], now, dep+1);
while(!V.empty() && V.back().first>=dep-H[chd[now][0]]-1) cnt[V.back().second]--, V.pop_back();
for(i=1; i<chd[now].size(); i++)
{
int nxt=chd[now][i];
if((V.empty() || V.back().first!=dep) && !cnt[A[now]]) V.push_back({dep, A[now]}), cnt[A[now]]++;
dfs3(nxt, now, dep+1);
}
while(!V.empty() && V.back().first>=dep-H[now]) cnt[V.back().second]--, V.pop_back();
if(chk[now]) ans[now]=V.size();
}
}
int main()
{
int i, j;
scanf("%d%d", &N, &M);
for(i=1; i<N; i++)
{
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for(i=1; i<=N; i++) scanf("%d", &A[i]);
dfs(1, 1, DP, 0); P=1;
for(i=1; i<=N; i++) if(DP[P]<DP[i]) P=i;
dfs(P, P, DP, 0); Q=1;
for(i=1; i<=N; i++) if(DP[Q]<DP[i]) Q=i;
dfs(Q, Q, DQ, 0);
//printf("%d %d\n", P, Q);
memset(chk, 0, sizeof(chk));
memset(cnt, 0, sizeof(cnt));
V.clear();
for(i=1; i<=N; i++) if(DP[i]>DQ[i]) chk[i]=1;
dfs2(P, P);
dfs3(P, P, 0);
memset(chk, 0, sizeof(chk));
memset(cnt, 0, sizeof(cnt));
V.clear();
for(i=1; i<=N; i++) if(DP[i]<=DQ[i]) chk[i]=1;
dfs2(Q, Q);
dfs3(Q, Q, 0);
for(i=1; i<=N; i++) printf("%d\n", ans[i]);
}
Compilation message
joi2019_ho_t5.cpp: In function 'void dfs3(int, int, int)':
joi2019_ho_t5.cpp:66:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=1; i<chd[now].size(); i++)
~^~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:48:12: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:79:12: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
joi2019_ho_t5.cpp:81:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:85:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:89:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=N; i++) scanf("%d", &A[i]);
~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
11264 KB |
Output is correct |
2 |
Correct |
13 ms |
11392 KB |
Output is correct |
3 |
Correct |
14 ms |
11520 KB |
Output is correct |
4 |
Correct |
13 ms |
11520 KB |
Output is correct |
5 |
Correct |
13 ms |
11520 KB |
Output is correct |
6 |
Correct |
13 ms |
11648 KB |
Output is correct |
7 |
Correct |
13 ms |
11520 KB |
Output is correct |
8 |
Correct |
15 ms |
11520 KB |
Output is correct |
9 |
Correct |
14 ms |
11520 KB |
Output is correct |
10 |
Correct |
15 ms |
11520 KB |
Output is correct |
11 |
Correct |
14 ms |
11520 KB |
Output is correct |
12 |
Correct |
12 ms |
11392 KB |
Output is correct |
13 |
Correct |
13 ms |
11648 KB |
Output is correct |
14 |
Correct |
13 ms |
11520 KB |
Output is correct |
15 |
Correct |
13 ms |
11520 KB |
Output is correct |
16 |
Correct |
12 ms |
11392 KB |
Output is correct |
17 |
Correct |
12 ms |
11648 KB |
Output is correct |
18 |
Correct |
14 ms |
11520 KB |
Output is correct |
19 |
Correct |
13 ms |
11520 KB |
Output is correct |
20 |
Correct |
13 ms |
11648 KB |
Output is correct |
21 |
Correct |
13 ms |
11648 KB |
Output is correct |
22 |
Correct |
13 ms |
11520 KB |
Output is correct |
23 |
Correct |
13 ms |
11520 KB |
Output is correct |
24 |
Correct |
13 ms |
11520 KB |
Output is correct |
25 |
Correct |
13 ms |
11520 KB |
Output is correct |
26 |
Correct |
13 ms |
11520 KB |
Output is correct |
27 |
Correct |
13 ms |
11648 KB |
Output is correct |
28 |
Correct |
13 ms |
11648 KB |
Output is correct |
29 |
Correct |
15 ms |
11520 KB |
Output is correct |
30 |
Correct |
12 ms |
11520 KB |
Output is correct |
31 |
Correct |
13 ms |
11776 KB |
Output is correct |
32 |
Correct |
13 ms |
11648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
187 ms |
19576 KB |
Output is correct |
2 |
Correct |
276 ms |
33356 KB |
Output is correct |
3 |
Correct |
42 ms |
15352 KB |
Output is correct |
4 |
Correct |
406 ms |
28620 KB |
Output is correct |
5 |
Correct |
504 ms |
49912 KB |
Output is correct |
6 |
Correct |
505 ms |
40056 KB |
Output is correct |
7 |
Correct |
387 ms |
28440 KB |
Output is correct |
8 |
Correct |
423 ms |
30540 KB |
Output is correct |
9 |
Correct |
442 ms |
30072 KB |
Output is correct |
10 |
Correct |
426 ms |
29816 KB |
Output is correct |
11 |
Correct |
254 ms |
27372 KB |
Output is correct |
12 |
Correct |
517 ms |
41080 KB |
Output is correct |
13 |
Correct |
494 ms |
38132 KB |
Output is correct |
14 |
Correct |
439 ms |
38260 KB |
Output is correct |
15 |
Correct |
247 ms |
26604 KB |
Output is correct |
16 |
Correct |
439 ms |
42352 KB |
Output is correct |
17 |
Correct |
439 ms |
39412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
332 ms |
22776 KB |
Output is correct |
2 |
Correct |
605 ms |
51560 KB |
Output is correct |
3 |
Correct |
49 ms |
16120 KB |
Output is correct |
4 |
Correct |
527 ms |
29560 KB |
Output is correct |
5 |
Correct |
540 ms |
53460 KB |
Output is correct |
6 |
Correct |
592 ms |
41816 KB |
Output is correct |
7 |
Correct |
465 ms |
29560 KB |
Output is correct |
8 |
Correct |
582 ms |
33660 KB |
Output is correct |
9 |
Correct |
457 ms |
32120 KB |
Output is correct |
10 |
Correct |
524 ms |
31096 KB |
Output is correct |
11 |
Correct |
353 ms |
28912 KB |
Output is correct |
12 |
Correct |
575 ms |
47828 KB |
Output is correct |
13 |
Correct |
498 ms |
38768 KB |
Output is correct |
14 |
Correct |
482 ms |
39412 KB |
Output is correct |
15 |
Correct |
200 ms |
27496 KB |
Output is correct |
16 |
Correct |
436 ms |
44904 KB |
Output is correct |
17 |
Correct |
444 ms |
40948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
11264 KB |
Output is correct |
2 |
Correct |
13 ms |
11392 KB |
Output is correct |
3 |
Correct |
14 ms |
11520 KB |
Output is correct |
4 |
Correct |
13 ms |
11520 KB |
Output is correct |
5 |
Correct |
13 ms |
11520 KB |
Output is correct |
6 |
Correct |
13 ms |
11648 KB |
Output is correct |
7 |
Correct |
13 ms |
11520 KB |
Output is correct |
8 |
Correct |
15 ms |
11520 KB |
Output is correct |
9 |
Correct |
14 ms |
11520 KB |
Output is correct |
10 |
Correct |
15 ms |
11520 KB |
Output is correct |
11 |
Correct |
14 ms |
11520 KB |
Output is correct |
12 |
Correct |
12 ms |
11392 KB |
Output is correct |
13 |
Correct |
13 ms |
11648 KB |
Output is correct |
14 |
Correct |
13 ms |
11520 KB |
Output is correct |
15 |
Correct |
13 ms |
11520 KB |
Output is correct |
16 |
Correct |
12 ms |
11392 KB |
Output is correct |
17 |
Correct |
12 ms |
11648 KB |
Output is correct |
18 |
Correct |
14 ms |
11520 KB |
Output is correct |
19 |
Correct |
13 ms |
11520 KB |
Output is correct |
20 |
Correct |
13 ms |
11648 KB |
Output is correct |
21 |
Correct |
13 ms |
11648 KB |
Output is correct |
22 |
Correct |
13 ms |
11520 KB |
Output is correct |
23 |
Correct |
13 ms |
11520 KB |
Output is correct |
24 |
Correct |
13 ms |
11520 KB |
Output is correct |
25 |
Correct |
13 ms |
11520 KB |
Output is correct |
26 |
Correct |
13 ms |
11520 KB |
Output is correct |
27 |
Correct |
13 ms |
11648 KB |
Output is correct |
28 |
Correct |
13 ms |
11648 KB |
Output is correct |
29 |
Correct |
15 ms |
11520 KB |
Output is correct |
30 |
Correct |
12 ms |
11520 KB |
Output is correct |
31 |
Correct |
13 ms |
11776 KB |
Output is correct |
32 |
Correct |
13 ms |
11648 KB |
Output is correct |
33 |
Correct |
187 ms |
19576 KB |
Output is correct |
34 |
Correct |
276 ms |
33356 KB |
Output is correct |
35 |
Correct |
42 ms |
15352 KB |
Output is correct |
36 |
Correct |
406 ms |
28620 KB |
Output is correct |
37 |
Correct |
504 ms |
49912 KB |
Output is correct |
38 |
Correct |
505 ms |
40056 KB |
Output is correct |
39 |
Correct |
387 ms |
28440 KB |
Output is correct |
40 |
Correct |
423 ms |
30540 KB |
Output is correct |
41 |
Correct |
442 ms |
30072 KB |
Output is correct |
42 |
Correct |
426 ms |
29816 KB |
Output is correct |
43 |
Correct |
254 ms |
27372 KB |
Output is correct |
44 |
Correct |
517 ms |
41080 KB |
Output is correct |
45 |
Correct |
494 ms |
38132 KB |
Output is correct |
46 |
Correct |
439 ms |
38260 KB |
Output is correct |
47 |
Correct |
247 ms |
26604 KB |
Output is correct |
48 |
Correct |
439 ms |
42352 KB |
Output is correct |
49 |
Correct |
439 ms |
39412 KB |
Output is correct |
50 |
Correct |
332 ms |
22776 KB |
Output is correct |
51 |
Correct |
605 ms |
51560 KB |
Output is correct |
52 |
Correct |
49 ms |
16120 KB |
Output is correct |
53 |
Correct |
527 ms |
29560 KB |
Output is correct |
54 |
Correct |
540 ms |
53460 KB |
Output is correct |
55 |
Correct |
592 ms |
41816 KB |
Output is correct |
56 |
Correct |
465 ms |
29560 KB |
Output is correct |
57 |
Correct |
582 ms |
33660 KB |
Output is correct |
58 |
Correct |
457 ms |
32120 KB |
Output is correct |
59 |
Correct |
524 ms |
31096 KB |
Output is correct |
60 |
Correct |
353 ms |
28912 KB |
Output is correct |
61 |
Correct |
575 ms |
47828 KB |
Output is correct |
62 |
Correct |
498 ms |
38768 KB |
Output is correct |
63 |
Correct |
482 ms |
39412 KB |
Output is correct |
64 |
Correct |
200 ms |
27496 KB |
Output is correct |
65 |
Correct |
436 ms |
44904 KB |
Output is correct |
66 |
Correct |
444 ms |
40948 KB |
Output is correct |
67 |
Correct |
40 ms |
13688 KB |
Output is correct |
68 |
Correct |
177 ms |
29004 KB |
Output is correct |
69 |
Correct |
270 ms |
29744 KB |
Output is correct |
70 |
Correct |
405 ms |
28664 KB |
Output is correct |
71 |
Correct |
519 ms |
49784 KB |
Output is correct |
72 |
Correct |
512 ms |
39800 KB |
Output is correct |
73 |
Correct |
420 ms |
28536 KB |
Output is correct |
74 |
Correct |
447 ms |
32248 KB |
Output is correct |
75 |
Correct |
434 ms |
30328 KB |
Output is correct |
76 |
Correct |
432 ms |
30072 KB |
Output is correct |
77 |
Correct |
310 ms |
27888 KB |
Output is correct |
78 |
Correct |
516 ms |
43512 KB |
Output is correct |
79 |
Correct |
459 ms |
40564 KB |
Output is correct |
80 |
Correct |
492 ms |
36980 KB |
Output is correct |
81 |
Correct |
210 ms |
26216 KB |
Output is correct |
82 |
Correct |
413 ms |
42224 KB |
Output is correct |
83 |
Correct |
575 ms |
39288 KB |
Output is correct |
84 |
Correct |
437 ms |
28920 KB |
Output is correct |
85 |
Correct |
530 ms |
50552 KB |
Output is correct |
86 |
Correct |
486 ms |
40312 KB |
Output is correct |
87 |
Correct |
388 ms |
28792 KB |
Output is correct |
88 |
Correct |
424 ms |
33016 KB |
Output is correct |
89 |
Correct |
437 ms |
30968 KB |
Output is correct |
90 |
Correct |
433 ms |
30588 KB |
Output is correct |
91 |
Correct |
335 ms |
28276 KB |
Output is correct |
92 |
Correct |
539 ms |
49788 KB |
Output is correct |
93 |
Correct |
456 ms |
35572 KB |
Output is correct |
94 |
Correct |
446 ms |
33912 KB |
Output is correct |
95 |
Correct |
210 ms |
26764 KB |
Output is correct |
96 |
Correct |
450 ms |
42992 KB |
Output is correct |
97 |
Correct |
477 ms |
39796 KB |
Output is correct |
98 |
Correct |
421 ms |
29564 KB |
Output is correct |
99 |
Correct |
530 ms |
50988 KB |
Output is correct |
100 |
Correct |
499 ms |
41452 KB |
Output is correct |
101 |
Correct |
392 ms |
29172 KB |
Output is correct |
102 |
Correct |
436 ms |
32632 KB |
Output is correct |
103 |
Correct |
447 ms |
31224 KB |
Output is correct |
104 |
Correct |
470 ms |
30968 KB |
Output is correct |
105 |
Correct |
394 ms |
28528 KB |
Output is correct |
106 |
Correct |
665 ms |
41400 KB |
Output is correct |
107 |
Correct |
472 ms |
41332 KB |
Output is correct |
108 |
Correct |
558 ms |
36024 KB |
Output is correct |
109 |
Correct |
214 ms |
27368 KB |
Output is correct |
110 |
Correct |
426 ms |
43888 KB |
Output is correct |
111 |
Correct |
445 ms |
40944 KB |
Output is correct |