This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<cstdio>
#include<algorithm>
#include<vector>
#define N_ 201000
using namespace std;
int n, m, Col[N_], Num[N_], cnt, Ed[N_], S[N_];
vector<int>E[N_], Ch[N_];
int D1[N_], D2[N_], P1[N_], par[N_][20], Dep[N_];
struct Range {
int b, e;
bool operator<(const Range &p)const {
return b < p.b;
}
};
vector<Range>G[N_];
void DFS1(int a, int pp) {
Num[a] = ++cnt;
par[a][0] = pp;
for (int i = 0; i < 18; i++)par[a][i + 1] = par[par[a][i]][i];
P1[a] = a;
for (auto &x : E[a]) {
if (x == pp)continue;
Dep[x] = Dep[a] + 1;
DFS1(x, a);
Ch[a].push_back(x);
D2[a] = max(D2[a], D1[a] + D1[x] + 1);
D2[a] = max(D2[a], D2[x]);
if (D1[a] < D1[x] + 1) {
D1[a] = D1[x] + 1;
P1[a] = P1[x];
}
}
Ed[a] = cnt;
}
int Up(int a, int d) {
int i = 0;
while (d) {
if (d & 1)a = par[a][i];
d >>= 1, i++;
}
return a;
}
int LCA(int a, int b) {
if (Dep[a] < Dep[b])return LCA(b, a);
a = Up(a, Dep[a] - Dep[b]);
for (int i = 18; i >= 0; i--) {
if (par[a][i] != par[b][i])a = par[a][i], b = par[b][i];
}
if (a != b)a = par[a][0];
return a;
}
void Put(int col, int b, int e) {
if (b > e)return;
G[col].push_back({ b,e });
}
void Add(int a, int x) {
int l = LCA(a, x);
int d = Dep[a] + Dep[x] - Dep[l] * 2;
if (Dep[a] <= Dep[x]) {
int t = Up(x, (d - 1) / 2);
Put(Col[a], Num[t], Ed[t]);
}
else {
int t = Up(a, d / 2);
Put(Col[a], Num[1], Num[t] - 1);
Put(Col[a], Ed[t] + 1, Ed[1]);
}
}
void DFS2(int a, int pp, int d1, int d2, int p1) {
if (pp) {
if (d1 > d2) {
Add(a, p1);
}
}
int dd2[2][2] = { {-3,0},{-3,0} }, dd1[3][2] = { {0,a},{-3,0},{ -3,0 } }, i;
for (auto &x : Ch[a]) {
if (D2[x] < D1[x] + 1) {
Add(a, P1[x]);
}
if (dd2[0][0] < D2[x]) {
dd2[1][0] = dd2[0][0], dd2[1][1] = dd2[0][1];
dd2[0][0] = D2[x], dd2[0][1] = x;
}
else if (dd2[1][0] < D2[x]) {
dd2[1][0] = D2[x], dd2[1][1] = x;
}
if (dd1[0][0] < D1[x] + 1) {
dd1[2][0] = dd1[1][0], dd1[2][1] = dd1[1][1];
dd1[1][0] = dd1[0][0], dd1[1][1] = dd1[0][1];
dd1[0][0] = D1[x] + 1, dd1[0][1] = x;
}
else if (dd1[1][0] < D1[x] + 1) {
dd1[2][0] = dd1[1][0], dd1[2][1] = dd1[1][1];
dd1[1][0] = D1[x] + 1, dd1[1][1] = x;
}
else if (dd1[2][0] < D1[x] + 1) {
dd1[2][0] = D1[x] + 1, dd1[2][1] = x;
}
}
for (auto &x : Ch[a]) {
int nd2 = d2, nd1 = d1 + 1, np1 = p1;
if (x == dd2[0][1]) nd2 = max(nd2, dd2[1][0]);
else nd2 = max(nd2, dd2[0][0]);
int ck = 0;
if (x == dd1[0][1])ck = 1;
if (nd1 < dd1[ck][0] + 1) {
nd1 = dd1[ck][0] + 1;
np1 = P1[dd1[ck][1]];
}
nd2 = max(nd2, dd1[ck][0] + d1);
if (x == dd1[0][1])nd2 = max(nd2, dd1[1][0] + dd1[2][0]);
else if (x == dd1[1][1])nd2 = max(nd2, dd1[0][0] + dd1[2][0]);
else nd2 = max(nd2, dd1[0][0] + dd1[1][0]);
DFS2(x, a, nd1, nd2, np1);
}
}
void Go(vector<Range> &T) {
sort(T.begin(), T.end());
int b = -1, e = -1;
for (auto &t : T) {
if (e < t.b) {
if (e != -1) {
S[b]++;
S[e + 1]--;
}
b = t.b;
}
e = max(e, t.e);
}
if (e != -1) {
S[b]++, S[e + 1]--;
}
}
int main() {
scanf("%d%d", &n, &m);
int i, a, b, j;
for (i = 1; i < n; i++) {
scanf("%d%d", &a, &b);
E[a].push_back(b);
E[b].push_back(a);
}
for (i = 1; i <= n; i++) {
scanf("%d", &Col[i]);
}
DFS1(1, 0);
DFS2(1, 0, 0, 0, 1);
for (i = 1; i <= m; i++) {
Go(G[i]);
}
for (i = 1; i <= n; i++)S[i] += S[i - 1];
for (i = 1; i <= n; i++) {
printf("%d\n", S[Num[i]]);
}
}
Compilation message (stderr)
joi2019_ho_t5.cpp: In function 'void DFS2(int, int, int, int, int)':
joi2019_ho_t5.cpp:75:76: warning: unused variable 'i' [-Wunused-variable]
int dd2[2][2] = { {-3,0},{-3,0} }, dd1[3][2] = { {0,a},{-3,0},{ -3,0 } }, i;
^
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:136:15: warning: unused variable 'j' [-Wunused-variable]
int i, a, b, j;
^
joi2019_ho_t5.cpp:135:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:138:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:143:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &Col[i]);
~~~~~^~~~~~~~~~~~~~~
# | 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... |