#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 = 2e5 + 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 |
37 ms |
26368 KB |
Output is correct |
2 |
Correct |
45 ms |
29192 KB |
Output is correct |
3 |
Correct |
61 ms |
34812 KB |
Output is correct |
4 |
Correct |
52 ms |
30976 KB |
Output is correct |
5 |
Correct |
53 ms |
30988 KB |
Output is correct |
6 |
Correct |
44 ms |
25868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
26368 KB |
Output is correct |
2 |
Correct |
45 ms |
29192 KB |
Output is correct |
3 |
Correct |
61 ms |
34812 KB |
Output is correct |
4 |
Correct |
52 ms |
30976 KB |
Output is correct |
5 |
Correct |
53 ms |
30988 KB |
Output is correct |
6 |
Correct |
44 ms |
25868 KB |
Output is correct |
7 |
Incorrect |
35 ms |
26120 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
24832 KB |
Output is correct |
2 |
Runtime error |
88 ms |
76408 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
24832 KB |
Output is correct |
2 |
Runtime error |
88 ms |
76408 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
29256 KB |
Output is correct |
2 |
Runtime error |
292 ms |
100648 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
29256 KB |
Output is correct |
2 |
Runtime error |
292 ms |
100648 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
28044 KB |
Output is correct |
2 |
Runtime error |
86 ms |
82408 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
28044 KB |
Output is correct |
2 |
Runtime error |
86 ms |
82408 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
29184 KB |
Output is correct |
2 |
Runtime error |
272 ms |
100604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
29184 KB |
Output is correct |
2 |
Runtime error |
272 ms |
100604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
26456 KB |
Output is correct |
2 |
Correct |
48 ms |
29280 KB |
Output is correct |
3 |
Correct |
61 ms |
34812 KB |
Output is correct |
4 |
Correct |
50 ms |
30788 KB |
Output is correct |
5 |
Correct |
55 ms |
30972 KB |
Output is correct |
6 |
Correct |
37 ms |
25868 KB |
Output is correct |
7 |
Correct |
34 ms |
24836 KB |
Output is correct |
8 |
Runtime error |
84 ms |
76384 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
26456 KB |
Output is correct |
2 |
Correct |
48 ms |
29280 KB |
Output is correct |
3 |
Correct |
61 ms |
34812 KB |
Output is correct |
4 |
Correct |
50 ms |
30788 KB |
Output is correct |
5 |
Correct |
55 ms |
30972 KB |
Output is correct |
6 |
Correct |
37 ms |
25868 KB |
Output is correct |
7 |
Correct |
34 ms |
24836 KB |
Output is correct |
8 |
Runtime error |
84 ms |
76384 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |