//gm --- akezhon
#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #define int long long
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define tm (tl+tr)/2
#define TL v+v, tl, tm
#define TR v+v+1, tm+1, tr
#define DA l <= tl && tr <= r
#define NET r < tl || tr < l
#define double long double
using namespace std;
const int N=2e5+7;
const int M=1e9+7;
const int inf=1e9;
int c[N], tin[N], tout[N], eul[N], sz[N], ans[N], big[N];
vector <pair<int*, int>> rb;
vector <int> g[N];
int n, timer, cnt;
int ok[N];
void dfs(int v=1, int p=0){
tin[v] = ++timer;
eul[timer] = v;
sz[v] = 1;
for(int to : g[v]){
if(to == p)continue;
dfs(to, v);
sz[v] += sz[to];
if(sz[to] > sz[big[v]])big[v] = to;
}
tout[v] = timer;
}
void roll(int &x){
rb.push_back({&x, x});
}
void upd(int v){
if(!ok[c[v]]){
roll(ok[c[v]]);
roll(cnt);
cnt++;
ok[c[v]] = 1;
}
}
void go(int v=1, int p=0, int cl=0){
int temp = rb.size();
for(int to: g[v]){
if(to != p && to != big[v])go(to, v, 1);
}
if(big[v])go(big[v], v, 0);
upd(v);
for(int to: g[v]){
if(to == p || to == big[v])continue;
for(int i = tin[to]; i <= tout[to]; i++)upd(eul[i]);
}
ans[v] = cnt;
if(cl){
while(rb.size()>temp){
*(rb.back().F) = rb.back().S;
rb.pop_back();
}
}
}
void AlemAmenov(){
cin >> n;
map<int, int>mp;
int cc=0;
for(int i=1; i <= n; i++){
cin >> c[i];
if(!mp[c[i]])mp[c[i]] = ++cc;
c[i] = mp[c[i]];
}
for(int i=1; i < n; i++){
int a, b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
dfs();
go();
for(int i=1; i <= n; i++){
cout << ans[i] << ' ';
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("justforfood.in", "r", stdin);
// freopen("justforfood.out", "w", stdout);
int RealName=1;
// cin >> RealName;
// int C=0;
while(RealName--){
// cout << "Case " << ++C << ":\n";
AlemAmenov();
}
return 0;
}
Compilation message
rot.cpp: In function 'void go(int, int, int)':
rot.cpp:63:18: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int*, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
63 | while(rb.size()>temp){
| ~~~~~~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
10584 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
10584 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
10840 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
11100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
13148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
14464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
52 ms |
16632 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
64 ms |
19744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
81 ms |
20472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
82 ms |
20940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |