#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ii pair<int,int>
const int MAXN=2e5+5;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
bool removed[MAXN];
int join[20][MAXN];
vector<pair<int,int>> cnt[MAXN];
vector<pair<int,int>> adj[MAXN];
int sz[MAXN];
int up[20][MAXN];
int parent[20][MAXN];
int get_sz(int x,int p){
sz[x]=1;
for(auto u:adj[x]){
if(removed[u.fi]||u.fi==p) continue;
sz[x]+=get_sz(u.fi,x);
}
return sz[x];
}
int get_centroid(int x,int t_sz,int p){
for(auto u:adj[x]){
if(u.fi==p||removed[u.fi]) continue;
if(sz[u.fi]*2>t_sz) return get_centroid(u.fi,t_sz,x);
}
return x;
}
bool incre[20][MAXN] , decre[20][MAXN];
int cur;
void dfs(int x,int p,int edgep,int centroid,int lvl,int edge){
for(auto u:adj[x]){
if(u.fi==p||removed[u.fi]) continue;
decre[lvl][u.fi]=decre[x];
incre[lvl][u.fi]=incre[x];
if(edgep<u.se) incre[lvl][u.fi]=false;
if(edgep>u.se) decre[lvl][u.fi]=false;
dfs(u.fi,x,u.se,centroid,lvl,edge);
}
cur++;
if(incre[lvl][x]) join[lvl][x]=edge;
if(decre[lvl][x]) up[lvl][x] = edgep;
parent[lvl][x] = centroid;
}
void build(int x,int lvl){
int centroid=get_centroid(x,get_sz(x,-1),-1);
parent[lvl][centroid]=centroid;
join[lvl][centroid] = -1;
up[lvl][centroid] = 1e9;
incre[lvl][centroid]=true;
decre[lvl][centroid]=true;
for(auto u:adj[centroid]){
if(removed[u.fi]) continue;
cur=0;
incre[lvl][u.fi] = true;
decre[lvl][u.fi] = true;
dfs(u.fi,x,u.se,centroid,lvl,u.se);
cnt[centroid].pb({u.se,cur});
}
removed[centroid]=true;
for(auto u : adj[centroid]){
if(removed[u.fi]) continue;
build(u.fi,lvl+1);
}
}
bool query(int x,int y,int t)
{
if(x == y)
return true;
bool test=false;
for(int lvl = 0 ; lvl < 20 ; lvl++)
{
if(parent[lvl][x]!=parent[lvl][y]) continue;
int centroid = parent[lvl][x];
if(centroid == -1)
continue;
if(!decre[lvl][x] || !incre[lvl][y])
continue;
if(x == centroid)
{
test|=(join[lvl][y] <= t);
}else if(y == centroid){
test|=(up[lvl][x] <= t);
}else
{
int edgep = up[lvl][x];
test|= ((edgep <= t) && (join[lvl][y]<=edgep));
}
}
return test;
}
int main(){
int n,k;
cin>>n>>k;
memset(parent,-1,sizeof parent);
for(int i=0;i<20;i++){
for(int j=0;j<n;j++){
join[i][j]=1e9;
up[i][j]=1e9;
}
}
vector<pair<char , pair<int,int>>> queries;
for(int i = 0 ; i < n - 1 + k ; i++)
{
char c;
cin>>c;
if(c == 'S')
{
int a , b;
cin>>a>>b;
a-- , b--;
adj[a].pb({b , i});
adj[b].pb({a , i});
queries.pb({c , {a , b}});
}
else if(c == 'Q')
{
int a , d;
cin>>a>>d;
a-- , d--;
queries.pb({c , {a , d}});
}else{
int a;
cin>>a;
a--;
queries.pb({c , {a , -1}});
}
}
build(0,0);
for(int i = 0 ; i < n - 1 + k ; i++)
{
auto Cur = queries[i];
if(Cur.fi=='Q'){
cout << (query(Cur.se.fi , Cur.se.se , i) ? "yes" : "no") <<endl;
}
// else if(Cur.fi=='C') cout << 0 <<endl;
// if(cur.fi == 'C')
// {
// }
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
146 ms |
29936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
146 ms |
29936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
29856 KB |
Output is correct |
2 |
Correct |
243 ms |
57132 KB |
Output is correct |
3 |
Correct |
230 ms |
57208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
29856 KB |
Output is correct |
2 |
Correct |
243 ms |
57132 KB |
Output is correct |
3 |
Correct |
230 ms |
57208 KB |
Output is correct |
4 |
Incorrect |
141 ms |
29888 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
150 ms |
29888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
150 ms |
29888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
158 ms |
29892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
158 ms |
29892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
160 ms |
31292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
160 ms |
31292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
152 ms |
31092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
152 ms |
31092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |