This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/// In The Name Of God
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define pp pop_back
#define mp make_pair
#define sz(x) (int)x.size()
#define sqr(x) ((x) * 1ll * (x))
#define all(x) x.begin(), x.end()
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, l, r) for (int i = (l); i >= (r); i--)
#define Kazakhstan ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define nl '\n'
#define ioi exit(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = (int)2e5 + 7;
const int inf = (int)1e9 + 7;
const int mod = (int)1e9 + 7;
const ll linf = (ll)1e18 + 7;
const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
using namespace std;
int n, m;
int a[N];
vector <int> g[N];
struct S {
int sz, df;
int st[N], cnt[N];
void add(int c) {
df += cnt[c]++ == 0;
}
void del(int c) {
df -= --cnt[c] == 0;
}
void push(int v) {
st[++sz] = v;
add(a[v]);
}
void pop() {
del(a[st[sz--]]);
}
bool empty() {
return sz == 0;
}
int back() {
return st[sz];
}
} T;
inline void dfs(int v, int p, vector <int> &dis) {
dis[v] = dis[p] + 1;
for (auto to : g[v]) {
if (to != p) dfs(to, v, dis);
}
}
int d[N], ans[N];
inline void prec(int v, int p) {
d[v] = 1;
for (auto to : g[v]) {
if (to != p) {
prec(to, v);
d[v] = max(d[v], d[to] + 1);
}
}
}
inline void solve(int v, int p, vector <int> &dep) {
vector < pair <int, int > > ch;
for (auto to : g[v]) {
if (to != p) {
ch.pb({d[to], to});
}
}
if (sz(ch)) swap(ch[0], *max_element(all(ch)));
int len = (sz(ch) > 1) ? max_element(ch.begin() + 1, ch.end()) -> f : 0;
if (sz(ch)) {
for (auto to : ch) {
while (!T.empty() && dep[T.back()] >= dep[v] - len) T.pop();
T.push(v);
solve(to.s, v, dep);
if (!T.empty() && T.back() == v) T.pop();
len = max(len, to.f);
}
}
while (!T.empty() && dep[T.back()] >= dep[v] - len) T.pop();
// if (v == 1) {
// cerr << T.back() << ' ' << dep[T.back()] << ' ' << dep[v] << nl;
// cerr << len << " -> " << sz(ch) << " -> " << T.sz << " -> ";
// rep(i, 1, T.sz) cerr << T.st[i] << ' ';
// cerr << nl;
// }
ans[v] = max(ans[v], T.df);
}
int main() {
#ifdef IOI
freopen ("in.txt", "r", stdin);
freopen ("res.txt", "w", stdout);
#endif
Kazakhstan
cin >> n >> m;
rep(i, 2, n) {
int v, u;
cin >> v >> u;
g[v].pb(u);
g[u].pb(v);
}
rep(i, 1, n) {
cin >> a[i];
}
vector <int> d1(n + 1);
dfs(1, 0, d1);
int A = max_element(all(d1)) - d1.begin();
prec(A, 0);
dfs(A, 0, d1);
solve(A, 0, d1);
int B = max_element(all(d1)) - d1.begin();
prec(B, 0);
dfs(B, 0, d1);
solve(B, 0, d1);
rep(i, 1, n) {
cout << ans[i] << nl;
}
ioi
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |