#include<stdio.h>
#include<string.h>
#define N 200000
#define N_ (N+1)
int n, m, c[N_], head[N_], vv[N_<<1], nxt[N_<<1], i, u, v, far,dd[N_], rt, orz[N_], oo, hh[N_], longc[N_][2], fr[N_], dist, ans[N_];
void link(int u,int v)
{
static int i=1;
nxt[i]=head[u];
vv[i]=v;
head[u]=i++;
}
void dfs0(int u,int p)
{
dd[u]=dd[p]+1;
if(dd[u]>dd[far])far=u;
for(int j=head[u];j;j=nxt[j])if(vv[j]-p)dfs0(vv[j],u);
}
void dfs1(int u,int p)
{
dd[u]=dd[p]+1;
hh[u]=0;
for(int j=head[u];j;j=nxt[j])if(vv[j]-p)
{
dfs1(vv[j],u);
if(hh[vv[j]]>hh[longc[u][0]])
longc[u][1]=longc[u][0],longc[u][0]=vv[j];
else if(hh[vv[j]]>hh[longc[u][1]])
longc[u][1]=vv[j];
}
hh[u]=hh[longc[u][0]]+1;
}
void del(int x) { dist-=!--fr[x]; }
void ins(int x) { dist+=!fr[x]++; }
void dfs2(int u,int p)
{
if(p)ins(c[p]),orz[++oo]=p;
if(longc[u][0])
{
while(oo&&dd[u]-dd[orz[oo]]<=hh[longc[u][1]])
del(c[orz[oo--]]);
dfs2(longc[u][0],u);
}
while(oo&&dd[u]-dd[orz[oo]]<=hh[longc[u][0]])
del(c[orz[oo--]]);
for(int j=head[u];j;j=nxt[j])if(vv[j]-p&&vv[j]-longc[u][0])
dfs2(vv[j],u);
if(dist>ans[u])ans[u]=dist;
if(orz[oo]==p)del(c[orz[oo--]]);
}
void solve(int u)
{
dfs0(u,0);
rt=far;
dfs1(rt,0);
dfs2(rt,0);
oo=dist=0;
memset(longc,0,sizeof longc);
memset(fr,0,sizeof fr);
}
int main()
{
scanf("%d%d",&n,&m);
for(i=1;i<n;++i)scanf("%d%d",&u,&v),link(u,v),link(v,u);
for(i=1;i<=n;++i)scanf("%d",c+i);
solve(1);
solve(rt);
for(i=1;i<=n;++i)printf("%d\n",ans[i]);
}
Compilation message
joi2019_ho_t5.c: In function 'main':
joi2019_ho_t5.c:74:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | scanf("%d%d",&n,&m);
| ^~~~~~~~~~~~~~~~~~~
joi2019_ho_t5.c:75:21: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | for(i=1;i<n;++i)scanf("%d%d",&u,&v),link(u,v),link(v,u);
| ^~~~~~~~~~~~~~~~~~~
joi2019_ho_t5.c:76:22: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | for(i=1;i<=n;++i)scanf("%d",c+i);
| ^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4700 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
3 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
3 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
5052 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
2 ms |
4956 KB |
Output is correct |
23 |
Correct |
2 ms |
4956 KB |
Output is correct |
24 |
Correct |
2 ms |
4956 KB |
Output is correct |
25 |
Correct |
2 ms |
4956 KB |
Output is correct |
26 |
Correct |
2 ms |
4956 KB |
Output is correct |
27 |
Correct |
2 ms |
4956 KB |
Output is correct |
28 |
Correct |
2 ms |
4956 KB |
Output is correct |
29 |
Correct |
2 ms |
4956 KB |
Output is correct |
30 |
Correct |
2 ms |
4976 KB |
Output is correct |
31 |
Correct |
3 ms |
4956 KB |
Output is correct |
32 |
Correct |
2 ms |
4868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
9552 KB |
Output is correct |
2 |
Correct |
74 ms |
18772 KB |
Output is correct |
3 |
Correct |
15 ms |
6748 KB |
Output is correct |
4 |
Correct |
107 ms |
12892 KB |
Output is correct |
5 |
Correct |
152 ms |
29392 KB |
Output is correct |
6 |
Correct |
139 ms |
20996 KB |
Output is correct |
7 |
Correct |
102 ms |
12952 KB |
Output is correct |
8 |
Correct |
116 ms |
14564 KB |
Output is correct |
9 |
Correct |
106 ms |
14160 KB |
Output is correct |
10 |
Correct |
109 ms |
13912 KB |
Output is correct |
11 |
Correct |
77 ms |
13064 KB |
Output is correct |
12 |
Correct |
136 ms |
23056 KB |
Output is correct |
13 |
Correct |
117 ms |
20816 KB |
Output is correct |
14 |
Correct |
116 ms |
20308 KB |
Output is correct |
15 |
Correct |
65 ms |
12848 KB |
Output is correct |
16 |
Correct |
121 ms |
23892 KB |
Output is correct |
17 |
Correct |
111 ms |
21076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
11888 KB |
Output is correct |
2 |
Correct |
135 ms |
29944 KB |
Output is correct |
3 |
Correct |
17 ms |
7272 KB |
Output is correct |
4 |
Correct |
107 ms |
13772 KB |
Output is correct |
5 |
Correct |
144 ms |
31292 KB |
Output is correct |
6 |
Correct |
130 ms |
22104 KB |
Output is correct |
7 |
Correct |
101 ms |
13908 KB |
Output is correct |
8 |
Correct |
114 ms |
17492 KB |
Output is correct |
9 |
Correct |
111 ms |
16204 KB |
Output is correct |
10 |
Correct |
140 ms |
15184 KB |
Output is correct |
11 |
Correct |
90 ms |
14028 KB |
Output is correct |
12 |
Correct |
139 ms |
27732 KB |
Output is correct |
13 |
Correct |
148 ms |
21124 KB |
Output is correct |
14 |
Correct |
121 ms |
21052 KB |
Output is correct |
15 |
Correct |
74 ms |
14000 KB |
Output is correct |
16 |
Correct |
138 ms |
25524 KB |
Output is correct |
17 |
Correct |
118 ms |
22080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4700 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
3 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
3 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
5052 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
2 ms |
4956 KB |
Output is correct |
23 |
Correct |
2 ms |
4956 KB |
Output is correct |
24 |
Correct |
2 ms |
4956 KB |
Output is correct |
25 |
Correct |
2 ms |
4956 KB |
Output is correct |
26 |
Correct |
2 ms |
4956 KB |
Output is correct |
27 |
Correct |
2 ms |
4956 KB |
Output is correct |
28 |
Correct |
2 ms |
4956 KB |
Output is correct |
29 |
Correct |
2 ms |
4956 KB |
Output is correct |
30 |
Correct |
2 ms |
4976 KB |
Output is correct |
31 |
Correct |
3 ms |
4956 KB |
Output is correct |
32 |
Correct |
2 ms |
4868 KB |
Output is correct |
33 |
Correct |
57 ms |
9552 KB |
Output is correct |
34 |
Correct |
74 ms |
18772 KB |
Output is correct |
35 |
Correct |
15 ms |
6748 KB |
Output is correct |
36 |
Correct |
107 ms |
12892 KB |
Output is correct |
37 |
Correct |
152 ms |
29392 KB |
Output is correct |
38 |
Correct |
139 ms |
20996 KB |
Output is correct |
39 |
Correct |
102 ms |
12952 KB |
Output is correct |
40 |
Correct |
116 ms |
14564 KB |
Output is correct |
41 |
Correct |
106 ms |
14160 KB |
Output is correct |
42 |
Correct |
109 ms |
13912 KB |
Output is correct |
43 |
Correct |
77 ms |
13064 KB |
Output is correct |
44 |
Correct |
136 ms |
23056 KB |
Output is correct |
45 |
Correct |
117 ms |
20816 KB |
Output is correct |
46 |
Correct |
116 ms |
20308 KB |
Output is correct |
47 |
Correct |
65 ms |
12848 KB |
Output is correct |
48 |
Correct |
121 ms |
23892 KB |
Output is correct |
49 |
Correct |
111 ms |
21076 KB |
Output is correct |
50 |
Correct |
89 ms |
11888 KB |
Output is correct |
51 |
Correct |
135 ms |
29944 KB |
Output is correct |
52 |
Correct |
17 ms |
7272 KB |
Output is correct |
53 |
Correct |
107 ms |
13772 KB |
Output is correct |
54 |
Correct |
144 ms |
31292 KB |
Output is correct |
55 |
Correct |
130 ms |
22104 KB |
Output is correct |
56 |
Correct |
101 ms |
13908 KB |
Output is correct |
57 |
Correct |
114 ms |
17492 KB |
Output is correct |
58 |
Correct |
111 ms |
16204 KB |
Output is correct |
59 |
Correct |
140 ms |
15184 KB |
Output is correct |
60 |
Correct |
90 ms |
14028 KB |
Output is correct |
61 |
Correct |
139 ms |
27732 KB |
Output is correct |
62 |
Correct |
148 ms |
21124 KB |
Output is correct |
63 |
Correct |
121 ms |
21052 KB |
Output is correct |
64 |
Correct |
74 ms |
14000 KB |
Output is correct |
65 |
Correct |
138 ms |
25524 KB |
Output is correct |
66 |
Correct |
118 ms |
22080 KB |
Output is correct |
67 |
Correct |
13 ms |
5720 KB |
Output is correct |
68 |
Correct |
54 ms |
15956 KB |
Output is correct |
69 |
Correct |
72 ms |
15588 KB |
Output is correct |
70 |
Correct |
103 ms |
13088 KB |
Output is correct |
71 |
Correct |
143 ms |
29540 KB |
Output is correct |
72 |
Correct |
140 ms |
20956 KB |
Output is correct |
73 |
Correct |
105 ms |
12880 KB |
Output is correct |
74 |
Correct |
141 ms |
16120 KB |
Output is correct |
75 |
Correct |
129 ms |
14420 KB |
Output is correct |
76 |
Correct |
125 ms |
14308 KB |
Output is correct |
77 |
Correct |
88 ms |
12916 KB |
Output is correct |
78 |
Correct |
138 ms |
24920 KB |
Output is correct |
79 |
Correct |
112 ms |
22864 KB |
Output is correct |
80 |
Correct |
123 ms |
19280 KB |
Output is correct |
81 |
Correct |
66 ms |
12624 KB |
Output is correct |
82 |
Correct |
120 ms |
23892 KB |
Output is correct |
83 |
Correct |
141 ms |
20928 KB |
Output is correct |
84 |
Correct |
106 ms |
13404 KB |
Output is correct |
85 |
Correct |
139 ms |
30036 KB |
Output is correct |
86 |
Correct |
142 ms |
21536 KB |
Output is correct |
87 |
Correct |
111 ms |
13136 KB |
Output is correct |
88 |
Correct |
112 ms |
16720 KB |
Output is correct |
89 |
Correct |
110 ms |
15188 KB |
Output is correct |
90 |
Correct |
123 ms |
15136 KB |
Output is correct |
91 |
Correct |
85 ms |
13332 KB |
Output is correct |
92 |
Correct |
147 ms |
29524 KB |
Output is correct |
93 |
Correct |
130 ms |
18856 KB |
Output is correct |
94 |
Correct |
112 ms |
17528 KB |
Output is correct |
95 |
Correct |
68 ms |
13140 KB |
Output is correct |
96 |
Correct |
153 ms |
24612 KB |
Output is correct |
97 |
Correct |
122 ms |
21820 KB |
Output is correct |
98 |
Correct |
109 ms |
13992 KB |
Output is correct |
99 |
Correct |
162 ms |
30588 KB |
Output is correct |
100 |
Correct |
141 ms |
22236 KB |
Output is correct |
101 |
Correct |
102 ms |
13648 KB |
Output is correct |
102 |
Correct |
113 ms |
16500 KB |
Output is correct |
103 |
Correct |
109 ms |
15440 KB |
Output is correct |
104 |
Correct |
110 ms |
15132 KB |
Output is correct |
105 |
Correct |
83 ms |
13676 KB |
Output is correct |
106 |
Correct |
130 ms |
23264 KB |
Output is correct |
107 |
Correct |
117 ms |
23636 KB |
Output is correct |
108 |
Correct |
116 ms |
18796 KB |
Output is correct |
109 |
Correct |
66 ms |
13904 KB |
Output is correct |
110 |
Correct |
126 ms |
25172 KB |
Output is correct |
111 |
Correct |
116 ms |
22100 KB |
Output is correct |