This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
// Hi
int n;
vector<int> gr[5005];
set<pair<int,int>> traf;
vector<int> path;
void dfs(int u,int par,int v){
if(u == v){
path.push_back(v);
return;
}
for(auto i:gr[u]){
if(i != par){
dfs(i,u,v);
if(!path.empty())break;
}
}
if(!path.empty())path.push_back(u);
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);
cin>>n;
for(int i=1;i<n;i++){
int x,y;
cin>>x>>y;
x--,y--;
gr[x].push_back(y);
gr[y].push_back(x);
}
int k;
cin>>k;
for(int i=0;i<k;i++){
int x,y;
cin>>x>>y;
x--,y--;
traf.insert({x,y});
}
int q;
cin>>q;
while(q--){
int t;
cin>>t;
if(t == 1){
int x,y;
cin>>x>>y;
x--,y--;
traf.insert({x,y});
}else if(t == 2){
int x,y;
cin>>x>>y;
x--,y--;
traf.erase({x,y});
}else{
int x,y,l,r;
cin>>x>>y>>l>>r;
x--,y--;
vector<int> cnt(n+1,0);
for(auto i:traf){
path.clear();
dfs(i.first,-1,i.second);
reverse(path.begin(),path.end());
/*
for(auto pp:path)cout << pp <<" ";
cout << '\n';*/
int pos = 0;
int p = path.size();
for(int xx=0;xx<=r;xx++){
if(xx>=l)cnt[path[pos]]++;
if(pos == p-1)pos = 0;
else pos++;
}
}
path.clear();
dfs(x,-1,y);
reverse(path.begin(),path.end());
int ans = 0;
for(auto i:path)ans += cnt[i];
cout << ans << '\n';
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |