#include<bits/stdc++.h>
using namespace std;
const int N = 1e5;
vector <int> e[N];
vector <pair<int,int>> q2[N];
int sub[N];
int top[N],bot[N];
int st[N],cnt = 0;
int c[N];
int d[N];
int pr[N];
int ans[N];
int st2[N];
int n,m,q;
void dfs(int node, int p){
sub[node] = 1;
pr[node] = p;
if(p == -1){
d[node] = 0;
}else{
d[node] = d[p] + 1;
}
for(auto i:e[node]){
if(i == p)continue;
dfs(i, node);
sub[node]+=sub[i];
}
}
void dfs2(int node, int p){
int mxid = -1;
for(auto i:e[node]){
if(i == p)continue;
if(mxid == -1 || sub[mxid] < sub[i]){
mxid = i;
}
}
st[node] = cnt++;
st2[cnt - 1] = node;
if(mxid != -1){
top[mxid] = top[node];
dfs2(mxid, node);
bot[node] = bot[mxid];
}else{
bot[node] = node;
}
for(auto i:e[node]){
if(i == p || i == mxid)continue;
top[i] = i;
dfs2(i, node);
}
}
int v[N];
void add(int u,int w,int id){
u = st[u];
w = st[w];
if(u > w)swap(u,w);
///segment override
for(int i = u;i <= w;i++){
v[i] = id;
}
}
int get(int pos){
int r = 0;
for(int i = 0;i < n;i++){
//cout<<v[i]<<' '<<st2[i]<<'\n';
if(pos <= v[i])r++;
}
//cout<<'\n';
return r;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m>>q;
for(int i = 0;i < n - 1;i++){
int a,b;
cin>>a>>b;
a--;b--;
e[a].push_back(b);
e[b].push_back(a);
v[i] = -1;
}
v[n - 1] = -1;
dfs(0, -1);
top[0] = 0;
dfs2(0, -1);
for(int i = 0;i < m;i++){
cin>>c[i];
c[i]--;
}
for(int i = 0;i < q;i++){
int l,r;
cin>>l>>r;
if(l == r){
ans[i] = 1;
continue;
}
q2[r - 1].push_back({l - 1,i});
}
for(int i = 1;i < m;i++){
///add
int u = c[i],w = c[i - 1];
while(top[u] != top[w]){
if(d[u] > d[w])swap(u,w);
add(w,top[w],i - 1);
w = pr[top[w]];
}
add(u,w,i - 1);
///queries
for(auto j:q2[i]){
ans[j.second] = get(j.first);
}
}
for(int i = 0;i < q;i++){
cout<<ans[i]<<'\n';
}
return 0;
}
/**
7 6 2
1 2
1 3
2 4
2 5
3 6
3 7
2 3 6 4 5 7
1 3
4 6
**/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
3 ms |
8892 KB |
Output is correct |
3 |
Incorrect |
2 ms |
8796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
3 ms |
8892 KB |
Output is correct |
3 |
Incorrect |
2 ms |
8796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
3 ms |
8796 KB |
Output is correct |
3 |
Correct |
4 ms |
8792 KB |
Output is correct |
4 |
Correct |
3106 ms |
18932 KB |
Output is correct |
5 |
Correct |
3787 ms |
21392 KB |
Output is correct |
6 |
Correct |
3975 ms |
22736 KB |
Output is correct |
7 |
Execution timed out |
5053 ms |
24660 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8792 KB |
Output is correct |
2 |
Incorrect |
94 ms |
11828 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9048 KB |
Output is correct |
2 |
Correct |
4 ms |
8796 KB |
Output is correct |
3 |
Correct |
4 ms |
8944 KB |
Output is correct |
4 |
Incorrect |
3340 ms |
15444 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
3 ms |
8892 KB |
Output is correct |
3 |
Incorrect |
2 ms |
8796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |