#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ar array
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, q; cin >> n >> m >> q;
vector <int> X(n - 1), Y(n - 1);
for ( int i = 0; i < n - 1; i++ ){
cin >> X[i] >> Y[i];
--X[i], --Y[i];
}
vector <int> state(n - 1), R(n - 1, -1), L(n - 1, m + 1);
for ( int i = 0; i < m; i++ ){
int x; cin >> x;
--x;
if ( L[x] == m + 1 ) L[x] = i;
R[x] = i;
state[x] ^= 1;
}
for ( int i = 0; i < n - 1; i++ ){
if ( state[i] > 0 ) R[i] = m;
}
auto calc = [&](int rt){
vector <vector<ar<int,2>>> adj(n);
for ( int i = 0; i < n - 1; i++ ){
adj[X[i]].pb({Y[i], i});
adj[Y[i]].pb({X[i], i});
}
auto dfs = [&](auto dfs, int u, int p, int r) -> int{
int ret = 1;
for ( auto &[v, j]: adj[u] ){
if ( v != p ){
if ( r >= L[j] ){
ret += dfs(dfs, v, u, min(r, R[j]));
}
}
}
return ret;
};
return dfs(dfs, rt, -1, m);
};
while ( q-- ){
int x; cin >> x;
cout << calc(x - 1) << '\n';
}
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
1372 KB |
Output is correct |
8 |
Incorrect |
3 ms |
1372 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
12344 KB |
Output is correct |
2 |
Correct |
26 ms |
11212 KB |
Output is correct |
3 |
Correct |
24 ms |
13660 KB |
Output is correct |
4 |
Correct |
24 ms |
13044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
8048 ms |
10052 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |