#include <bits/stdc++.h>
#define all(i) (i).begin(), (i).end()
using namespace std;
template<typename T1>
void Debug(bool _split, T1 i) {
if (_split)
cerr << ", ";
cerr << i;
}
template<typename T1, typename ...T2>
void Debug(bool _split, T1 i, T2 ...j) {
if (_split)
cerr << ", ";
cerr << i;
Debug(true, j...);
}
#define debug(args...) cout << "Line" << __LINE__ << " : [" << #args << "] is [", Debug(false, args), cerr << "]" << endl;
typedef pair<int, int> pi;
typedef long long ll;
const int inf = 0x3f3f3f3f, lg = 20;
const ll mod = 1e9 + 7, INF = 0x3f3f3f3f;
vector<vector<int>> g;
vector<int> pa, c, at, cnt, ans;
vector<pair<pi, pi>> mx;
deque<pi> dq;
pi dfs1(int i, int p) {
pa[i] = p;
pi res(0, i);
for (int j : g[i])
if (j != p) {
pi tmp = dfs1(j, i);
++tmp.first;
res = max(res, tmp);
}
return res;
}
int dfs2(int i, int p, int _v) {
mx[i].first = mx[i].second = pi(0, i);
for (int j : g[i])
if (j != p) {
pi tmp(dfs2(j, i, _v - 1) + 1, j);
if (tmp.first > mx[i].first.first)
mx[i].second = mx[i].first, mx[i].first = tmp;
else if (tmp.first > mx[i].second.first)
mx[i].second = tmp;
}
return mx[i].first.first;
}
void dfs3(int i, int p, int _v) {
if (at[_v] == -2)
at[_v] = c[i];
else
at[_v] = -1;
for (int j : g[i])
if (j != p)
dfs3(j, i, _v + 1);
}
void dfs4(int i, int p, int _v) {
int tmp;
if (mx[i].first.first > 0)
tmp = _v + mx[i].first.first;
else
tmp = _v;
vector<pi> stk;
while (dq.front().first <= tmp)
stk.push_back(dq.front()), dq.pop_front();
int now = ans[i] = dq.front().second;
if (!cnt[c[i]])
++now, dq.emplace_front(_v, now);
++cnt[c[i]];
for (int j : g[i])
if (j != p && j != mx[i].first.second)
dfs4(j, i, _v - 1);
--cnt[c[i]];
if (!cnt[c[i]])
--now, dq.pop_front();
if (mx[i].first.second != i) {
if (mx[i].second.first > 0)
tmp = _v + mx[i].second.first;
else
tmp = _v;
while (!stk.empty() && stk.back().first > tmp)
dq.push_front(stk.back()), stk.pop_back();
now = dq.front().second;
if (!cnt[c[i]])
++now, dq.emplace_front(_v, now);
++cnt[c[i]];
dfs4(mx[i].first.second, i, _v - 1);
--cnt[c[i]];
if (!cnt[c[i]])
--now, dq.pop_front();
}
while (!stk.empty())
dq.push_front(stk.back()), stk.pop_back();
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, _;
cin >> n >> _;
g.resize(n);
pa.resize(n);
at.resize(n);
mx.resize(n);
ans.resize(n, -1);
for (int i = 0; i < n - 1; ++i) {
int x, y;
cin >> x >> y;
--x, --y;
g[x].push_back(y), g[y].push_back(x);
}
c.resize(n);
for (int &i : c)
cin >> i, --i;
int x = dfs1(0, -1).second;
int y = dfs1(x, -1).second;
vector<int> dm;
for (int i = y; ~i; i = pa[i])
dm.push_back(i);
x = dm.size() - 1 >> 1;//2k -> k-1, 2k+1 -> k
dfs2(dm[x], dm[x + 1], 0);
at.assign(n, -2);
dfs3(dm[x + 1], dm[x], 1);
cnt.assign(n, 0);
int now = 0;
dq.emplace_back(inf, 0);
for (int i = n - 1; i; --i)
if (at[i] >= 0) {
if (!cnt[at[i]])
++now;
++cnt[at[i]];
dq.emplace_front(i, now);
}
dfs4(dm[x], dm[x + 1], 0);
x = dm.size() >> 1;//2k -> k, 2k+1 -> k
dfs2(dm[x], dm[x - 1], 0);
at.assign(n, -2);
dfs3(dm[x - 1], dm[x], 1);
cnt.assign(n, 0);
now = 0;
dq.clear();
dq.emplace_back(inf, 0);
for (int i = n - 1; i; --i)
if (at[i] >= 0) {
if (!cnt[at[i]])
++now;
++cnt[at[i]];
dq.emplace_front(i, now);
}
dfs4(dm[x], dm[x - 1], 0);
for (int i : ans)
cout << i << '\n';
}
Compilation message
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:140:19: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
140 | x = dm.size() - 1 >> 1;//2k -> k-1, 2k+1 -> k
| ~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
3 ms |
460 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
109 ms |
11084 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
220 ms |
15024 KB |
Output is correct |
2 |
Runtime error |
711 ms |
274436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
3 ms |
460 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |