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;
const int MAXN=200005,LOG=18;
vector<int> adj[MAXN];
int depth[MAXN],pa[LOG][MAXN],a[MAXN];
int naik(int x,int mask){
for(int i=LOG-1;i>=0;i--){
if(mask & (1<<i))x=pa[i][x];
}
return x;
}
void dfs(int now,int par){
for(auto nxt : adj[now]){
if(nxt==par)continue;
depth[nxt]=depth[now]+1;
pa[0][nxt]=now;
for(int i=1;i<LOG;i++)pa[i][nxt]=pa[i-1][pa[i-1][nxt]];
dfs(nxt,now);
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n,m,q,op,x,y,u,v;
cin >> n >> m >> q;
for(int i=1;i<n;i++){
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1,-1);
for(int i=1;i<=m;i++)cin >> a[i];
while(q--){
cin >> op >> x >> y;
if(op==1)a[x]=y;
else{
cin >> v;
bool sudah=false;
for(int i=x;i<y;i++){
int selisih=depth[a[i]]-depth[v];
if(selisih<0)continue;
bool ya1=(naik(a[i],selisih)==v);
if(!ya1)continue;
int ortu1;
if(selisih==0){
cout << i << " " << i << '\n';
sudah=true;
break;
}
else ortu1=naik(a[i],selisih-1);
selisih=depth[a[i+1]]-depth[v];
if(selisih<0)continue;
ya1=(naik(a[i+1],selisih)==v);
if(!ya1)continue;
int ortu2;
if(selisih==0){
cout << i+1 << " " << i+1 << '\n';
sudah=true;
break;
}
else ortu2=naik(a[i+1],selisih-1);
if(ortu1==ortu2)continue;
cout << i << " " << i+1 << '\n';
sudah=true;
break;
}
if(!sudah){
cout << -1 << " " << -1 << '\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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |