#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
*/
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){
// debug(u);
// debug(siz);
// rep(i,siz){
// debug(active[i]);
// }
// debug(first_occ[2]);
// cout << endl;
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:134:12: warning: unused variable 'old_siz' [-Wunused-variable]
134 | ll old_siz = siz;
| ^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
16632 KB |
Output is correct |
2 |
Correct |
7 ms |
16888 KB |
Output is correct |
3 |
Correct |
7 ms |
16732 KB |
Output is correct |
4 |
Correct |
8 ms |
16988 KB |
Output is correct |
5 |
Correct |
7 ms |
16708 KB |
Output is correct |
6 |
Correct |
7 ms |
16988 KB |
Output is correct |
7 |
Correct |
8 ms |
16724 KB |
Output is correct |
8 |
Correct |
9 ms |
16732 KB |
Output is correct |
9 |
Correct |
9 ms |
16732 KB |
Output is correct |
10 |
Correct |
7 ms |
16732 KB |
Output is correct |
11 |
Correct |
8 ms |
16988 KB |
Output is correct |
12 |
Correct |
8 ms |
16692 KB |
Output is correct |
13 |
Correct |
7 ms |
16988 KB |
Output is correct |
14 |
Correct |
8 ms |
16728 KB |
Output is correct |
15 |
Correct |
7 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 |
7 ms |
16732 KB |
Output is correct |
19 |
Correct |
8 ms |
16728 KB |
Output is correct |
20 |
Correct |
7 ms |
17244 KB |
Output is correct |
21 |
Correct |
7 ms |
16984 KB |
Output is correct |
22 |
Correct |
7 ms |
16732 KB |
Output is correct |
23 |
Correct |
7 ms |
16728 KB |
Output is correct |
24 |
Correct |
9 ms |
16732 KB |
Output is correct |
25 |
Correct |
8 ms |
16732 KB |
Output is correct |
26 |
Correct |
9 ms |
16728 KB |
Output is correct |
27 |
Correct |
8 ms |
17244 KB |
Output is correct |
28 |
Correct |
8 ms |
16988 KB |
Output is correct |
29 |
Correct |
8 ms |
16824 KB |
Output is correct |
30 |
Correct |
7 ms |
16472 KB |
Output is correct |
31 |
Correct |
7 ms |
16988 KB |
Output is correct |
32 |
Correct |
10 ms |
17084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
106 ms |
23888 KB |
Output is correct |
2 |
Correct |
180 ms |
53816 KB |
Output is correct |
3 |
Correct |
26 ms |
22108 KB |
Output is correct |
4 |
Correct |
324 ms |
28496 KB |
Output is correct |
5 |
Correct |
440 ms |
80688 KB |
Output is correct |
6 |
Correct |
390 ms |
54356 KB |
Output is correct |
7 |
Correct |
311 ms |
28360 KB |
Output is correct |
8 |
Correct |
324 ms |
33620 KB |
Output is correct |
9 |
Correct |
317 ms |
32080 KB |
Output is correct |
10 |
Correct |
344 ms |
31956 KB |
Output is correct |
11 |
Correct |
189 ms |
28868 KB |
Output is correct |
12 |
Correct |
426 ms |
61376 KB |
Output is correct |
13 |
Correct |
360 ms |
53840 KB |
Output is correct |
14 |
Correct |
372 ms |
52168 KB |
Output is correct |
15 |
Correct |
152 ms |
26688 KB |
Output is correct |
16 |
Correct |
369 ms |
63432 KB |
Output is correct |
17 |
Correct |
390 ms |
54472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
243 ms |
26848 KB |
Output is correct |
2 |
Correct |
630 ms |
96660 KB |
Output is correct |
3 |
Correct |
43 ms |
25240 KB |
Output is correct |
4 |
Correct |
433 ms |
28652 KB |
Output is correct |
5 |
Correct |
595 ms |
100368 KB |
Output is correct |
6 |
Correct |
558 ms |
70112 KB |
Output is correct |
7 |
Correct |
346 ms |
28440 KB |
Output is correct |
8 |
Correct |
461 ms |
47492 KB |
Output is correct |
9 |
Correct |
463 ms |
40784 KB |
Output is correct |
10 |
Correct |
447 ms |
35824 KB |
Output is correct |
11 |
Correct |
347 ms |
28360 KB |
Output is correct |
12 |
Correct |
621 ms |
90624 KB |
Output is correct |
13 |
Correct |
518 ms |
63680 KB |
Output is correct |
14 |
Correct |
516 ms |
65720 KB |
Output is correct |
15 |
Correct |
141 ms |
26780 KB |
Output is correct |
16 |
Correct |
449 ms |
75608 KB |
Output is correct |
17 |
Correct |
547 ms |
70440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
16632 KB |
Output is correct |
2 |
Correct |
7 ms |
16888 KB |
Output is correct |
3 |
Correct |
7 ms |
16732 KB |
Output is correct |
4 |
Correct |
8 ms |
16988 KB |
Output is correct |
5 |
Correct |
7 ms |
16708 KB |
Output is correct |
6 |
Correct |
7 ms |
16988 KB |
Output is correct |
7 |
Correct |
8 ms |
16724 KB |
Output is correct |
8 |
Correct |
9 ms |
16732 KB |
Output is correct |
9 |
Correct |
9 ms |
16732 KB |
Output is correct |
10 |
Correct |
7 ms |
16732 KB |
Output is correct |
11 |
Correct |
8 ms |
16988 KB |
Output is correct |
12 |
Correct |
8 ms |
16692 KB |
Output is correct |
13 |
Correct |
7 ms |
16988 KB |
Output is correct |
14 |
Correct |
8 ms |
16728 KB |
Output is correct |
15 |
Correct |
7 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 |
7 ms |
16732 KB |
Output is correct |
19 |
Correct |
8 ms |
16728 KB |
Output is correct |
20 |
Correct |
7 ms |
17244 KB |
Output is correct |
21 |
Correct |
7 ms |
16984 KB |
Output is correct |
22 |
Correct |
7 ms |
16732 KB |
Output is correct |
23 |
Correct |
7 ms |
16728 KB |
Output is correct |
24 |
Correct |
9 ms |
16732 KB |
Output is correct |
25 |
Correct |
8 ms |
16732 KB |
Output is correct |
26 |
Correct |
9 ms |
16728 KB |
Output is correct |
27 |
Correct |
8 ms |
17244 KB |
Output is correct |
28 |
Correct |
8 ms |
16988 KB |
Output is correct |
29 |
Correct |
8 ms |
16824 KB |
Output is correct |
30 |
Correct |
7 ms |
16472 KB |
Output is correct |
31 |
Correct |
7 ms |
16988 KB |
Output is correct |
32 |
Correct |
10 ms |
17084 KB |
Output is correct |
33 |
Correct |
106 ms |
23888 KB |
Output is correct |
34 |
Correct |
180 ms |
53816 KB |
Output is correct |
35 |
Correct |
26 ms |
22108 KB |
Output is correct |
36 |
Correct |
324 ms |
28496 KB |
Output is correct |
37 |
Correct |
440 ms |
80688 KB |
Output is correct |
38 |
Correct |
390 ms |
54356 KB |
Output is correct |
39 |
Correct |
311 ms |
28360 KB |
Output is correct |
40 |
Correct |
324 ms |
33620 KB |
Output is correct |
41 |
Correct |
317 ms |
32080 KB |
Output is correct |
42 |
Correct |
344 ms |
31956 KB |
Output is correct |
43 |
Correct |
189 ms |
28868 KB |
Output is correct |
44 |
Correct |
426 ms |
61376 KB |
Output is correct |
45 |
Correct |
360 ms |
53840 KB |
Output is correct |
46 |
Correct |
372 ms |
52168 KB |
Output is correct |
47 |
Correct |
152 ms |
26688 KB |
Output is correct |
48 |
Correct |
369 ms |
63432 KB |
Output is correct |
49 |
Correct |
390 ms |
54472 KB |
Output is correct |
50 |
Correct |
243 ms |
26848 KB |
Output is correct |
51 |
Correct |
630 ms |
96660 KB |
Output is correct |
52 |
Correct |
43 ms |
25240 KB |
Output is correct |
53 |
Correct |
433 ms |
28652 KB |
Output is correct |
54 |
Correct |
595 ms |
100368 KB |
Output is correct |
55 |
Correct |
558 ms |
70112 KB |
Output is correct |
56 |
Correct |
346 ms |
28440 KB |
Output is correct |
57 |
Correct |
461 ms |
47492 KB |
Output is correct |
58 |
Correct |
463 ms |
40784 KB |
Output is correct |
59 |
Correct |
447 ms |
35824 KB |
Output is correct |
60 |
Correct |
347 ms |
28360 KB |
Output is correct |
61 |
Correct |
621 ms |
90624 KB |
Output is correct |
62 |
Correct |
518 ms |
63680 KB |
Output is correct |
63 |
Correct |
516 ms |
65720 KB |
Output is correct |
64 |
Correct |
141 ms |
26780 KB |
Output is correct |
65 |
Correct |
449 ms |
75608 KB |
Output is correct |
66 |
Correct |
547 ms |
70440 KB |
Output is correct |
67 |
Correct |
28 ms |
18768 KB |
Output is correct |
68 |
Correct |
152 ms |
49580 KB |
Output is correct |
69 |
Correct |
246 ms |
47592 KB |
Output is correct |
70 |
Correct |
362 ms |
31440 KB |
Output is correct |
71 |
Correct |
459 ms |
83416 KB |
Output is correct |
72 |
Correct |
419 ms |
57132 KB |
Output is correct |
73 |
Correct |
335 ms |
31180 KB |
Output is correct |
74 |
Correct |
346 ms |
41940 KB |
Output is correct |
75 |
Correct |
340 ms |
36160 KB |
Output is correct |
76 |
Correct |
354 ms |
35476 KB |
Output is correct |
77 |
Correct |
271 ms |
31308 KB |
Output is correct |
78 |
Correct |
454 ms |
70228 KB |
Output is correct |
79 |
Correct |
387 ms |
62756 KB |
Output is correct |
80 |
Correct |
431 ms |
52316 KB |
Output is correct |
81 |
Correct |
220 ms |
29284 KB |
Output is correct |
82 |
Correct |
376 ms |
66324 KB |
Output is correct |
83 |
Correct |
431 ms |
57288 KB |
Output is correct |
84 |
Correct |
360 ms |
31700 KB |
Output is correct |
85 |
Correct |
471 ms |
84256 KB |
Output is correct |
86 |
Correct |
523 ms |
57680 KB |
Output is correct |
87 |
Correct |
351 ms |
31688 KB |
Output is correct |
88 |
Correct |
348 ms |
43344 KB |
Output is correct |
89 |
Correct |
364 ms |
39452 KB |
Output is correct |
90 |
Correct |
407 ms |
37816 KB |
Output is correct |
91 |
Correct |
314 ms |
31804 KB |
Output is correct |
92 |
Correct |
482 ms |
82512 KB |
Output is correct |
93 |
Correct |
409 ms |
50048 KB |
Output is correct |
94 |
Correct |
368 ms |
45488 KB |
Output is correct |
95 |
Correct |
172 ms |
28916 KB |
Output is correct |
96 |
Correct |
356 ms |
66964 KB |
Output is correct |
97 |
Correct |
413 ms |
58132 KB |
Output is correct |
98 |
Correct |
421 ms |
32300 KB |
Output is correct |
99 |
Correct |
466 ms |
84816 KB |
Output is correct |
100 |
Correct |
566 ms |
70608 KB |
Output is correct |
101 |
Correct |
359 ms |
32008 KB |
Output is correct |
102 |
Correct |
480 ms |
46772 KB |
Output is correct |
103 |
Correct |
378 ms |
41040 KB |
Output is correct |
104 |
Correct |
376 ms |
39632 KB |
Output is correct |
105 |
Correct |
348 ms |
32204 KB |
Output is correct |
106 |
Correct |
514 ms |
75088 KB |
Output is correct |
107 |
Correct |
437 ms |
64708 KB |
Output is correct |
108 |
Correct |
511 ms |
58572 KB |
Output is correct |
109 |
Correct |
211 ms |
29572 KB |
Output is correct |
110 |
Correct |
404 ms |
70088 KB |
Output is correct |
111 |
Correct |
603 ms |
69184 KB |
Output is correct |