#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
using namespace std;
#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>> queries_in_subtree[N];
void rec(int root , vector<vector<int>> queries)
{
vector<int>().swap(nodes);
vector<vector<int>>().swap(queries_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 : queries)
{
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
{
queries_in_subtree[belongs[a]].pb(query);
}
}
for(auto node : nodes)
decre[node] = incre[node] = 0;
forbidden[centroid] = 1;
for(auto [u,i] : adj[centroid])
{
if(!forbidden[u])
rec(u , queries_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>> queries;
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
queries.pb({a , d , i});
}
}
rec(1 , queries);
fore(i , n + q - 1)
{
if(type[i] == 'Q')
{
cout<<(ans[i] ? "yes" : "no")<<nl;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
31036 KB |
Output is correct |
2 |
Correct |
60 ms |
34048 KB |
Output is correct |
3 |
Correct |
94 ms |
39284 KB |
Output is correct |
4 |
Correct |
60 ms |
35680 KB |
Output is correct |
5 |
Correct |
57 ms |
35612 KB |
Output is correct |
6 |
Correct |
39 ms |
30464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
31036 KB |
Output is correct |
2 |
Correct |
60 ms |
34048 KB |
Output is correct |
3 |
Correct |
94 ms |
39284 KB |
Output is correct |
4 |
Correct |
60 ms |
35680 KB |
Output is correct |
5 |
Correct |
57 ms |
35612 KB |
Output is correct |
6 |
Correct |
39 ms |
30464 KB |
Output is correct |
7 |
Incorrect |
37 ms |
30936 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
29708 KB |
Output is correct |
2 |
Correct |
76 ms |
44396 KB |
Output is correct |
3 |
Correct |
77 ms |
44396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
29708 KB |
Output is correct |
2 |
Correct |
76 ms |
44396 KB |
Output is correct |
3 |
Correct |
77 ms |
44396 KB |
Output is correct |
4 |
Incorrect |
31 ms |
28416 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
34072 KB |
Output is correct |
2 |
Correct |
330 ms |
56312 KB |
Output is correct |
3 |
Correct |
297 ms |
56736 KB |
Output is correct |
4 |
Correct |
157 ms |
62876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
34072 KB |
Output is correct |
2 |
Correct |
330 ms |
56312 KB |
Output is correct |
3 |
Correct |
297 ms |
56736 KB |
Output is correct |
4 |
Correct |
157 ms |
62876 KB |
Output is correct |
5 |
Incorrect |
39 ms |
32780 KB |
Extra information in the output file |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
32768 KB |
Output is correct |
2 |
Correct |
141 ms |
49144 KB |
Output is correct |
3 |
Correct |
273 ms |
46972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
32768 KB |
Output is correct |
2 |
Correct |
141 ms |
49144 KB |
Output is correct |
3 |
Correct |
273 ms |
46972 KB |
Output is correct |
4 |
Incorrect |
36 ms |
31236 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
34060 KB |
Output is correct |
2 |
Correct |
319 ms |
56320 KB |
Output is correct |
3 |
Correct |
331 ms |
57052 KB |
Output is correct |
4 |
Correct |
160 ms |
63108 KB |
Output is correct |
5 |
Correct |
38 ms |
33012 KB |
Output is correct |
6 |
Correct |
151 ms |
48892 KB |
Output is correct |
7 |
Correct |
256 ms |
47108 KB |
Output is correct |
8 |
Correct |
160 ms |
49272 KB |
Output is correct |
9 |
Correct |
289 ms |
49996 KB |
Output is correct |
10 |
Correct |
309 ms |
49148 KB |
Output is correct |
11 |
Correct |
268 ms |
50612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
34060 KB |
Output is correct |
2 |
Correct |
319 ms |
56320 KB |
Output is correct |
3 |
Correct |
331 ms |
57052 KB |
Output is correct |
4 |
Correct |
160 ms |
63108 KB |
Output is correct |
5 |
Correct |
38 ms |
33012 KB |
Output is correct |
6 |
Correct |
151 ms |
48892 KB |
Output is correct |
7 |
Correct |
256 ms |
47108 KB |
Output is correct |
8 |
Correct |
160 ms |
49272 KB |
Output is correct |
9 |
Correct |
289 ms |
49996 KB |
Output is correct |
10 |
Correct |
309 ms |
49148 KB |
Output is correct |
11 |
Correct |
268 ms |
50612 KB |
Output is correct |
12 |
Incorrect |
40 ms |
32932 KB |
Extra information in the output file |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
30988 KB |
Output is correct |
2 |
Correct |
48 ms |
34056 KB |
Output is correct |
3 |
Correct |
97 ms |
39504 KB |
Output is correct |
4 |
Correct |
61 ms |
35596 KB |
Output is correct |
5 |
Correct |
56 ms |
35672 KB |
Output is correct |
6 |
Correct |
39 ms |
30464 KB |
Output is correct |
7 |
Correct |
34 ms |
29696 KB |
Output is correct |
8 |
Correct |
80 ms |
44400 KB |
Output is correct |
9 |
Correct |
69 ms |
44396 KB |
Output is correct |
10 |
Correct |
38 ms |
34060 KB |
Output is correct |
11 |
Correct |
283 ms |
56444 KB |
Output is correct |
12 |
Correct |
325 ms |
56828 KB |
Output is correct |
13 |
Correct |
175 ms |
62920 KB |
Output is correct |
14 |
Correct |
44 ms |
32772 KB |
Output is correct |
15 |
Correct |
138 ms |
48892 KB |
Output is correct |
16 |
Correct |
257 ms |
47096 KB |
Output is correct |
17 |
Correct |
164 ms |
49276 KB |
Output is correct |
18 |
Correct |
257 ms |
50044 KB |
Output is correct |
19 |
Correct |
264 ms |
49068 KB |
Output is correct |
20 |
Correct |
224 ms |
50548 KB |
Output is correct |
21 |
Correct |
94 ms |
47292 KB |
Output is correct |
22 |
Correct |
100 ms |
48636 KB |
Output is correct |
23 |
Correct |
198 ms |
53600 KB |
Output is correct |
24 |
Correct |
175 ms |
53936 KB |
Output is correct |
25 |
Correct |
303 ms |
54056 KB |
Output is correct |
26 |
Correct |
234 ms |
43644 KB |
Output is correct |
27 |
Correct |
212 ms |
43664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
30988 KB |
Output is correct |
2 |
Correct |
48 ms |
34056 KB |
Output is correct |
3 |
Correct |
97 ms |
39504 KB |
Output is correct |
4 |
Correct |
61 ms |
35596 KB |
Output is correct |
5 |
Correct |
56 ms |
35672 KB |
Output is correct |
6 |
Correct |
39 ms |
30464 KB |
Output is correct |
7 |
Correct |
34 ms |
29696 KB |
Output is correct |
8 |
Correct |
80 ms |
44400 KB |
Output is correct |
9 |
Correct |
69 ms |
44396 KB |
Output is correct |
10 |
Correct |
38 ms |
34060 KB |
Output is correct |
11 |
Correct |
283 ms |
56444 KB |
Output is correct |
12 |
Correct |
325 ms |
56828 KB |
Output is correct |
13 |
Correct |
175 ms |
62920 KB |
Output is correct |
14 |
Correct |
44 ms |
32772 KB |
Output is correct |
15 |
Correct |
138 ms |
48892 KB |
Output is correct |
16 |
Correct |
257 ms |
47096 KB |
Output is correct |
17 |
Correct |
164 ms |
49276 KB |
Output is correct |
18 |
Correct |
257 ms |
50044 KB |
Output is correct |
19 |
Correct |
264 ms |
49068 KB |
Output is correct |
20 |
Correct |
224 ms |
50548 KB |
Output is correct |
21 |
Correct |
94 ms |
47292 KB |
Output is correct |
22 |
Correct |
100 ms |
48636 KB |
Output is correct |
23 |
Correct |
198 ms |
53600 KB |
Output is correct |
24 |
Correct |
175 ms |
53936 KB |
Output is correct |
25 |
Correct |
303 ms |
54056 KB |
Output is correct |
26 |
Correct |
234 ms |
43644 KB |
Output is correct |
27 |
Correct |
212 ms |
43664 KB |
Output is correct |
28 |
Incorrect |
37 ms |
30732 KB |
Extra information in the output file |
29 |
Halted |
0 ms |
0 KB |
- |