#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define pb push_back
#define nl '\n'
#define fore(i, y) for(int i = 0; i < y; i++)
#define forr(i, x, y) for(int i = x;i<=y;i++)
#define forn(i, y, x) for(int i = y; i >= x; i--)
const int N = 3e5 + 10;
vector<pair<int ,int>> adj[N];
int subtree[N] ;
int belongs[N];
bool forbidden[N];
int join[N] , up[N];
bool decre[N] , incre[N];
void dfs_subtree(int x ,int p)
{
subtree[x] = 1;
for(auto [u,i] :adj[x])
{
if(u == p || forbidden[u])
continue;
dfs_subtree(u , x);
subtree[x]+=subtree[u];
}
}
int dfs_down(int x , int p ,int n)
{
for(auto [u,i] : adj[x])
{
if(u == p || forbidden[u])
continue;
if(subtree[u] > n/2)
return dfs_down(u , x , n);
}
return x;
}
vector<int> nodes;
void dfs_belong(int x , int p , int child , int edge_p , int edge_join)
{
nodes.pb(x);
belongs[x] = child;
up[x] = edge_p;
join[x] = edge_join;
for(auto [u , i] : adj[x])
{
if(u == p || forbidden[u])
continue;
dfs_belong(u , x , child , i , edge_join);
}
}
void dfs_incre(int x , int p , int edge_p)
{
incre[x] = 1;
for(auto [u,i] : adj[x])
{
if(u == p || forbidden[u])
continue;
if(i <= edge_p)
continue;
dfs_incre(u , x , i);
}
}
void dfs_decre(int x , int p , int edge_p)
{
decre[x] = 1;
for(auto [u,i] : adj[x])
{
if(u == p || forbidden[u])
continue;
if(i >= edge_p)
continue;
dfs_decre(u , x , i);
}
}
int ans[N];
vector<vector<int>> queriesQ_in_subtree[N] , queriesC_in_subtree[N];
void rec(int root , vector<vector<int>> queriesQ , vector<vector<int>> queriesC)
{
vector<int>().swap(nodes);
vector<vector<int>>().swap(queriesQ_in_subtree[root]);
vector<vector<int>>().swap(queriesC_in_subtree[root]);
dfs_subtree(root , -1);
int centroid = dfs_down(root ,-1 , subtree[root]);
incre[centroid] = 1;
decre[centroid] = 1;
belongs[centroid] = centroid;
for(auto [child,i] : adj[centroid])
{
if(!forbidden[child])
{
dfs_belong(child , centroid , child , i , i);
dfs_incre(child , centroid , i);
dfs_decre(child , centroid , i);
}
}
for(auto query : queriesQ)
{
int a = query[0] , b = query[1] , id = query[2];
if(belongs[a] != belongs[b])
{
if(a == centroid)
{
ans[id] = (decre[b] && join[b] < id);
}
else if(b == centroid)
{
ans[id] = (incre[a] && up[a] < id);
}
else
ans[id] = (incre[a] && decre[b] && join[a] > join[b] && up[a] < id);
}
else
{
queriesQ_in_subtree[belongs[a]].pb(query);
}
}
vector<tuple<int ,int , int>> sweep;
for(auto node : nodes)
{
if(!incre[node])
continue;
sweep.pb({up[node] ,0, node});
}
for(auto query : queriesC)
{
queriesC_in_subtree[belongs[query[0]]].pb(query);
if(!decre[query[0]])
continue;
sweep.pb({query[1] , 1,query[0]});
}
sort(sweep.begin() , sweep.end());
Tree<int> t;
for(auto [Time , type , node] : sweep)
{
if(type == 0)
{
t.insert(join[node]);
}
else
{
if(node == centroid)
{
ans[Time]+=(int)t.size() +1;
}
else
{
ans[Time]+= (int)t.size() - (int)t.order_of_key(join[node] + 1);
if(join[node] < Time)
ans[Time]++;
}
}
}
for(auto node : nodes)
decre[node] = incre[node] = 0;
forbidden[centroid] = 1;
for(auto [u,i] : adj[centroid])
{
if(!forbidden[u])
rec(u , queriesQ_in_subtree[u] , queriesC_in_subtree[u]);
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n , q;
cin>>n>>q;
vector<vector<int>> queriesQ , queriesC;
vector<char> type;
fore(i , n + q - 1)
{
char c;
cin>>c;
type.pb(c);
if(c == 'S')
{
int a , b;
cin>>a>>b;
adj[a].pb({b , i});
adj[b].pb({a , i});
}
if(c == 'Q')
{
int a , d;
cin>>a>>d;
if(a == d)
ans[i] = 1;
else
queriesQ.pb({a , d , i});
}
if(c == 'C')
{
int a;
cin>>a;
queriesC.pb({a , i});
}
}
rec(1 , queriesQ , queriesC);
fore(i , n + q - 1)
{
if(type[i] == 'Q' )
{
cout<<(ans[i] ? "yes" : "no")<<nl;
}
if(type[i] == 'C')
cout<<ans[i]<<nl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
38152 KB |
Output is correct |
2 |
Correct |
50 ms |
40980 KB |
Output is correct |
3 |
Correct |
73 ms |
46656 KB |
Output is correct |
4 |
Correct |
61 ms |
42712 KB |
Output is correct |
5 |
Correct |
60 ms |
42752 KB |
Output is correct |
6 |
Correct |
43 ms |
37880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
38152 KB |
Output is correct |
2 |
Correct |
50 ms |
40980 KB |
Output is correct |
3 |
Correct |
73 ms |
46656 KB |
Output is correct |
4 |
Correct |
61 ms |
42712 KB |
Output is correct |
5 |
Correct |
60 ms |
42752 KB |
Output is correct |
6 |
Correct |
43 ms |
37880 KB |
Output is correct |
7 |
Correct |
45 ms |
40192 KB |
Output is correct |
8 |
Correct |
101 ms |
44980 KB |
Output is correct |
9 |
Correct |
88 ms |
49084 KB |
Output is correct |
10 |
Correct |
122 ms |
46348 KB |
Output is correct |
11 |
Correct |
137 ms |
46768 KB |
Output is correct |
12 |
Correct |
58 ms |
43448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
36616 KB |
Output is correct |
2 |
Correct |
152 ms |
60680 KB |
Output is correct |
3 |
Correct |
145 ms |
60564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
36616 KB |
Output is correct |
2 |
Correct |
152 ms |
60680 KB |
Output is correct |
3 |
Correct |
145 ms |
60564 KB |
Output is correct |
4 |
Correct |
38 ms |
37372 KB |
Output is correct |
5 |
Correct |
164 ms |
62816 KB |
Output is correct |
6 |
Correct |
159 ms |
68892 KB |
Output is correct |
7 |
Correct |
181 ms |
74856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
40964 KB |
Output is correct |
2 |
Correct |
368 ms |
63420 KB |
Output is correct |
3 |
Correct |
339 ms |
64012 KB |
Output is correct |
4 |
Correct |
260 ms |
79204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
40964 KB |
Output is correct |
2 |
Correct |
368 ms |
63420 KB |
Output is correct |
3 |
Correct |
339 ms |
64012 KB |
Output is correct |
4 |
Correct |
260 ms |
79204 KB |
Output is correct |
5 |
Correct |
49 ms |
41740 KB |
Output is correct |
6 |
Correct |
392 ms |
67596 KB |
Output is correct |
7 |
Correct |
475 ms |
81932 KB |
Output is correct |
8 |
Correct |
411 ms |
71428 KB |
Output is correct |
9 |
Correct |
424 ms |
71420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
39936 KB |
Output is correct |
2 |
Correct |
273 ms |
64328 KB |
Output is correct |
3 |
Correct |
286 ms |
54264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
39936 KB |
Output is correct |
2 |
Correct |
273 ms |
64328 KB |
Output is correct |
3 |
Correct |
286 ms |
54264 KB |
Output is correct |
4 |
Correct |
61 ms |
40444 KB |
Output is correct |
5 |
Correct |
386 ms |
71192 KB |
Output is correct |
6 |
Correct |
391 ms |
58248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
40904 KB |
Output is correct |
2 |
Correct |
323 ms |
63452 KB |
Output is correct |
3 |
Correct |
364 ms |
63996 KB |
Output is correct |
4 |
Correct |
286 ms |
79208 KB |
Output is correct |
5 |
Correct |
51 ms |
39980 KB |
Output is correct |
6 |
Correct |
281 ms |
64328 KB |
Output is correct |
7 |
Correct |
286 ms |
54272 KB |
Output is correct |
8 |
Correct |
220 ms |
56448 KB |
Output is correct |
9 |
Correct |
266 ms |
57084 KB |
Output is correct |
10 |
Correct |
325 ms |
60156 KB |
Output is correct |
11 |
Correct |
298 ms |
60568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
40904 KB |
Output is correct |
2 |
Correct |
323 ms |
63452 KB |
Output is correct |
3 |
Correct |
364 ms |
63996 KB |
Output is correct |
4 |
Correct |
286 ms |
79208 KB |
Output is correct |
5 |
Correct |
51 ms |
39980 KB |
Output is correct |
6 |
Correct |
281 ms |
64328 KB |
Output is correct |
7 |
Correct |
286 ms |
54272 KB |
Output is correct |
8 |
Correct |
220 ms |
56448 KB |
Output is correct |
9 |
Correct |
266 ms |
57084 KB |
Output is correct |
10 |
Correct |
325 ms |
60156 KB |
Output is correct |
11 |
Correct |
298 ms |
60568 KB |
Output is correct |
12 |
Correct |
50 ms |
41724 KB |
Output is correct |
13 |
Correct |
373 ms |
67400 KB |
Output is correct |
14 |
Correct |
408 ms |
81848 KB |
Output is correct |
15 |
Correct |
430 ms |
71296 KB |
Output is correct |
16 |
Correct |
478 ms |
71436 KB |
Output is correct |
17 |
Correct |
48 ms |
40452 KB |
Output is correct |
18 |
Correct |
370 ms |
71292 KB |
Output is correct |
19 |
Correct |
369 ms |
58236 KB |
Output is correct |
20 |
Correct |
338 ms |
60436 KB |
Output is correct |
21 |
Correct |
390 ms |
61448 KB |
Output is correct |
22 |
Correct |
590 ms |
73612 KB |
Output is correct |
23 |
Correct |
589 ms |
68184 KB |
Output is correct |
24 |
Correct |
463 ms |
62600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
38148 KB |
Output is correct |
2 |
Correct |
53 ms |
40964 KB |
Output is correct |
3 |
Correct |
71 ms |
46408 KB |
Output is correct |
4 |
Correct |
59 ms |
42744 KB |
Output is correct |
5 |
Correct |
62 ms |
42756 KB |
Output is correct |
6 |
Correct |
44 ms |
37888 KB |
Output is correct |
7 |
Correct |
47 ms |
36608 KB |
Output is correct |
8 |
Correct |
153 ms |
60512 KB |
Output is correct |
9 |
Correct |
146 ms |
60532 KB |
Output is correct |
10 |
Correct |
49 ms |
40964 KB |
Output is correct |
11 |
Correct |
342 ms |
63640 KB |
Output is correct |
12 |
Correct |
340 ms |
64160 KB |
Output is correct |
13 |
Correct |
261 ms |
79204 KB |
Output is correct |
14 |
Correct |
59 ms |
39936 KB |
Output is correct |
15 |
Correct |
266 ms |
64224 KB |
Output is correct |
16 |
Correct |
291 ms |
54168 KB |
Output is correct |
17 |
Correct |
195 ms |
56444 KB |
Output is correct |
18 |
Correct |
300 ms |
57092 KB |
Output is correct |
19 |
Correct |
326 ms |
60088 KB |
Output is correct |
20 |
Correct |
291 ms |
60536 KB |
Output is correct |
21 |
Correct |
152 ms |
66520 KB |
Output is correct |
22 |
Correct |
157 ms |
63480 KB |
Output is correct |
23 |
Correct |
261 ms |
61180 KB |
Output is correct |
24 |
Correct |
233 ms |
61696 KB |
Output is correct |
25 |
Correct |
335 ms |
65744 KB |
Output is correct |
26 |
Correct |
255 ms |
50556 KB |
Output is correct |
27 |
Correct |
258 ms |
50552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
38148 KB |
Output is correct |
2 |
Correct |
53 ms |
40964 KB |
Output is correct |
3 |
Correct |
71 ms |
46408 KB |
Output is correct |
4 |
Correct |
59 ms |
42744 KB |
Output is correct |
5 |
Correct |
62 ms |
42756 KB |
Output is correct |
6 |
Correct |
44 ms |
37888 KB |
Output is correct |
7 |
Correct |
47 ms |
36608 KB |
Output is correct |
8 |
Correct |
153 ms |
60512 KB |
Output is correct |
9 |
Correct |
146 ms |
60532 KB |
Output is correct |
10 |
Correct |
49 ms |
40964 KB |
Output is correct |
11 |
Correct |
342 ms |
63640 KB |
Output is correct |
12 |
Correct |
340 ms |
64160 KB |
Output is correct |
13 |
Correct |
261 ms |
79204 KB |
Output is correct |
14 |
Correct |
59 ms |
39936 KB |
Output is correct |
15 |
Correct |
266 ms |
64224 KB |
Output is correct |
16 |
Correct |
291 ms |
54168 KB |
Output is correct |
17 |
Correct |
195 ms |
56444 KB |
Output is correct |
18 |
Correct |
300 ms |
57092 KB |
Output is correct |
19 |
Correct |
326 ms |
60088 KB |
Output is correct |
20 |
Correct |
291 ms |
60536 KB |
Output is correct |
21 |
Correct |
152 ms |
66520 KB |
Output is correct |
22 |
Correct |
157 ms |
63480 KB |
Output is correct |
23 |
Correct |
261 ms |
61180 KB |
Output is correct |
24 |
Correct |
233 ms |
61696 KB |
Output is correct |
25 |
Correct |
335 ms |
65744 KB |
Output is correct |
26 |
Correct |
255 ms |
50556 KB |
Output is correct |
27 |
Correct |
258 ms |
50552 KB |
Output is correct |
28 |
Correct |
46 ms |
40208 KB |
Output is correct |
29 |
Correct |
96 ms |
44984 KB |
Output is correct |
30 |
Correct |
89 ms |
49296 KB |
Output is correct |
31 |
Correct |
116 ms |
46404 KB |
Output is correct |
32 |
Correct |
131 ms |
46572 KB |
Output is correct |
33 |
Correct |
58 ms |
43444 KB |
Output is correct |
34 |
Correct |
43 ms |
37380 KB |
Output is correct |
35 |
Correct |
167 ms |
62952 KB |
Output is correct |
36 |
Correct |
181 ms |
68964 KB |
Output is correct |
37 |
Correct |
189 ms |
75016 KB |
Output is correct |
38 |
Correct |
49 ms |
41728 KB |
Output is correct |
39 |
Correct |
408 ms |
67392 KB |
Output is correct |
40 |
Correct |
415 ms |
81888 KB |
Output is correct |
41 |
Correct |
434 ms |
71212 KB |
Output is correct |
42 |
Correct |
467 ms |
71420 KB |
Output is correct |
43 |
Correct |
47 ms |
40448 KB |
Output is correct |
44 |
Correct |
409 ms |
71292 KB |
Output is correct |
45 |
Correct |
341 ms |
58340 KB |
Output is correct |
46 |
Correct |
308 ms |
60548 KB |
Output is correct |
47 |
Correct |
383 ms |
61652 KB |
Output is correct |
48 |
Correct |
552 ms |
73556 KB |
Output is correct |
49 |
Correct |
588 ms |
68380 KB |
Output is correct |
50 |
Correct |
483 ms |
62592 KB |
Output is correct |
51 |
Correct |
190 ms |
78776 KB |
Output is correct |
52 |
Correct |
162 ms |
75108 KB |
Output is correct |
53 |
Correct |
195 ms |
75612 KB |
Output is correct |
54 |
Correct |
171 ms |
70080 KB |
Output is correct |
55 |
Correct |
193 ms |
79300 KB |
Output is correct |
56 |
Correct |
236 ms |
69716 KB |
Output is correct |
57 |
Correct |
366 ms |
120724 KB |
Output is correct |
58 |
Correct |
419 ms |
62588 KB |
Output is correct |
59 |
Correct |
255 ms |
51224 KB |
Output is correct |