#include "bits/stdc++.h"
#define MAXN 200009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x) cerr<< #x <<" = "<< x<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
vector<int>adj[MAXN];
int arr[MAXN],n,m,lvl[MAXN],height[MAXN];
void dfs0(int nd,int pr=0){
lvl[nd]=lvl[pr]+1;
for(auto to:adj[nd])
if(to!=pr)
dfs0(to,nd);
}
void dfs1(int nd,int pr=0){
height[nd]=0;
for(auto to:adj[nd])
if(to!=pr){
dfs1(to,nd);
umax(height[nd],height[to]+1);
}
}
bool cmp(int x,int y){
return (height[x]>height[y]);
}
int ans[MAXN],cur,cnt[MAXN];
vector<int>S;
void upd(int nd,int d){
while(!S.empty() and lvl[nd]-lvl[S.back()]<=d)
cur-=(--cnt[arr[S.back()]]==0),S.ppb();
}
void dfs2(int nd,int pr){
sort(all(adj[nd]),cmp);
if(adj[nd].size()>1){
if(adj[nd].size()>=3)
upd(nd,height[adj[nd][2]]+1);
S.pb(nd);
cur+=(cnt[arr[nd]]++==0);
dfs2(adj[nd][1],nd);
if(!S.empty() and S.back()==nd)
cur-=(--cnt[arr[nd]]==0),S.ppb();
upd(nd,height[adj[nd][1]]+1);
for(int i=2;i<int(adj[nd].size());i++){
int to=adj[nd][i];
S.pb(nd);
cur+=(cnt[arr[nd]]++==0);
dfs2(to,nd);
if(!S.empty() and S.back()==nd)
cur-=(--cnt[arr[nd]]==0),S.ppb();
}
}
umax(ans[nd],cur);
}
int main(){
//freopen("file.in", "r", stdin);
scanf("%d%d",&n,&m);
for(int i=1;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
adj[u].pb(v);
adj[v].pb(u);
}
for(int i=1;i<=n;i++)
scanf("%d",arr+i);
dfs0(1);
for(int i=0;i<2;i++){
int mx=0,who=0;
for(int j=1;j<=n;j++)
if(umax(mx,lvl[j]))
who=j;
dfs1(who); dfs0(who); S.pb(who);
cur+=(cnt[arr[who]]++==0);
dfs2(adj[who][0],who);
}
for(int i=1;i<=n;i++)printf("%d\n",ans[i]);
return 0;
}
Compilation message
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
joi2019_ho_t5.cpp:72:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d%d",&u,&v);
| ~~~~~^~~~~~~~~~~~~~
joi2019_ho_t5.cpp:77:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%d",arr+i);
| ~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
4 ms |
5196 KB |
Output is correct |
5 |
Correct |
5 ms |
5140 KB |
Output is correct |
6 |
Correct |
4 ms |
5216 KB |
Output is correct |
7 |
Correct |
5 ms |
5196 KB |
Output is correct |
8 |
Correct |
5 ms |
5088 KB |
Output is correct |
9 |
Correct |
5 ms |
5068 KB |
Output is correct |
10 |
Correct |
5 ms |
5136 KB |
Output is correct |
11 |
Correct |
5 ms |
5068 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
5 ms |
5268 KB |
Output is correct |
14 |
Correct |
5 ms |
5068 KB |
Output is correct |
15 |
Correct |
4 ms |
5068 KB |
Output is correct |
16 |
Correct |
4 ms |
5104 KB |
Output is correct |
17 |
Correct |
5 ms |
5196 KB |
Output is correct |
18 |
Correct |
5 ms |
5196 KB |
Output is correct |
19 |
Correct |
5 ms |
5068 KB |
Output is correct |
20 |
Correct |
6 ms |
5196 KB |
Output is correct |
21 |
Correct |
5 ms |
5196 KB |
Output is correct |
22 |
Correct |
5 ms |
5068 KB |
Output is correct |
23 |
Correct |
5 ms |
5068 KB |
Output is correct |
24 |
Correct |
5 ms |
5068 KB |
Output is correct |
25 |
Correct |
5 ms |
5144 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
5 ms |
5196 KB |
Output is correct |
28 |
Correct |
5 ms |
5132 KB |
Output is correct |
29 |
Correct |
5 ms |
5196 KB |
Output is correct |
30 |
Correct |
4 ms |
5068 KB |
Output is correct |
31 |
Correct |
5 ms |
5196 KB |
Output is correct |
32 |
Correct |
5 ms |
5196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
11368 KB |
Output is correct |
2 |
Correct |
223 ms |
21248 KB |
Output is correct |
3 |
Correct |
30 ms |
7876 KB |
Output is correct |
4 |
Correct |
345 ms |
16000 KB |
Output is correct |
5 |
Correct |
465 ms |
32512 KB |
Output is correct |
6 |
Correct |
463 ms |
24136 KB |
Output is correct |
7 |
Correct |
376 ms |
16072 KB |
Output is correct |
8 |
Correct |
418 ms |
17636 KB |
Output is correct |
9 |
Correct |
414 ms |
17112 KB |
Output is correct |
10 |
Correct |
362 ms |
16960 KB |
Output is correct |
11 |
Correct |
206 ms |
16564 KB |
Output is correct |
12 |
Correct |
454 ms |
26116 KB |
Output is correct |
13 |
Correct |
363 ms |
24000 KB |
Output is correct |
14 |
Correct |
424 ms |
23484 KB |
Output is correct |
15 |
Correct |
184 ms |
16696 KB |
Output is correct |
16 |
Correct |
412 ms |
27336 KB |
Output is correct |
17 |
Correct |
391 ms |
24256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
282 ms |
14100 KB |
Output is correct |
2 |
Correct |
459 ms |
32876 KB |
Output is correct |
3 |
Correct |
37 ms |
8516 KB |
Output is correct |
4 |
Correct |
393 ms |
16812 KB |
Output is correct |
5 |
Correct |
481 ms |
34156 KB |
Output is correct |
6 |
Correct |
498 ms |
25100 KB |
Output is correct |
7 |
Correct |
379 ms |
16920 KB |
Output is correct |
8 |
Correct |
386 ms |
20292 KB |
Output is correct |
9 |
Correct |
376 ms |
19140 KB |
Output is correct |
10 |
Correct |
359 ms |
18108 KB |
Output is correct |
11 |
Correct |
328 ms |
17012 KB |
Output is correct |
12 |
Correct |
447 ms |
30564 KB |
Output is correct |
13 |
Correct |
400 ms |
24248 KB |
Output is correct |
14 |
Correct |
463 ms |
24256 KB |
Output is correct |
15 |
Correct |
197 ms |
16640 KB |
Output is correct |
16 |
Correct |
397 ms |
28564 KB |
Output is correct |
17 |
Correct |
463 ms |
25220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
4 ms |
5196 KB |
Output is correct |
5 |
Correct |
5 ms |
5140 KB |
Output is correct |
6 |
Correct |
4 ms |
5216 KB |
Output is correct |
7 |
Correct |
5 ms |
5196 KB |
Output is correct |
8 |
Correct |
5 ms |
5088 KB |
Output is correct |
9 |
Correct |
5 ms |
5068 KB |
Output is correct |
10 |
Correct |
5 ms |
5136 KB |
Output is correct |
11 |
Correct |
5 ms |
5068 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
5 ms |
5268 KB |
Output is correct |
14 |
Correct |
5 ms |
5068 KB |
Output is correct |
15 |
Correct |
4 ms |
5068 KB |
Output is correct |
16 |
Correct |
4 ms |
5104 KB |
Output is correct |
17 |
Correct |
5 ms |
5196 KB |
Output is correct |
18 |
Correct |
5 ms |
5196 KB |
Output is correct |
19 |
Correct |
5 ms |
5068 KB |
Output is correct |
20 |
Correct |
6 ms |
5196 KB |
Output is correct |
21 |
Correct |
5 ms |
5196 KB |
Output is correct |
22 |
Correct |
5 ms |
5068 KB |
Output is correct |
23 |
Correct |
5 ms |
5068 KB |
Output is correct |
24 |
Correct |
5 ms |
5068 KB |
Output is correct |
25 |
Correct |
5 ms |
5144 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
5 ms |
5196 KB |
Output is correct |
28 |
Correct |
5 ms |
5132 KB |
Output is correct |
29 |
Correct |
5 ms |
5196 KB |
Output is correct |
30 |
Correct |
4 ms |
5068 KB |
Output is correct |
31 |
Correct |
5 ms |
5196 KB |
Output is correct |
32 |
Correct |
5 ms |
5196 KB |
Output is correct |
33 |
Correct |
165 ms |
11368 KB |
Output is correct |
34 |
Correct |
223 ms |
21248 KB |
Output is correct |
35 |
Correct |
30 ms |
7876 KB |
Output is correct |
36 |
Correct |
345 ms |
16000 KB |
Output is correct |
37 |
Correct |
465 ms |
32512 KB |
Output is correct |
38 |
Correct |
463 ms |
24136 KB |
Output is correct |
39 |
Correct |
376 ms |
16072 KB |
Output is correct |
40 |
Correct |
418 ms |
17636 KB |
Output is correct |
41 |
Correct |
414 ms |
17112 KB |
Output is correct |
42 |
Correct |
362 ms |
16960 KB |
Output is correct |
43 |
Correct |
206 ms |
16564 KB |
Output is correct |
44 |
Correct |
454 ms |
26116 KB |
Output is correct |
45 |
Correct |
363 ms |
24000 KB |
Output is correct |
46 |
Correct |
424 ms |
23484 KB |
Output is correct |
47 |
Correct |
184 ms |
16696 KB |
Output is correct |
48 |
Correct |
412 ms |
27336 KB |
Output is correct |
49 |
Correct |
391 ms |
24256 KB |
Output is correct |
50 |
Correct |
282 ms |
14100 KB |
Output is correct |
51 |
Correct |
459 ms |
32876 KB |
Output is correct |
52 |
Correct |
37 ms |
8516 KB |
Output is correct |
53 |
Correct |
393 ms |
16812 KB |
Output is correct |
54 |
Correct |
481 ms |
34156 KB |
Output is correct |
55 |
Correct |
498 ms |
25100 KB |
Output is correct |
56 |
Correct |
379 ms |
16920 KB |
Output is correct |
57 |
Correct |
386 ms |
20292 KB |
Output is correct |
58 |
Correct |
376 ms |
19140 KB |
Output is correct |
59 |
Correct |
359 ms |
18108 KB |
Output is correct |
60 |
Correct |
328 ms |
17012 KB |
Output is correct |
61 |
Correct |
447 ms |
30564 KB |
Output is correct |
62 |
Correct |
400 ms |
24248 KB |
Output is correct |
63 |
Correct |
463 ms |
24256 KB |
Output is correct |
64 |
Correct |
197 ms |
16640 KB |
Output is correct |
65 |
Correct |
397 ms |
28564 KB |
Output is correct |
66 |
Correct |
463 ms |
25220 KB |
Output is correct |
67 |
Correct |
29 ms |
6876 KB |
Output is correct |
68 |
Correct |
168 ms |
18224 KB |
Output is correct |
69 |
Correct |
232 ms |
17504 KB |
Output is correct |
70 |
Correct |
359 ms |
16092 KB |
Output is correct |
71 |
Correct |
468 ms |
32484 KB |
Output is correct |
72 |
Correct |
428 ms |
24140 KB |
Output is correct |
73 |
Correct |
345 ms |
16084 KB |
Output is correct |
74 |
Correct |
419 ms |
19252 KB |
Output is correct |
75 |
Correct |
389 ms |
17440 KB |
Output is correct |
76 |
Correct |
390 ms |
17172 KB |
Output is correct |
77 |
Correct |
266 ms |
16320 KB |
Output is correct |
78 |
Correct |
444 ms |
27908 KB |
Output is correct |
79 |
Correct |
372 ms |
25912 KB |
Output is correct |
80 |
Correct |
395 ms |
22484 KB |
Output is correct |
81 |
Correct |
186 ms |
16692 KB |
Output is correct |
82 |
Correct |
377 ms |
27148 KB |
Output is correct |
83 |
Correct |
400 ms |
24064 KB |
Output is correct |
84 |
Correct |
349 ms |
16032 KB |
Output is correct |
85 |
Correct |
543 ms |
32692 KB |
Output is correct |
86 |
Correct |
500 ms |
24088 KB |
Output is correct |
87 |
Correct |
384 ms |
16044 KB |
Output is correct |
88 |
Correct |
416 ms |
19316 KB |
Output is correct |
89 |
Correct |
390 ms |
17676 KB |
Output is correct |
90 |
Correct |
370 ms |
17220 KB |
Output is correct |
91 |
Correct |
282 ms |
16232 KB |
Output is correct |
92 |
Correct |
480 ms |
32036 KB |
Output is correct |
93 |
Correct |
366 ms |
21696 KB |
Output is correct |
94 |
Correct |
373 ms |
20124 KB |
Output is correct |
95 |
Correct |
199 ms |
16692 KB |
Output is correct |
96 |
Correct |
389 ms |
27284 KB |
Output is correct |
97 |
Correct |
426 ms |
24172 KB |
Output is correct |
98 |
Correct |
393 ms |
16560 KB |
Output is correct |
99 |
Correct |
467 ms |
32820 KB |
Output is correct |
100 |
Correct |
441 ms |
24624 KB |
Output is correct |
101 |
Correct |
363 ms |
15968 KB |
Output is correct |
102 |
Correct |
396 ms |
18808 KB |
Output is correct |
103 |
Correct |
367 ms |
17624 KB |
Output is correct |
104 |
Correct |
374 ms |
17228 KB |
Output is correct |
105 |
Correct |
265 ms |
16188 KB |
Output is correct |
106 |
Correct |
482 ms |
25844 KB |
Output is correct |
107 |
Correct |
391 ms |
26008 KB |
Output is correct |
108 |
Correct |
422 ms |
21672 KB |
Output is correct |
109 |
Correct |
212 ms |
16452 KB |
Output is correct |
110 |
Correct |
414 ms |
27572 KB |
Output is correct |
111 |
Correct |
445 ms |
24488 KB |
Output is correct |