/* made by amunduzbaev */
//~ #include <ext/pb_ds/assoc_container.hpp>
//~ #include <ext/pb_ds/tree_policy.hpp>
#include "bits/stdc++.h"
using namespace std;
//~ using namespace __gnu_pbds;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define NeedForSpeed ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vv vector
#define mem(arr, v) memset(arr, v, sizeof arr)
#define int long long
#define degub(x) cout<<#x<<" : "<<x<<"\n"
#define GG cout<<"here\n";
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vii;
typedef vector<pii> vpii;
template<class T> bool umin(T& a, const T& b) { return a > b ? a = b, true : false; }
template<class T> bool umax(T& a, const T& b) { return a < b ? a = b, true : false; }
template<int sz> using tut = array<int, sz>;
void usaco(string s) { freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout); NeedForSpeed }
//~ template<class T> using oset = tree<T,
//~ null_type, less_equal<T>, rb_tree_tag,
//~ tree_order_statistics_node_update> ordered_set;
const int N = 2e5+5;
const int mod = 1e9+7;
const ll inf = 1e18;
const ld Pi = acos(-1);
#define MULTI 0
int n, m, k, s, t, q, ans, res[N], a[N];
int d1, d2, d[N], mx[N];
vii edges[N];
void dfs(int u, int p = -1){ mx[u] = 0;
for(auto x : edges[u]){
if(x == p) continue;
d[x] = d[u] + 1, dfs(x, u);
umax(mx[u], mx[x] + 1);
}
}
vpii ss;
map<int, int> mm;
void dfs2(int u, int d = 0, int p = -1){
int mxx = 0, mx2 = 0;
for(auto x : edges[u]){
if(x == p) continue;
if(mx[mxx] < mx[x] + 1) mx2 = mxx, mxx = x;
else if(mx[mx2] < mx[x] + 1) mx2 = x;
}
if(mx2){
int del = d - (mx[mx2] + 1);
while(!ss.empty() && ss.back().ff >= del){
mm[a[ss.back().ss]]--;
if(!mm[a[ss.back().ss]]) mm.erase(a[ss.back().ss]);
ss.pop_back();
}
}
if(mxx){
ss.pb({d, u}), mm[a[u]]++;
dfs2(mxx, d+1, u);
int del = d - (mx[mxx] + 1);
while(!ss.empty() && ss.back().ff >= del){
mm[a[ss.back().ss]]--;
if(!mm[a[ss.back().ss]]) mm.erase(a[ss.back().ss]);
ss.pop_back();
}
}
for(auto x : edges[u]){
if(x == p || x == mxx) continue;
if(ss.empty() || ss.back().ss != u) ss.pb({d, u}), mm[a[u]]++;
dfs2(x, d+1, u);
}
while(!ss.empty() && ss.back().ff >= d){
mm[a[ss.back().ss]]--;
if(!mm[a[ss.back().ss]]) mm.erase(a[ss.back().ss]);
ss.pop_back();
}
//~ cout<<u<<"\n";
//~ for(auto x : ss) cout<<x.ff<<" "<<x.ss<<"\n";
//~ cout<<"\n";
//~ for(auto x : mm) cout<<x.ff<<" "<<x.ss<<"\n";
//~ cout<<"\n";
umax(res[u], sz(mm));
}
void solve(int t_case){
cin>>n>>m;
for(int i=1;i<n;i++){
int a, b; cin>>a>>b;
edges[a].pb(b), edges[b].pb(a);
} for(int i=1;i<=n;i++) cin>>a[i];
dfs(1); d1 = 1;
for(int i=1;i<=n;i++) if(d[d1] < d[i]) d1 = i;
d[d1] = 0, dfs(d1), d2 = d1;
for(int i=1;i<=n;i++) if(d[d2] < d[i]) d2 = i;
//~ cout<<d1<<"\n";
dfs2(d1);
d[d2] = 0;
//~ cout<<d2<<"\n";
dfs(d2), dfs2(d2);
for(int i=1;i<=n;i++) cout<<res[i]<<"\n";
}
/*
10 10
6 4
9 6
6 3
10 5
10 9
8 1
4 1
2 6
7 3
10 10 3 2 8 5 8 7 3 3
*/
signed main(){
NeedForSpeed
if(MULTI){
int t; cin>>t;
for(int t_case = 1; t_case <= t; t_case++) solve(t_case);
} else solve(1);
return 0;
}
Compilation message
joi2019_ho_t5.cpp: In function 'void usaco(std::string)':
joi2019_ho_t5.cpp:34:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | void usaco(string s) { freopen((s+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:35:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen((s+".out").c_str(),"w",stdout); NeedForSpeed }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
6 ms |
5196 KB |
Output is correct |
3 |
Correct |
7 ms |
5244 KB |
Output is correct |
4 |
Correct |
6 ms |
5324 KB |
Output is correct |
5 |
Correct |
6 ms |
5100 KB |
Output is correct |
6 |
Correct |
7 ms |
5452 KB |
Output is correct |
7 |
Correct |
5 ms |
5324 KB |
Output is correct |
8 |
Correct |
5 ms |
5160 KB |
Output is correct |
9 |
Correct |
5 ms |
5196 KB |
Output is correct |
10 |
Correct |
5 ms |
5160 KB |
Output is correct |
11 |
Correct |
5 ms |
5148 KB |
Output is correct |
12 |
Correct |
5 ms |
5196 KB |
Output is correct |
13 |
Correct |
6 ms |
5464 KB |
Output is correct |
14 |
Correct |
5 ms |
5292 KB |
Output is correct |
15 |
Correct |
7 ms |
5196 KB |
Output is correct |
16 |
Correct |
5 ms |
5104 KB |
Output is correct |
17 |
Correct |
5 ms |
5324 KB |
Output is correct |
18 |
Correct |
5 ms |
5324 KB |
Output is correct |
19 |
Correct |
5 ms |
5196 KB |
Output is correct |
20 |
Correct |
7 ms |
5580 KB |
Output is correct |
21 |
Correct |
6 ms |
5324 KB |
Output is correct |
22 |
Correct |
5 ms |
5196 KB |
Output is correct |
23 |
Correct |
5 ms |
5196 KB |
Output is correct |
24 |
Correct |
5 ms |
5196 KB |
Output is correct |
25 |
Correct |
6 ms |
5196 KB |
Output is correct |
26 |
Correct |
5 ms |
5164 KB |
Output is correct |
27 |
Correct |
7 ms |
5484 KB |
Output is correct |
28 |
Correct |
6 ms |
5452 KB |
Output is correct |
29 |
Correct |
6 ms |
5324 KB |
Output is correct |
30 |
Correct |
4 ms |
5196 KB |
Output is correct |
31 |
Correct |
6 ms |
5452 KB |
Output is correct |
32 |
Correct |
7 ms |
5324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
181 ms |
13148 KB |
Output is correct |
2 |
Correct |
256 ms |
32224 KB |
Output is correct |
3 |
Correct |
30 ms |
9516 KB |
Output is correct |
4 |
Correct |
413 ms |
21904 KB |
Output is correct |
5 |
Correct |
504 ms |
52264 KB |
Output is correct |
6 |
Correct |
498 ms |
36084 KB |
Output is correct |
7 |
Correct |
417 ms |
21796 KB |
Output is correct |
8 |
Correct |
431 ms |
24772 KB |
Output is correct |
9 |
Correct |
455 ms |
23856 KB |
Output is correct |
10 |
Correct |
377 ms |
23748 KB |
Output is correct |
11 |
Correct |
187 ms |
22400 KB |
Output is correct |
12 |
Correct |
449 ms |
40516 KB |
Output is correct |
13 |
Correct |
396 ms |
36420 KB |
Output is correct |
14 |
Correct |
414 ms |
35028 KB |
Output is correct |
15 |
Correct |
178 ms |
22308 KB |
Output is correct |
16 |
Correct |
409 ms |
42600 KB |
Output is correct |
17 |
Correct |
415 ms |
36280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
303 ms |
16188 KB |
Output is correct |
2 |
Correct |
988 ms |
63708 KB |
Output is correct |
3 |
Correct |
61 ms |
10564 KB |
Output is correct |
4 |
Correct |
432 ms |
22780 KB |
Output is correct |
5 |
Correct |
1099 ms |
66428 KB |
Output is correct |
6 |
Correct |
698 ms |
40348 KB |
Output is correct |
7 |
Correct |
396 ms |
22588 KB |
Output is correct |
8 |
Correct |
523 ms |
28996 KB |
Output is correct |
9 |
Correct |
470 ms |
26720 KB |
Output is correct |
10 |
Correct |
458 ms |
25072 KB |
Output is correct |
11 |
Correct |
326 ms |
22840 KB |
Output is correct |
12 |
Correct |
882 ms |
55256 KB |
Output is correct |
13 |
Correct |
560 ms |
38520 KB |
Output is correct |
14 |
Correct |
630 ms |
37816 KB |
Output is correct |
15 |
Correct |
191 ms |
23428 KB |
Output is correct |
16 |
Correct |
738 ms |
52048 KB |
Output is correct |
17 |
Correct |
673 ms |
40360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
6 ms |
5196 KB |
Output is correct |
3 |
Correct |
7 ms |
5244 KB |
Output is correct |
4 |
Correct |
6 ms |
5324 KB |
Output is correct |
5 |
Correct |
6 ms |
5100 KB |
Output is correct |
6 |
Correct |
7 ms |
5452 KB |
Output is correct |
7 |
Correct |
5 ms |
5324 KB |
Output is correct |
8 |
Correct |
5 ms |
5160 KB |
Output is correct |
9 |
Correct |
5 ms |
5196 KB |
Output is correct |
10 |
Correct |
5 ms |
5160 KB |
Output is correct |
11 |
Correct |
5 ms |
5148 KB |
Output is correct |
12 |
Correct |
5 ms |
5196 KB |
Output is correct |
13 |
Correct |
6 ms |
5464 KB |
Output is correct |
14 |
Correct |
5 ms |
5292 KB |
Output is correct |
15 |
Correct |
7 ms |
5196 KB |
Output is correct |
16 |
Correct |
5 ms |
5104 KB |
Output is correct |
17 |
Correct |
5 ms |
5324 KB |
Output is correct |
18 |
Correct |
5 ms |
5324 KB |
Output is correct |
19 |
Correct |
5 ms |
5196 KB |
Output is correct |
20 |
Correct |
7 ms |
5580 KB |
Output is correct |
21 |
Correct |
6 ms |
5324 KB |
Output is correct |
22 |
Correct |
5 ms |
5196 KB |
Output is correct |
23 |
Correct |
5 ms |
5196 KB |
Output is correct |
24 |
Correct |
5 ms |
5196 KB |
Output is correct |
25 |
Correct |
6 ms |
5196 KB |
Output is correct |
26 |
Correct |
5 ms |
5164 KB |
Output is correct |
27 |
Correct |
7 ms |
5484 KB |
Output is correct |
28 |
Correct |
6 ms |
5452 KB |
Output is correct |
29 |
Correct |
6 ms |
5324 KB |
Output is correct |
30 |
Correct |
4 ms |
5196 KB |
Output is correct |
31 |
Correct |
6 ms |
5452 KB |
Output is correct |
32 |
Correct |
7 ms |
5324 KB |
Output is correct |
33 |
Correct |
181 ms |
13148 KB |
Output is correct |
34 |
Correct |
256 ms |
32224 KB |
Output is correct |
35 |
Correct |
30 ms |
9516 KB |
Output is correct |
36 |
Correct |
413 ms |
21904 KB |
Output is correct |
37 |
Correct |
504 ms |
52264 KB |
Output is correct |
38 |
Correct |
498 ms |
36084 KB |
Output is correct |
39 |
Correct |
417 ms |
21796 KB |
Output is correct |
40 |
Correct |
431 ms |
24772 KB |
Output is correct |
41 |
Correct |
455 ms |
23856 KB |
Output is correct |
42 |
Correct |
377 ms |
23748 KB |
Output is correct |
43 |
Correct |
187 ms |
22400 KB |
Output is correct |
44 |
Correct |
449 ms |
40516 KB |
Output is correct |
45 |
Correct |
396 ms |
36420 KB |
Output is correct |
46 |
Correct |
414 ms |
35028 KB |
Output is correct |
47 |
Correct |
178 ms |
22308 KB |
Output is correct |
48 |
Correct |
409 ms |
42600 KB |
Output is correct |
49 |
Correct |
415 ms |
36280 KB |
Output is correct |
50 |
Correct |
303 ms |
16188 KB |
Output is correct |
51 |
Correct |
988 ms |
63708 KB |
Output is correct |
52 |
Correct |
61 ms |
10564 KB |
Output is correct |
53 |
Correct |
432 ms |
22780 KB |
Output is correct |
54 |
Correct |
1099 ms |
66428 KB |
Output is correct |
55 |
Correct |
698 ms |
40348 KB |
Output is correct |
56 |
Correct |
396 ms |
22588 KB |
Output is correct |
57 |
Correct |
523 ms |
28996 KB |
Output is correct |
58 |
Correct |
470 ms |
26720 KB |
Output is correct |
59 |
Correct |
458 ms |
25072 KB |
Output is correct |
60 |
Correct |
326 ms |
22840 KB |
Output is correct |
61 |
Correct |
882 ms |
55256 KB |
Output is correct |
62 |
Correct |
560 ms |
38520 KB |
Output is correct |
63 |
Correct |
630 ms |
37816 KB |
Output is correct |
64 |
Correct |
191 ms |
23428 KB |
Output is correct |
65 |
Correct |
738 ms |
52048 KB |
Output is correct |
66 |
Correct |
673 ms |
40360 KB |
Output is correct |
67 |
Correct |
31 ms |
7364 KB |
Output is correct |
68 |
Correct |
252 ms |
27964 KB |
Output is correct |
69 |
Correct |
348 ms |
25596 KB |
Output is correct |
70 |
Correct |
418 ms |
21896 KB |
Output is correct |
71 |
Correct |
486 ms |
52180 KB |
Output is correct |
72 |
Correct |
468 ms |
36080 KB |
Output is correct |
73 |
Correct |
388 ms |
21780 KB |
Output is correct |
74 |
Correct |
441 ms |
27476 KB |
Output is correct |
75 |
Correct |
407 ms |
24392 KB |
Output is correct |
76 |
Correct |
410 ms |
24116 KB |
Output is correct |
77 |
Correct |
289 ms |
21912 KB |
Output is correct |
78 |
Correct |
473 ms |
43836 KB |
Output is correct |
79 |
Correct |
418 ms |
40072 KB |
Output is correct |
80 |
Correct |
431 ms |
33436 KB |
Output is correct |
81 |
Correct |
162 ms |
22084 KB |
Output is correct |
82 |
Correct |
390 ms |
42280 KB |
Output is correct |
83 |
Correct |
407 ms |
36128 KB |
Output is correct |
84 |
Correct |
421 ms |
22268 KB |
Output is correct |
85 |
Correct |
547 ms |
52988 KB |
Output is correct |
86 |
Correct |
506 ms |
36548 KB |
Output is correct |
87 |
Correct |
412 ms |
22036 KB |
Output is correct |
88 |
Correct |
461 ms |
28352 KB |
Output is correct |
89 |
Correct |
439 ms |
25284 KB |
Output is correct |
90 |
Correct |
439 ms |
24612 KB |
Output is correct |
91 |
Correct |
311 ms |
22328 KB |
Output is correct |
92 |
Correct |
566 ms |
51820 KB |
Output is correct |
93 |
Correct |
438 ms |
32324 KB |
Output is correct |
94 |
Correct |
451 ms |
29780 KB |
Output is correct |
95 |
Correct |
179 ms |
22532 KB |
Output is correct |
96 |
Correct |
433 ms |
42872 KB |
Output is correct |
97 |
Correct |
477 ms |
36716 KB |
Output is correct |
98 |
Correct |
417 ms |
22784 KB |
Output is correct |
99 |
Correct |
634 ms |
53492 KB |
Output is correct |
100 |
Correct |
712 ms |
39672 KB |
Output is correct |
101 |
Correct |
447 ms |
22456 KB |
Output is correct |
102 |
Correct |
498 ms |
27380 KB |
Output is correct |
103 |
Correct |
501 ms |
25500 KB |
Output is correct |
104 |
Correct |
505 ms |
24916 KB |
Output is correct |
105 |
Correct |
309 ms |
22712 KB |
Output is correct |
106 |
Correct |
701 ms |
42016 KB |
Output is correct |
107 |
Correct |
564 ms |
41348 KB |
Output is correct |
108 |
Correct |
538 ms |
32396 KB |
Output is correct |
109 |
Correct |
169 ms |
23212 KB |
Output is correct |
110 |
Correct |
544 ms |
45288 KB |
Output is correct |
111 |
Correct |
637 ms |
39736 KB |
Output is correct |