#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define F first
#define S second
using namespace std;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
const int inf = 1e9;
const int LOG = 20;
int in[N],out[N],up[N][LOG],timer = 0,a[N],c[N];
vector<int>g[N];
void Dfs(int x,int par) {
in[x] = ++timer;
up[x][0] = par;
for(int j = 1; j < LOG; j++) up[x][j] = up[up[x][j - 1]][j - 1];
for(int j : g[x]) {
if(j != par) {
Dfs(j,x);
}
}
out[x] = timer;
}
void dfs(int x,int par) {
for(int j : g[x]) {
if(j != par) {
dfs(j,x);
a[x] += a[j];
}
}
}
bool In(int u,int v) {
return in[u] <= in[v] && out[u] >= out[v];
}
int LCA(int u,int v) {
if(In(u,v)) return u;
if(In(v,u)) return v;
for(int j = LOG - 1; j >= 0; j--) {
if(up[u][j] > 0 && !In(up[u][j],v)) u = up[u][j];
}
return up[u][0];
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m,q;
cin >> n >> m >> q;
for(int i = 1; i < n; i++) {
int u,v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
Dfs(1,0);
for(int i = 1; i <= m; i++) cin >> c[i];
while(q--) {
int l,r;
cin >> l >> r;
if(r - l == 0) {
cout << 1 << "\n";
continue;
}
for(int i = 1; i <= n; i++) a[i] = 0;
for(int i = l; i < r; i++) {
a[c[i]]++;
a[c[i + 1]]++;
a[up[LCA(c[i],c[i + 1])][0]] -= 2;
}
dfs(1,0);
int ans = 0;
for(int i = 1; i <= n; i++) if(a[i]) ans++;
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |