#include <bits/stdc++.h>
using namespace std;
vector<pair<int,int>> adj[120005];
bool ss = 0;
int sz[120005];
int vis[120005];
map<int,pair<int,int>> aw[120005];
int dfs(int no,int pr){
sz[no] = 1;
for(auto i:adj[no]){
if(i.first==pr||vis[i.first]!=0)continue;
sz[no]+=dfs(i.first,no);
}
return sz[no];
}
int get_centroid(int no,int ss,int pr){
for(auto i:adj[no]){
if(pr==i.first||vis[i.first]!=0)continue;
if(sz[i.first]*2>ss){
return get_centroid(i.first,ss,no);
}
}
return no;
}
vector<pair<pair<int,int>,pair<int,int>>> v[120005*2];
void calc(int i,int pr,int la,int ex,int nah,int le){
v[la].push_back({{ex,nah},{i,le}});
for(auto j:adj[i]){
if((!(vis[j.first]||j.first==pr))&&j.second>la){
calc(j.first,i,j.second,ex,nah,le);
}
}
}
map<int,pair<int,int>> mp[120005];
void calc2(int i,int pr,int la,int ex,int nah,int le,bool ss){
if(ss)mp[ex][i] = {nah,le};
else mp[ex][i] = {-1,-1};
for(auto j:adj[i]){
if((!(vis[j.first]||j.first==pr))&&j.second<la){
calc2(j.first,i,j.second,ex,nah,le,ss);
}else if(!(vis[j.first]||j.first==pr)){
calc2(j.first,i,j.second,ex,nah,le,0);
}
}
}
vector<int> lol[120005],val[120005];
int pr[120005];
void centroid(int no,int par){
int cen = get_centroid(no,dfs(no,-1),-1);
pr[cen] = par;
vis[cen] = 1;
val[cen].push_back(0);
for(auto i:adj[cen]){
if(vis[i.first])continue;
calc(i.first,-1,i.second,cen,i.second,i.first);
calc2(i.first,-1,i.second,cen,i.first,i.second,1);
lol[cen].push_back(i.second);
val[cen].push_back(0);
}
for(auto i:adj[cen]){
if(vis[i.first])continue;
centroid(i.first,cen);
}
}
void add(int x,int d){
int it = lower_bound(lol[x].begin(),lol[x].end(),d)-lol[x].begin();
it++;
while(it<=lol[x].size()){
val[x][it]++;
it+=it&-it;
}
}
int sum(int x,int d){
int it = lower_bound(lol[x].begin(),lol[x].end(),d)-lol[x].begin();
it++;it = min(it,(int)lol[x].size());
int su = 0;
while(it>=1){
su+=val[x][it];
it-=it&-it;
}return su;
}
int main(){
//freopen("input.txt","r",stdin);
//freopen("outout.txt","w",stdout);
int n,k;
cin>>n>>k;
k+=n-1;
vector<vector<int>> qu;
for(int i = 0;i<k;i++){
char c;cin>>c;
if(c=='S'){
int a,b;cin>>a>>b;
adj[a].push_back({b,i});
adj[b].push_back({a,i});
qu.push_back({-1});
}else if(c=='C'){
int d;cin>>d;
qu.push_back({d});
}else{
int a,b;cin>>a>>b;
qu.push_back({b,a});
}
}
centroid(1,0);
for(int i = 1;i<=n;i++){
sort(lol[i].begin(),lol[i].end());
}
int xd = 0;
for(auto i:qu){
if(i[0]==-1){
for(auto j:v[xd]){
add(j.first.first,j.first.second);
aw[j.first.first][j.second.first] = {j.second.second,j.first.second};
}
}else if(i.size()==1){
int xd = i[0];
long long all = 0;
while(xd!=0){
if(xd==i[0]){
all+=sum(xd,1e9);
all++;
}else{
if(mp[xd][i[0]].first!=-1){
all+=sum(xd,1e9)-sum(xd,mp[xd][i[0]].second);
all++;
}
}
xd = pr[xd];
}
cout<<all<<endl;
}else{
if(i[0]==i[1]){
cout<<"yes\n";
continue;
}
int b = i[1];
int xd = i[0];
long long all = 0;
bool sss = 0;
while(xd!=0){
if(xd==i[0]){
if(aw[xd][b].first!=0){
sss = 1;
break;
}
}else{
if(xd==b&&mp[xd][i[0]].first!=-1){
sss = 1;break;
}
if(mp[xd][i[0]].first!=-1&&aw[xd][b].first!=0&&aw[xd][b].first!=mp[xd][i[0]].first&&mp[xd][i[0]].second<aw[xd][b].second){
sss = 1; break ;
}
}
xd = pr[xd];
}
if(sss)cout<<"yes\n";
else cout<<"no\n";
}
xd++;
}
}
Compilation message
servers.cpp: In function 'void add(int, int)':
servers.cpp:68:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | while(it<=lol[x].size()){
| ~~^~~~~~~~~~~~~~~
servers.cpp: In function 'int main()':
servers.cpp:138:23: warning: unused variable 'all' [-Wunused-variable]
138 | long long all = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
88 ms |
34044 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
88 ms |
34044 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
66 ms |
34052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
66 ms |
34052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
72 ms |
34044 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
72 ms |
34044 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
80 ms |
34044 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
80 ms |
34044 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
68 ms |
34004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
68 ms |
34004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
76 ms |
34496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
76 ms |
34496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |