#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 12, MOD = (int)1e9 + 7;
int n, m, a[N];
bool vis[N], timer;
vector<int> g[N];
vector<vector<int>> dist;
bool zap;
int f(int d) {
vector<int> D(n + 1, 0);
timer = 1;
for(int i = 1; i <= n; i++) {
vis[i] = 0;
}
vis[d] = timer;
int lst = d;
queue<int> q;
q.push(d);
while(!q.empty()) {
int v = q.front();
lst = v;
q.pop();
for(int to : g[v]) {
if(vis[to] != timer) {
D[to] = D[v] + 1;
q.push(to);
vis[to] = timer;
}
}
}
dist.push_back(D);
return lst;
}
int dep[N], mxd[N], res[N], d, d1, o, cr, gl[N];
void dfs(int v, int pr = -1) {
mxd[v] = dep[v];
for(int to:g[v]) if(to != pr) {
dep[to] = dep[v] + 1;
dfs(to, v);
mxd[v] = max(mxd[v], mxd[to]);
}
for(int i = 0; i < (int)g[v].size(); i++) {
if(g[v][i] == pr) {
swap(g[v][(int)g[v].size() - 1], g[v][i]);
break;
}
}
for(int i = 0; i < (int)g[v].size() - 1; i++) {
if(mxd[g[v][i]] == mxd[v]) {
swap(g[v][0], g[v][i]);
}
}
}
int b = 17, it = 0, cur = 0, ver[N];
int up[N][18];
int rt;
vector<int> e[N];
set<pair<int, int>> cl[N];
void bld(int v) {
int sz = (int)g[v].size() - (rt != v), mx1 = dep[v];
if(!sz) return;
for(int i = 1; i < sz; i++) {
mx1 = max(mx1, mxd[g[v][i]]);
}
int bf = cur;
for(int i = 0; i < sz; i++) {
int to = g[v][i];
int mx = dep[v];
if(i) mx = mxd[g[v][0]];
if(i < sz - 1) mx = max(mx, mx1);
mx -= dep[v];
if(i == 1 && ver[cur] == v) {
cur = up[cur][0];
}
if(i <= 1 && dep[v] - dep[ver[cur]] <= mx) {
for(int i = b - 1; i >= 0; i--) {
int nv = up[cur][i];
if(dep[v] - dep[ver[nv]] <= mx) {
cur = nv;
}
}
cur = up[cur][0];
}
if(i <= 1) {
it++;
e[cur].push_back(it);
ver[it] = v;
gl[it] = gl[cur] + 1;
up[it][0] = cur;
for(int i = 1; i < b; i++) {
up[it][i] = up[up[it][i - 1]][i - 1];
}
cur = it;
}
bld(to);
}
cur = bf;
}
int tin[N], tout[N], tt;
void dfs1(int v) {
}
void go(int v) {
if(dist[cr][v] > dist[o][v] || (dist[cr][v] == dist[o][v] && zap)) {
int f = cur, val = -mxd[v] + dep[v] * 2;
set<int> r;
if(dep[ver[f]] >= val) {
for(int i = b - 1; i >= 0; i--) {
int nv = ver[up[f][i]];
if(dep[nv] >= val) {
f = up[f][i];
}
}
f = up[f][0];
}
res[v] += gl[f];
}
int sz = (int)g[v].size() - (rt != v), mx1 = dep[v];
if(!sz) return;
for(int i = 1; i < sz; i++) {
mx1 = max(mx1, mxd[g[v][i]]);
}
int bf = cur;
for(int i = 0; i < sz; i++) {
int to = g[v][i];
int mx = dep[v];
if(i) mx = mxd[g[v][0]];
if(i < sz - 1) mx = max(mx, mx1);
mx -= dep[v];
if(i == 1) {
cur = up[cur][0];
}
if(i <= 1 && dep[v] - dep[ver[cur]] <= mx) {
for(int i = b - 1; i >= 0; i--) {
int nv = up[cur][i];
if(dep[v] - dep[ver[nv]] <= mx) {
cur = nv;
}
}
cur = up[cur][0];
}
if(i <= 1) {
it++;
ver[it] = v;
auto check = [&](){
int f = cur;
while(f) {
if(a[ver[f]] == a[v]) return 0;
f = up[f][0];
}
return 1;
};
gl[it] = gl[cur] + (check());
up[it][0] = cur;
for(int i = 1; i < b; i++) {
up[it][i] = up[up[it][i - 1]][i - 1];
}
cur = it;
}
go(to);
}
cur = bf;
}
void solve(int root) {
it = cur = 0;
ver[0] = 0;
dep[0] = -(int)1e9;
rt = root;
dep[root] = 1;
dfs(root);
bld(root);
it = cur = 0;
go(root);
for(int i = 0; i <= it; i++) {
e[i].clear();
}
for(int i = 1; i <= m; i++) {
cl[i].clear();
}
}
void test() {
cin >> n >> m;
for(int i = 1; i <= n - 1; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
d = f(1), d1 = f(d);
f(d1);
o = 2;cr = 1;
solve(d);
zap = 1;
o = 1;cr = 2;
solve(d1);
for(int i = 1; i <= n; i++) {
cout << res[i] << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
26960 KB |
Output is correct |
2 |
Correct |
6 ms |
27084 KB |
Output is correct |
3 |
Correct |
7 ms |
27216 KB |
Output is correct |
4 |
Correct |
8 ms |
27364 KB |
Output is correct |
5 |
Correct |
7 ms |
27216 KB |
Output is correct |
6 |
Correct |
7 ms |
27472 KB |
Output is correct |
7 |
Correct |
6 ms |
27388 KB |
Output is correct |
8 |
Correct |
6 ms |
27216 KB |
Output is correct |
9 |
Correct |
6 ms |
27216 KB |
Output is correct |
10 |
Correct |
7 ms |
27216 KB |
Output is correct |
11 |
Correct |
7 ms |
27216 KB |
Output is correct |
12 |
Correct |
7 ms |
27272 KB |
Output is correct |
13 |
Correct |
7 ms |
27472 KB |
Output is correct |
14 |
Correct |
6 ms |
27216 KB |
Output is correct |
15 |
Correct |
6 ms |
27216 KB |
Output is correct |
16 |
Correct |
5 ms |
27216 KB |
Output is correct |
17 |
Correct |
6 ms |
27216 KB |
Output is correct |
18 |
Correct |
5 ms |
27216 KB |
Output is correct |
19 |
Correct |
7 ms |
27252 KB |
Output is correct |
20 |
Correct |
13 ms |
27472 KB |
Output is correct |
21 |
Correct |
8 ms |
27368 KB |
Output is correct |
22 |
Correct |
7 ms |
27216 KB |
Output is correct |
23 |
Correct |
6 ms |
27216 KB |
Output is correct |
24 |
Correct |
6 ms |
27076 KB |
Output is correct |
25 |
Correct |
7 ms |
27216 KB |
Output is correct |
26 |
Correct |
5 ms |
27216 KB |
Output is correct |
27 |
Correct |
11 ms |
27216 KB |
Output is correct |
28 |
Correct |
9 ms |
27216 KB |
Output is correct |
29 |
Correct |
8 ms |
27216 KB |
Output is correct |
30 |
Correct |
6 ms |
27216 KB |
Output is correct |
31 |
Correct |
8 ms |
27216 KB |
Output is correct |
32 |
Correct |
8 ms |
27388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
130 ms |
40580 KB |
Output is correct |
2 |
Correct |
172 ms |
56540 KB |
Output is correct |
3 |
Correct |
31 ms |
32592 KB |
Output is correct |
4 |
Correct |
271 ms |
52464 KB |
Output is correct |
5 |
Correct |
464 ms |
76716 KB |
Output is correct |
6 |
Correct |
297 ms |
64752 KB |
Output is correct |
7 |
Correct |
229 ms |
50100 KB |
Output is correct |
8 |
Correct |
266 ms |
54924 KB |
Output is correct |
9 |
Correct |
238 ms |
53816 KB |
Output is correct |
10 |
Correct |
262 ms |
53824 KB |
Output is correct |
11 |
Correct |
89 ms |
38328 KB |
Output is correct |
12 |
Correct |
379 ms |
66720 KB |
Output is correct |
13 |
Correct |
341 ms |
60852 KB |
Output is correct |
14 |
Correct |
305 ms |
61108 KB |
Output is correct |
15 |
Correct |
88 ms |
38572 KB |
Output is correct |
16 |
Correct |
255 ms |
65588 KB |
Output is correct |
17 |
Correct |
225 ms |
62388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
169 ms |
47580 KB |
Output is correct |
2 |
Execution timed out |
2080 ms |
71516 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
26960 KB |
Output is correct |
2 |
Correct |
6 ms |
27084 KB |
Output is correct |
3 |
Correct |
7 ms |
27216 KB |
Output is correct |
4 |
Correct |
8 ms |
27364 KB |
Output is correct |
5 |
Correct |
7 ms |
27216 KB |
Output is correct |
6 |
Correct |
7 ms |
27472 KB |
Output is correct |
7 |
Correct |
6 ms |
27388 KB |
Output is correct |
8 |
Correct |
6 ms |
27216 KB |
Output is correct |
9 |
Correct |
6 ms |
27216 KB |
Output is correct |
10 |
Correct |
7 ms |
27216 KB |
Output is correct |
11 |
Correct |
7 ms |
27216 KB |
Output is correct |
12 |
Correct |
7 ms |
27272 KB |
Output is correct |
13 |
Correct |
7 ms |
27472 KB |
Output is correct |
14 |
Correct |
6 ms |
27216 KB |
Output is correct |
15 |
Correct |
6 ms |
27216 KB |
Output is correct |
16 |
Correct |
5 ms |
27216 KB |
Output is correct |
17 |
Correct |
6 ms |
27216 KB |
Output is correct |
18 |
Correct |
5 ms |
27216 KB |
Output is correct |
19 |
Correct |
7 ms |
27252 KB |
Output is correct |
20 |
Correct |
13 ms |
27472 KB |
Output is correct |
21 |
Correct |
8 ms |
27368 KB |
Output is correct |
22 |
Correct |
7 ms |
27216 KB |
Output is correct |
23 |
Correct |
6 ms |
27216 KB |
Output is correct |
24 |
Correct |
6 ms |
27076 KB |
Output is correct |
25 |
Correct |
7 ms |
27216 KB |
Output is correct |
26 |
Correct |
5 ms |
27216 KB |
Output is correct |
27 |
Correct |
11 ms |
27216 KB |
Output is correct |
28 |
Correct |
9 ms |
27216 KB |
Output is correct |
29 |
Correct |
8 ms |
27216 KB |
Output is correct |
30 |
Correct |
6 ms |
27216 KB |
Output is correct |
31 |
Correct |
8 ms |
27216 KB |
Output is correct |
32 |
Correct |
8 ms |
27388 KB |
Output is correct |
33 |
Correct |
130 ms |
40580 KB |
Output is correct |
34 |
Correct |
172 ms |
56540 KB |
Output is correct |
35 |
Correct |
31 ms |
32592 KB |
Output is correct |
36 |
Correct |
271 ms |
52464 KB |
Output is correct |
37 |
Correct |
464 ms |
76716 KB |
Output is correct |
38 |
Correct |
297 ms |
64752 KB |
Output is correct |
39 |
Correct |
229 ms |
50100 KB |
Output is correct |
40 |
Correct |
266 ms |
54924 KB |
Output is correct |
41 |
Correct |
238 ms |
53816 KB |
Output is correct |
42 |
Correct |
262 ms |
53824 KB |
Output is correct |
43 |
Correct |
89 ms |
38328 KB |
Output is correct |
44 |
Correct |
379 ms |
66720 KB |
Output is correct |
45 |
Correct |
341 ms |
60852 KB |
Output is correct |
46 |
Correct |
305 ms |
61108 KB |
Output is correct |
47 |
Correct |
88 ms |
38572 KB |
Output is correct |
48 |
Correct |
255 ms |
65588 KB |
Output is correct |
49 |
Correct |
225 ms |
62388 KB |
Output is correct |
50 |
Correct |
169 ms |
47580 KB |
Output is correct |
51 |
Execution timed out |
2080 ms |
71516 KB |
Time limit exceeded |
52 |
Halted |
0 ms |
0 KB |
- |