#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define io ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ld long double
#define _all(T) T.begin(),T.end()
const int mxn = 1e3+10;
vector<int> paths[mxn];
int fa[mxn],link_top[mxn],bs[mxn],sz[mxn],dep[mxn],arr[mxn];
void dfs1(int now,int par){
fa[now] = par;
sz[now] = 1;
for(auto nxt:paths[now]){
if(nxt == par)continue;
dep[nxt] = dep[now]+1;
dfs1(nxt,now);
if(bs[now] == 0||sz[bs[now]] < sz[nxt])bs[now] = nxt;
sz[now] += sz[nxt];
}
return;
}
void dfs2(int now,int top){
link_top[now] = top;
if(bs[now])dfs2(bs[now],top);
for(auto nxt:paths[now]){
if(nxt == fa[now]||nxt == bs[now])continue;
dfs2(nxt,nxt);
}
return;
}
int lca(int a,int b){
int ta = link_top[a],tb = link_top[b];
while(ta != tb){
if(dep[ta]<dep[tb]){
swap(ta,tb);
swap(a,b);
}
a = fa[ta];
ta = link_top[a];
}
if(dep[a]>dep[b])return b;
else return a;
}
int main(){
io
int n,m,q;
cin>>n>>m>>q;
if(m*m*q*__lg(n)>=1e9)return 0;
for(int i = 0;i<n-1;i++){
int a,b;
cin>>a>>b;
paths[a].push_back(b);
paths[b].push_back(a);
}
dfs1(1,1);
dfs2(1,1);
for(int i = 1;i<=m;i++){
cin>>arr[i];
}
// cout<<lca(3,4);
while(q--){
int t;
cin>>t;
if(t == 1){
int p,v;
cin>>p>>v;
arr[p] = v;
}
else{
int l,r,v;
cin>>l>>r>>v;
pii ans = {-1,-1};
for(int i = l;i<=r;i++){
int now = arr[i];
for(int j= i;j<=r;j++){
now = lca(now,arr[j]);
if(now == v){
ans = {i,j};
}
}
}
cout<<ans.fs<<' '<<ans.sc<<'\n';
}
}
return 0;
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
n=5 |
2 |
Correct |
1 ms |
340 KB |
n=100 |
3 |
Correct |
1 ms |
340 KB |
n=100 |
4 |
Correct |
2 ms |
340 KB |
n=100 |
5 |
Correct |
1 ms |
340 KB |
n=100 |
6 |
Correct |
4 ms |
340 KB |
n=100 |
7 |
Correct |
4 ms |
340 KB |
n=100 |
8 |
Correct |
1 ms |
340 KB |
n=100 |
9 |
Correct |
1 ms |
340 KB |
n=100 |
10 |
Correct |
3 ms |
348 KB |
n=100 |
11 |
Correct |
2 ms |
340 KB |
n=100 |
12 |
Correct |
1 ms |
348 KB |
n=100 |
13 |
Correct |
1 ms |
340 KB |
n=100 |
14 |
Correct |
1 ms |
340 KB |
n=100 |
15 |
Correct |
1 ms |
340 KB |
n=100 |
16 |
Correct |
1 ms |
348 KB |
n=100 |
17 |
Correct |
1 ms |
340 KB |
n=100 |
18 |
Correct |
1 ms |
340 KB |
n=100 |
19 |
Correct |
1 ms |
340 KB |
n=100 |
20 |
Correct |
1 ms |
340 KB |
n=100 |
21 |
Correct |
1 ms |
340 KB |
n=100 |
22 |
Correct |
1 ms |
344 KB |
n=100 |
23 |
Correct |
1 ms |
340 KB |
n=100 |
24 |
Correct |
1 ms |
340 KB |
n=100 |
25 |
Correct |
1 ms |
348 KB |
n=100 |
26 |
Correct |
1 ms |
340 KB |
n=12 |
27 |
Correct |
2 ms |
348 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
n=5 |
2 |
Correct |
1 ms |
340 KB |
n=100 |
3 |
Correct |
1 ms |
340 KB |
n=100 |
4 |
Correct |
2 ms |
340 KB |
n=100 |
5 |
Correct |
1 ms |
340 KB |
n=100 |
6 |
Correct |
4 ms |
340 KB |
n=100 |
7 |
Correct |
4 ms |
340 KB |
n=100 |
8 |
Correct |
1 ms |
340 KB |
n=100 |
9 |
Correct |
1 ms |
340 KB |
n=100 |
10 |
Correct |
3 ms |
348 KB |
n=100 |
11 |
Correct |
2 ms |
340 KB |
n=100 |
12 |
Correct |
1 ms |
348 KB |
n=100 |
13 |
Correct |
1 ms |
340 KB |
n=100 |
14 |
Correct |
1 ms |
340 KB |
n=100 |
15 |
Correct |
1 ms |
340 KB |
n=100 |
16 |
Correct |
1 ms |
348 KB |
n=100 |
17 |
Correct |
1 ms |
340 KB |
n=100 |
18 |
Correct |
1 ms |
340 KB |
n=100 |
19 |
Correct |
1 ms |
340 KB |
n=100 |
20 |
Correct |
1 ms |
340 KB |
n=100 |
21 |
Correct |
1 ms |
340 KB |
n=100 |
22 |
Correct |
1 ms |
344 KB |
n=100 |
23 |
Correct |
1 ms |
340 KB |
n=100 |
24 |
Correct |
1 ms |
340 KB |
n=100 |
25 |
Correct |
1 ms |
348 KB |
n=100 |
26 |
Correct |
1 ms |
340 KB |
n=12 |
27 |
Correct |
2 ms |
348 KB |
n=100 |
28 |
Incorrect |
1 ms |
340 KB |
Unexpected end of file - int32 expected |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
n=5 |
2 |
Correct |
1 ms |
340 KB |
n=100 |
3 |
Correct |
1 ms |
340 KB |
n=100 |
4 |
Correct |
2 ms |
340 KB |
n=100 |
5 |
Correct |
1 ms |
340 KB |
n=100 |
6 |
Correct |
4 ms |
340 KB |
n=100 |
7 |
Correct |
4 ms |
340 KB |
n=100 |
8 |
Correct |
1 ms |
340 KB |
n=100 |
9 |
Correct |
1 ms |
340 KB |
n=100 |
10 |
Correct |
3 ms |
348 KB |
n=100 |
11 |
Correct |
2 ms |
340 KB |
n=100 |
12 |
Correct |
1 ms |
348 KB |
n=100 |
13 |
Correct |
1 ms |
340 KB |
n=100 |
14 |
Correct |
1 ms |
340 KB |
n=100 |
15 |
Correct |
1 ms |
340 KB |
n=100 |
16 |
Correct |
1 ms |
348 KB |
n=100 |
17 |
Correct |
1 ms |
340 KB |
n=100 |
18 |
Correct |
1 ms |
340 KB |
n=100 |
19 |
Correct |
1 ms |
340 KB |
n=100 |
20 |
Correct |
1 ms |
340 KB |
n=100 |
21 |
Correct |
1 ms |
340 KB |
n=100 |
22 |
Correct |
1 ms |
344 KB |
n=100 |
23 |
Correct |
1 ms |
340 KB |
n=100 |
24 |
Correct |
1 ms |
340 KB |
n=100 |
25 |
Correct |
1 ms |
348 KB |
n=100 |
26 |
Correct |
1 ms |
340 KB |
n=12 |
27 |
Correct |
2 ms |
348 KB |
n=100 |
28 |
Incorrect |
1 ms |
340 KB |
Unexpected end of file - int32 expected |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
n=5 |
2 |
Correct |
1 ms |
340 KB |
n=100 |
3 |
Correct |
1 ms |
340 KB |
n=100 |
4 |
Correct |
2 ms |
340 KB |
n=100 |
5 |
Correct |
1 ms |
340 KB |
n=100 |
6 |
Correct |
4 ms |
340 KB |
n=100 |
7 |
Correct |
4 ms |
340 KB |
n=100 |
8 |
Correct |
1 ms |
340 KB |
n=100 |
9 |
Correct |
1 ms |
340 KB |
n=100 |
10 |
Correct |
3 ms |
348 KB |
n=100 |
11 |
Correct |
2 ms |
340 KB |
n=100 |
12 |
Correct |
1 ms |
348 KB |
n=100 |
13 |
Correct |
1 ms |
340 KB |
n=100 |
14 |
Correct |
1 ms |
340 KB |
n=100 |
15 |
Correct |
1 ms |
340 KB |
n=100 |
16 |
Correct |
1 ms |
348 KB |
n=100 |
17 |
Correct |
1 ms |
340 KB |
n=100 |
18 |
Correct |
1 ms |
340 KB |
n=100 |
19 |
Correct |
1 ms |
340 KB |
n=100 |
20 |
Correct |
1 ms |
340 KB |
n=100 |
21 |
Correct |
1 ms |
340 KB |
n=100 |
22 |
Correct |
1 ms |
344 KB |
n=100 |
23 |
Correct |
1 ms |
340 KB |
n=100 |
24 |
Correct |
1 ms |
340 KB |
n=100 |
25 |
Correct |
1 ms |
348 KB |
n=100 |
26 |
Correct |
1 ms |
340 KB |
n=12 |
27 |
Correct |
2 ms |
348 KB |
n=100 |
28 |
Incorrect |
1 ms |
340 KB |
Unexpected end of file - int32 expected |
29 |
Halted |
0 ms |
0 KB |
- |