/* 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);
}
}
void dfs2(int u, int d = 0, int p = -1, vpii ss = {}, map<int, int> mm = {}){
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, ss, mm);
mm[a[u]]--;
if(!mm[a[u]]) mm.erase(a[u]);
ss.pop_back();
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();
}
} ss.pb({d, u}), mm[a[u]]++;
for(auto x : edges[u]){
if(x == p || x == mxx) continue;
dfs2(x, d+1, u, ss, mm);
}
//~ cout<<u<<" "<<mxx<<" "<<mx2<<"\n";
mm[a[u]]--;
if(!mm[a[u]]) mm.erase(a[u]);
ss.pop_back();
//~ 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<<"\n"<<d1<<"\n";
dfs2(d1);
//~ cout<<"\n"<<d2<<"\n";
dfs(d2), dfs2(d2);
//~ cout<<"\n";
for(int i=1;i<=n;i++) cout<<res[i]<<"\n";
}
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 }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
7 ms |
5196 KB |
Output is correct |
3 |
Correct |
133 ms |
39888 KB |
Output is correct |
4 |
Correct |
80 ms |
20804 KB |
Output is correct |
5 |
Correct |
6 ms |
5196 KB |
Output is correct |
6 |
Correct |
114 ms |
75736 KB |
Output is correct |
7 |
Correct |
26 ms |
13280 KB |
Output is correct |
8 |
Correct |
6 ms |
5196 KB |
Output is correct |
9 |
Correct |
6 ms |
5324 KB |
Output is correct |
10 |
Correct |
6 ms |
5324 KB |
Output is correct |
11 |
Correct |
7 ms |
5292 KB |
Output is correct |
12 |
Correct |
5 ms |
5196 KB |
Output is correct |
13 |
Correct |
99 ms |
63872 KB |
Output is correct |
14 |
Correct |
11 ms |
9464 KB |
Output is correct |
15 |
Correct |
13 ms |
7760 KB |
Output is correct |
16 |
Correct |
5 ms |
5212 KB |
Output is correct |
17 |
Correct |
53 ms |
38052 KB |
Output is correct |
18 |
Correct |
25 ms |
13136 KB |
Output is correct |
19 |
Correct |
6 ms |
5212 KB |
Output is correct |
20 |
Correct |
550 ms |
150428 KB |
Output is correct |
21 |
Correct |
142 ms |
27020 KB |
Output is correct |
22 |
Correct |
6 ms |
5196 KB |
Output is correct |
23 |
Correct |
7 ms |
5476 KB |
Output is correct |
24 |
Correct |
9 ms |
5324 KB |
Output is correct |
25 |
Correct |
7 ms |
5196 KB |
Output is correct |
26 |
Correct |
5 ms |
5196 KB |
Output is correct |
27 |
Correct |
250 ms |
70440 KB |
Output is correct |
28 |
Correct |
195 ms |
54532 KB |
Output is correct |
29 |
Correct |
61 ms |
16708 KB |
Output is correct |
30 |
Correct |
6 ms |
5160 KB |
Output is correct |
31 |
Correct |
146 ms |
49348 KB |
Output is correct |
32 |
Correct |
139 ms |
26820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
212 ms |
14776 KB |
Output is correct |
2 |
Runtime error |
235 ms |
274436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
425 ms |
19192 KB |
Output is correct |
2 |
Runtime error |
413 ms |
274436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
7 ms |
5196 KB |
Output is correct |
3 |
Correct |
133 ms |
39888 KB |
Output is correct |
4 |
Correct |
80 ms |
20804 KB |
Output is correct |
5 |
Correct |
6 ms |
5196 KB |
Output is correct |
6 |
Correct |
114 ms |
75736 KB |
Output is correct |
7 |
Correct |
26 ms |
13280 KB |
Output is correct |
8 |
Correct |
6 ms |
5196 KB |
Output is correct |
9 |
Correct |
6 ms |
5324 KB |
Output is correct |
10 |
Correct |
6 ms |
5324 KB |
Output is correct |
11 |
Correct |
7 ms |
5292 KB |
Output is correct |
12 |
Correct |
5 ms |
5196 KB |
Output is correct |
13 |
Correct |
99 ms |
63872 KB |
Output is correct |
14 |
Correct |
11 ms |
9464 KB |
Output is correct |
15 |
Correct |
13 ms |
7760 KB |
Output is correct |
16 |
Correct |
5 ms |
5212 KB |
Output is correct |
17 |
Correct |
53 ms |
38052 KB |
Output is correct |
18 |
Correct |
25 ms |
13136 KB |
Output is correct |
19 |
Correct |
6 ms |
5212 KB |
Output is correct |
20 |
Correct |
550 ms |
150428 KB |
Output is correct |
21 |
Correct |
142 ms |
27020 KB |
Output is correct |
22 |
Correct |
6 ms |
5196 KB |
Output is correct |
23 |
Correct |
7 ms |
5476 KB |
Output is correct |
24 |
Correct |
9 ms |
5324 KB |
Output is correct |
25 |
Correct |
7 ms |
5196 KB |
Output is correct |
26 |
Correct |
5 ms |
5196 KB |
Output is correct |
27 |
Correct |
250 ms |
70440 KB |
Output is correct |
28 |
Correct |
195 ms |
54532 KB |
Output is correct |
29 |
Correct |
61 ms |
16708 KB |
Output is correct |
30 |
Correct |
6 ms |
5160 KB |
Output is correct |
31 |
Correct |
146 ms |
49348 KB |
Output is correct |
32 |
Correct |
139 ms |
26820 KB |
Output is correct |
33 |
Correct |
212 ms |
14776 KB |
Output is correct |
34 |
Runtime error |
235 ms |
274436 KB |
Execution killed with signal 9 |
35 |
Halted |
0 ms |
0 KB |
- |