#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+1});
adj[b].push_back({a,i+1});
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 = 1;
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 nn = i[0];
long long all = 0;
while(nn!=0){
if(nn==i[0]){
all+=sum(nn,1e9);
all++;
}else{
if(mp[nn][i[0]].first!=-1&&mp[nn][i[0]].second<=xd){
all+=sum(nn,1e9)-sum(nn,mp[nn][i[0]].second);
all++;
}
}
nn = pr[nn];
}
cout<<all<<endl;
}else{
if(i[0]==i[1]){
cout<<"yes\n";
continue;
}
int b = i[1];
int nn = i[0];
long long all = 0;
bool sss = 0;
while(nn!=0){
if(nn==i[0]){
if(aw[nn][b].first!=0){
sss = 1;
break;
}
}else{
if(nn==b&&mp[nn][i[0]].first!=-1&&mp[nn][i[0]].second<=xd){
sss = 1;break;
}
if(nn!=b&&mp[nn][i[0]].first!=-1&&aw[nn][b].first!=0&&aw[nn][b].first!=mp[nn][i[0]].first&&mp[nn][i[0]].second<aw[nn][b].second){
sss = 1; break ;
}
}
nn = pr[nn];
}
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 |
Correct |
88 ms |
33976 KB |
Output is correct |
2 |
Incorrect |
329 ms |
54764 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
33976 KB |
Output is correct |
2 |
Incorrect |
329 ms |
54764 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
34044 KB |
Output is correct |
2 |
Incorrect |
676 ms |
75752 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
34044 KB |
Output is correct |
2 |
Incorrect |
676 ms |
75752 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
34120 KB |
Output is correct |
2 |
Incorrect |
1858 ms |
186456 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
34120 KB |
Output is correct |
2 |
Incorrect |
1858 ms |
186456 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
34080 KB |
Output is correct |
2 |
Incorrect |
3109 ms |
260244 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
34080 KB |
Output is correct |
2 |
Incorrect |
3109 ms |
260244 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
33940 KB |
Output is correct |
2 |
Incorrect |
1626 ms |
186492 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
33940 KB |
Output is correct |
2 |
Incorrect |
1626 ms |
186492 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
34048 KB |
Output is correct |
2 |
Incorrect |
305 ms |
54848 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
34048 KB |
Output is correct |
2 |
Incorrect |
305 ms |
54848 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |