#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
int n, m;
vector<vector<int>> g;
vector<int> C;
void init(){
g.resize(n + 1);
C.resize(n + 1);
}
int fd = -1, fv = -1;
void dfsfar(int now, int p, int d){
if(d > fd) fd = d, fv = now;
for(int i : g[now]){
if(i == p) continue;
dfsfar(i, now, d + 1);
}
}
struct Solver{
vector<int> dep, cnt, dis, ans;
vector<int> st;
int tans = 0;
void add(int id){
st.eb(id);
cnt[C[id]]++;
if(cnt[C[id]] == 1) tans++;
}
void remove(){
int id = st.back();
st.pob;
cnt[C[id]]--;
if(cnt[C[id]] == 0) tans--;
}
void dfs1(int now, int p, int d){
dis[now] = d;
for(int i : g[now]){
if(i == p) continue;
dfs1(i, now, d + 1);
dep[now] = max(dep[i] + 1, dep[now]);
}
}
void dfs2(int now, int p){
int mx1 = -1, mx2 = -1;
for(int i : g[now]){
if(i == p) continue;
if(mx1 == -1 || dep[i] > dep[mx1]) swap(mx1, i);
if(i == -1) continue;
if(mx2 == -1 || dep[i] > dep[mx2]) swap(mx2, i);
}
if(mx1 == -1){
ans[now] = tans;
return;
}
if(mx2 != -1){
while(!st.empty() && dis[st.back()] >= dis[now] - dep[mx2] - 1) remove();
}
add(now);
dfs2(mx1, now);
if(!st.empty() && st.back() == now) remove();
while(!st.empty() && dis[st.back()] >= dis[now] - dep[mx1] - 1) remove();
add(now);
for(int i : g[now]){
if(i == p || i == mx1) continue;
dfs2(i, now);
}
if(!st.empty() && st.back() == now) remove();
//cerr << "dfs2 " << now << " " << mx1 << " " << mx2 << "\n";
//printv(st, cerr);
ans[now] = tans;
}
void solve(int s){
cnt.resize(n + 1);
dep.resize(n + 1);
dis.resize(n + 1);
ans.resize(n + 1);
dfs1(s, s, 0);
dfs2(s, s);
/*cerr << "solve " << s << "\n";
printv(dep, cerr);
printv(dis, cerr);
printv(ans, cerr);*/
}
};
int main(){
StarBurstStream
cin >> n >> m;
init();
for(int i = 0; i < n - 1; i++){
int u, v;
cin >> u >> v;
g[u].eb(v);
g[v].eb(u);
}
for(int i = 1; i <= n; i++) cin >> C[i];
dfsfar(1, 1, 0);
int v1 = fv;
fv = -1; fd = -1;
dfsfar(v1, v1, 0);
int v2 = fv;
//cerr << v1 << " " << v2 << "\n";
Solver s1, s2;
s1.solve(v1);
s2.solve(v2);
for(int i = 1; i <= n; i++){
cout << max(s1.ans[i], s2.ans[i]) << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
145 ms |
12612 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
268 ms |
17840 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |