#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a,b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a,b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
refs:
edi
https://codeforces.com/blog/entry/65042?#comment-491880
key idea:
for a node u, let d = arbitrary node with farthest dis from u
all good nodes lie on the path from u to d
proof:
consider the tree rooted at the path (u,d)
anybody in a branching subtree cant be good
why? because there exists another node on the path (u,d) with the same distance
if there exists a good guy in the subtree, then it contradicts the definition of d (d should have been somewhere in the subtree)
(s,t) = endpoints of diameter
farthest node from u = s or t
run dfs from s and t, calculate answers
when we go into a subtree, find the deepest in the other subtree and remove the respective nodes from consideration
(look at the pictures in the edi for better understanding)
in order to find #of good nodes, maintain a lazy segtree with range add updates and min, no.of min queries
when going into a subtree, add 1 to all bad nodes that are caused by the deepest in the other subtree
we need #of 0s
we know that min_val >= 0, so just get min and #of min
how to count #of distinct guys?
similar idea to ceoi harbingers
maintain a stack of all the unique nodes when doing the dfs
when we want to add a new node, add him only if the color is not present in the stack
keep an additional variable called "siz" which maintains the size of the active part of the stack
when we want to remove multiple nodes, just change the value of siz
rollback the changes after dfs
*/
const int MOD = 1e9 + 7;
const int N = 2e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
vector<ll> adj[N];
vector<ll> a(N);
pll diam = {-1,-1};
void dfs1(ll u, ll p, ll d){
pll px = {d,u};
amax(diam,px);
trav(v,adj[u]){
if(v == p) conts;
dfs1(v,u,d+1);
}
}
ll dis[N][2];
ll deepest[N][2];
void dfs2(ll u, ll p, ll t){
trav(v,adj[u]){
if(v == p) conts;
dis[v][t] = dis[u][t]+1;
dfs2(v,u,t);
amax(deepest[u][t],deepest[v][t]+1);
}
}
vector<ll> ans(N);
vector<ll> mx_without(N);
vector<ll> first_occ(N,inf2);
vector<pll> active(N);
ll siz = 0;
void dfs3(ll u, ll p, ll d, ll t){
ll mx = deepest[u][t];
ll cnt = upper_bound(active.begin(),active.begin()+siz,make_pair(d-mx,-1ll))-active.begin();
if(dis[u][t] >= dis[u][t^1]){
ans[u] = cnt;
}
mx = 0;
trav(v,adj[u]){
if(v == p) conts;
mx_without[v] = mx;
amax(mx,deepest[v][t]+1);
}
mx = 0;
reverse(all(adj[u]));
trav(v,adj[u]){
if(v == p) conts;
amax(mx_without[v],mx);
amax(mx,deepest[v][t]+1);
}
reverse(all(adj[u]));
trav(v,adj[u]){
if(v == p) conts;
vector<array<ll,4>> history;
mx = mx_without[v];
ll new_siz = upper_bound(active.begin(),active.begin()+siz,make_pair(d-mx,-1ll))-active.begin();
ll old_siz = siz;
history.pb({3,siz,-1,-1});
siz = new_siz;
ll x = a[u];
if(first_occ[x] >= siz){
history.pb({1,siz,active[siz].ff,active[siz].ss});
history.pb({2,x,first_occ[x],-1});
history.pb({3,siz,-1,-1});
ll w = active[siz].ss;
if(first_occ[a[w]] == siz){
history.pb({2,a[w],first_occ[a[w]],-1});
first_occ[a[w]] = inf2;
}
active[siz] = {d,u};
first_occ[x] = siz;
siz++;
}
dfs3(v,u,d+1,t);
reverse(all(history));
trav(ar,history){
auto [t,ind,val1,val2] = ar;
if(t == 1){
active[ind] = {val1,val2};
}
else if(t == 2){
first_occ[ind] = val1;
}
else{
siz = ind;
}
}
}
}
void solve(int test_case)
{
ll n,m; cin >> n >> m;
rep1(i,n-1){
ll u,v; cin >> u >> v;
adj[u].pb(v), adj[v].pb(u);
}
rep1(i,n) cin >> a[i];
dfs1(1,-1,0);
ll s = diam.ss;
diam = {-1,-1};
dfs1(s,-1,0);
ll t = diam.ss;
dfs2(s,-1,0);
dfs2(t,-1,1);
dfs3(s,-1,0,0);
dfs3(t,-1,0,1);
rep1(i,n){
cout << ans[i] << endl;
}
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
Compilation message
joi2019_ho_t5.cpp: In function 'void dfs3(ll, ll, ll, ll)':
joi2019_ho_t5.cpp:158:12: warning: unused variable 'old_siz' [-Wunused-variable]
158 | ll old_siz = siz;
| ^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
16476 KB |
Output is correct |
2 |
Correct |
7 ms |
16568 KB |
Output is correct |
3 |
Correct |
8 ms |
16732 KB |
Output is correct |
4 |
Correct |
8 ms |
16776 KB |
Output is correct |
5 |
Correct |
9 ms |
16732 KB |
Output is correct |
6 |
Correct |
7 ms |
17244 KB |
Output is correct |
7 |
Correct |
7 ms |
16984 KB |
Output is correct |
8 |
Correct |
7 ms |
16728 KB |
Output is correct |
9 |
Correct |
8 ms |
16732 KB |
Output is correct |
10 |
Correct |
8 ms |
16732 KB |
Output is correct |
11 |
Correct |
8 ms |
16732 KB |
Output is correct |
12 |
Correct |
7 ms |
16732 KB |
Output is correct |
13 |
Correct |
7 ms |
16988 KB |
Output is correct |
14 |
Correct |
8 ms |
16732 KB |
Output is correct |
15 |
Correct |
8 ms |
16732 KB |
Output is correct |
16 |
Correct |
7 ms |
16476 KB |
Output is correct |
17 |
Correct |
7 ms |
16988 KB |
Output is correct |
18 |
Correct |
8 ms |
16732 KB |
Output is correct |
19 |
Correct |
7 ms |
16572 KB |
Output is correct |
20 |
Correct |
8 ms |
17352 KB |
Output is correct |
21 |
Correct |
7 ms |
17084 KB |
Output is correct |
22 |
Correct |
8 ms |
16732 KB |
Output is correct |
23 |
Correct |
8 ms |
16732 KB |
Output is correct |
24 |
Correct |
7 ms |
16732 KB |
Output is correct |
25 |
Correct |
8 ms |
16732 KB |
Output is correct |
26 |
Correct |
8 ms |
16732 KB |
Output is correct |
27 |
Correct |
9 ms |
17160 KB |
Output is correct |
28 |
Correct |
7 ms |
16988 KB |
Output is correct |
29 |
Correct |
8 ms |
16988 KB |
Output is correct |
30 |
Correct |
7 ms |
16476 KB |
Output is correct |
31 |
Correct |
7 ms |
16988 KB |
Output is correct |
32 |
Correct |
8 ms |
17244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
25680 KB |
Output is correct |
2 |
Correct |
182 ms |
55272 KB |
Output is correct |
3 |
Correct |
26 ms |
22356 KB |
Output is correct |
4 |
Correct |
283 ms |
31100 KB |
Output is correct |
5 |
Correct |
414 ms |
83284 KB |
Output is correct |
6 |
Correct |
406 ms |
56752 KB |
Output is correct |
7 |
Correct |
265 ms |
31024 KB |
Output is correct |
8 |
Correct |
379 ms |
36420 KB |
Output is correct |
9 |
Correct |
308 ms |
34900 KB |
Output is correct |
10 |
Correct |
300 ms |
34628 KB |
Output is correct |
11 |
Correct |
193 ms |
31680 KB |
Output is correct |
12 |
Correct |
394 ms |
64340 KB |
Output is correct |
13 |
Correct |
354 ms |
56524 KB |
Output is correct |
14 |
Correct |
349 ms |
55028 KB |
Output is correct |
15 |
Correct |
149 ms |
29344 KB |
Output is correct |
16 |
Correct |
376 ms |
66280 KB |
Output is correct |
17 |
Correct |
424 ms |
56900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
242 ms |
29628 KB |
Output is correct |
2 |
Correct |
537 ms |
100176 KB |
Output is correct |
3 |
Correct |
40 ms |
26004 KB |
Output is correct |
4 |
Correct |
389 ms |
31972 KB |
Output is correct |
5 |
Correct |
599 ms |
103784 KB |
Output is correct |
6 |
Correct |
554 ms |
73688 KB |
Output is correct |
7 |
Correct |
325 ms |
31692 KB |
Output is correct |
8 |
Correct |
454 ms |
50844 KB |
Output is correct |
9 |
Correct |
411 ms |
44180 KB |
Output is correct |
10 |
Correct |
422 ms |
39012 KB |
Output is correct |
11 |
Correct |
337 ms |
31688 KB |
Output is correct |
12 |
Correct |
603 ms |
94036 KB |
Output is correct |
13 |
Correct |
491 ms |
66628 KB |
Output is correct |
14 |
Correct |
549 ms |
69168 KB |
Output is correct |
15 |
Correct |
160 ms |
30272 KB |
Output is correct |
16 |
Correct |
527 ms |
79088 KB |
Output is correct |
17 |
Correct |
560 ms |
73672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
16476 KB |
Output is correct |
2 |
Correct |
7 ms |
16568 KB |
Output is correct |
3 |
Correct |
8 ms |
16732 KB |
Output is correct |
4 |
Correct |
8 ms |
16776 KB |
Output is correct |
5 |
Correct |
9 ms |
16732 KB |
Output is correct |
6 |
Correct |
7 ms |
17244 KB |
Output is correct |
7 |
Correct |
7 ms |
16984 KB |
Output is correct |
8 |
Correct |
7 ms |
16728 KB |
Output is correct |
9 |
Correct |
8 ms |
16732 KB |
Output is correct |
10 |
Correct |
8 ms |
16732 KB |
Output is correct |
11 |
Correct |
8 ms |
16732 KB |
Output is correct |
12 |
Correct |
7 ms |
16732 KB |
Output is correct |
13 |
Correct |
7 ms |
16988 KB |
Output is correct |
14 |
Correct |
8 ms |
16732 KB |
Output is correct |
15 |
Correct |
8 ms |
16732 KB |
Output is correct |
16 |
Correct |
7 ms |
16476 KB |
Output is correct |
17 |
Correct |
7 ms |
16988 KB |
Output is correct |
18 |
Correct |
8 ms |
16732 KB |
Output is correct |
19 |
Correct |
7 ms |
16572 KB |
Output is correct |
20 |
Correct |
8 ms |
17352 KB |
Output is correct |
21 |
Correct |
7 ms |
17084 KB |
Output is correct |
22 |
Correct |
8 ms |
16732 KB |
Output is correct |
23 |
Correct |
8 ms |
16732 KB |
Output is correct |
24 |
Correct |
7 ms |
16732 KB |
Output is correct |
25 |
Correct |
8 ms |
16732 KB |
Output is correct |
26 |
Correct |
8 ms |
16732 KB |
Output is correct |
27 |
Correct |
9 ms |
17160 KB |
Output is correct |
28 |
Correct |
7 ms |
16988 KB |
Output is correct |
29 |
Correct |
8 ms |
16988 KB |
Output is correct |
30 |
Correct |
7 ms |
16476 KB |
Output is correct |
31 |
Correct |
7 ms |
16988 KB |
Output is correct |
32 |
Correct |
8 ms |
17244 KB |
Output is correct |
33 |
Correct |
104 ms |
25680 KB |
Output is correct |
34 |
Correct |
182 ms |
55272 KB |
Output is correct |
35 |
Correct |
26 ms |
22356 KB |
Output is correct |
36 |
Correct |
283 ms |
31100 KB |
Output is correct |
37 |
Correct |
414 ms |
83284 KB |
Output is correct |
38 |
Correct |
406 ms |
56752 KB |
Output is correct |
39 |
Correct |
265 ms |
31024 KB |
Output is correct |
40 |
Correct |
379 ms |
36420 KB |
Output is correct |
41 |
Correct |
308 ms |
34900 KB |
Output is correct |
42 |
Correct |
300 ms |
34628 KB |
Output is correct |
43 |
Correct |
193 ms |
31680 KB |
Output is correct |
44 |
Correct |
394 ms |
64340 KB |
Output is correct |
45 |
Correct |
354 ms |
56524 KB |
Output is correct |
46 |
Correct |
349 ms |
55028 KB |
Output is correct |
47 |
Correct |
149 ms |
29344 KB |
Output is correct |
48 |
Correct |
376 ms |
66280 KB |
Output is correct |
49 |
Correct |
424 ms |
56900 KB |
Output is correct |
50 |
Correct |
242 ms |
29628 KB |
Output is correct |
51 |
Correct |
537 ms |
100176 KB |
Output is correct |
52 |
Correct |
40 ms |
26004 KB |
Output is correct |
53 |
Correct |
389 ms |
31972 KB |
Output is correct |
54 |
Correct |
599 ms |
103784 KB |
Output is correct |
55 |
Correct |
554 ms |
73688 KB |
Output is correct |
56 |
Correct |
325 ms |
31692 KB |
Output is correct |
57 |
Correct |
454 ms |
50844 KB |
Output is correct |
58 |
Correct |
411 ms |
44180 KB |
Output is correct |
59 |
Correct |
422 ms |
39012 KB |
Output is correct |
60 |
Correct |
337 ms |
31688 KB |
Output is correct |
61 |
Correct |
603 ms |
94036 KB |
Output is correct |
62 |
Correct |
491 ms |
66628 KB |
Output is correct |
63 |
Correct |
549 ms |
69168 KB |
Output is correct |
64 |
Correct |
160 ms |
30272 KB |
Output is correct |
65 |
Correct |
527 ms |
79088 KB |
Output is correct |
66 |
Correct |
560 ms |
73672 KB |
Output is correct |
67 |
Correct |
28 ms |
18780 KB |
Output is correct |
68 |
Correct |
144 ms |
49448 KB |
Output is correct |
69 |
Correct |
238 ms |
47496 KB |
Output is correct |
70 |
Correct |
333 ms |
31324 KB |
Output is correct |
71 |
Correct |
455 ms |
83116 KB |
Output is correct |
72 |
Correct |
443 ms |
56856 KB |
Output is correct |
73 |
Correct |
358 ms |
30924 KB |
Output is correct |
74 |
Correct |
392 ms |
41412 KB |
Output is correct |
75 |
Correct |
353 ms |
36032 KB |
Output is correct |
76 |
Correct |
351 ms |
35156 KB |
Output is correct |
77 |
Correct |
300 ms |
31032 KB |
Output is correct |
78 |
Correct |
442 ms |
69712 KB |
Output is correct |
79 |
Correct |
405 ms |
62576 KB |
Output is correct |
80 |
Correct |
421 ms |
51916 KB |
Output is correct |
81 |
Correct |
174 ms |
29000 KB |
Output is correct |
82 |
Correct |
385 ms |
65888 KB |
Output is correct |
83 |
Correct |
432 ms |
57000 KB |
Output is correct |
84 |
Correct |
341 ms |
31316 KB |
Output is correct |
85 |
Correct |
427 ms |
83796 KB |
Output is correct |
86 |
Correct |
428 ms |
57424 KB |
Output is correct |
87 |
Correct |
308 ms |
31184 KB |
Output is correct |
88 |
Correct |
416 ms |
42580 KB |
Output is correct |
89 |
Correct |
363 ms |
39068 KB |
Output is correct |
90 |
Correct |
398 ms |
37452 KB |
Output is correct |
91 |
Correct |
298 ms |
31428 KB |
Output is correct |
92 |
Correct |
408 ms |
81964 KB |
Output is correct |
93 |
Correct |
358 ms |
49624 KB |
Output is correct |
94 |
Correct |
339 ms |
45004 KB |
Output is correct |
95 |
Correct |
136 ms |
28612 KB |
Output is correct |
96 |
Correct |
356 ms |
66500 KB |
Output is correct |
97 |
Correct |
352 ms |
57548 KB |
Output is correct |
98 |
Correct |
333 ms |
31780 KB |
Output is correct |
99 |
Correct |
418 ms |
84452 KB |
Output is correct |
100 |
Correct |
485 ms |
69976 KB |
Output is correct |
101 |
Correct |
321 ms |
31340 KB |
Output is correct |
102 |
Correct |
373 ms |
46220 KB |
Output is correct |
103 |
Correct |
348 ms |
40516 KB |
Output is correct |
104 |
Correct |
360 ms |
38996 KB |
Output is correct |
105 |
Correct |
248 ms |
31688 KB |
Output is correct |
106 |
Correct |
515 ms |
74576 KB |
Output is correct |
107 |
Correct |
411 ms |
64092 KB |
Output is correct |
108 |
Correct |
481 ms |
57808 KB |
Output is correct |
109 |
Correct |
159 ms |
28868 KB |
Output is correct |
110 |
Correct |
398 ms |
69656 KB |
Output is correct |
111 |
Correct |
508 ms |
68556 KB |
Output is correct |