#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, q; cin >> n >> m >> q;
vector <vector<int>> G(n);
for ( int i = 1; i < n; i++ ){
int u, v; cin >> u >> v;
--u, --v;
G[u].pb(v), G[v].pb(u);
}
vector <int> C(m);
for ( auto &x: C ){
cin >> x; --x;
}
while ( q-- ){
int l, r; cin >> l >> r;
--l, --r;
int s = r - l + 1;
vector <int> sub(n), hv(n);
for ( int i = l; i <= r; i++ ){
sub[C[i]]++;
hv[C[i]] = true;
}
auto dfs = [&](auto dfs, int u, int p) -> void{
for ( auto &v: G[u] ){
if ( v != p ){
dfs(dfs, v, u);
sub[u] += sub[v];
}
}
};
dfs(dfs, 0, -1);
int ans = 0;
auto dfs2 = [&](auto dfs2, int u, int p) -> void{
bool flag = false, vs = false;
for ( auto &v: G[u] ){
if ( sub[v] ){
if ( !vs ) vs = true;
else flag = true;
}
}
if ( hv[u] || flag ) ++ans;
for ( auto &v: G[u] ){
if ( v != p ){
sub[u] -= sub[v];
sub[v] += sub[u];
dfs2(dfs2, v, u);
sub[v] -= sub[u];
sub[u] += sub[v];
}
}
};
dfs2(dfs2, 0, -1);
cout << ans << ln;
}
cout << '\n';
}
Compilation message
tourism.cpp: In function 'int main()':
tourism.cpp:49:13: warning: unused variable 's' [-Wunused-variable]
49 | int s = r - l + 1;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
3 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
464 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
488 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
348 KB |
Output is correct |
29 |
Correct |
3 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
3 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
464 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
488 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
348 KB |
Output is correct |
29 |
Correct |
3 ms |
348 KB |
Output is correct |
30 |
Correct |
59 ms |
600 KB |
Output is correct |
31 |
Correct |
72 ms |
580 KB |
Output is correct |
32 |
Correct |
119 ms |
604 KB |
Output is correct |
33 |
Correct |
121 ms |
612 KB |
Output is correct |
34 |
Correct |
119 ms |
608 KB |
Output is correct |
35 |
Correct |
80 ms |
856 KB |
Output is correct |
36 |
Correct |
82 ms |
604 KB |
Output is correct |
37 |
Correct |
81 ms |
632 KB |
Output is correct |
38 |
Correct |
99 ms |
688 KB |
Output is correct |
39 |
Correct |
104 ms |
604 KB |
Output is correct |
40 |
Correct |
96 ms |
600 KB |
Output is correct |
41 |
Correct |
103 ms |
604 KB |
Output is correct |
42 |
Correct |
101 ms |
600 KB |
Output is correct |
43 |
Correct |
95 ms |
672 KB |
Output is correct |
44 |
Correct |
101 ms |
636 KB |
Output is correct |
45 |
Correct |
100 ms |
640 KB |
Output is correct |
46 |
Correct |
98 ms |
640 KB |
Output is correct |
47 |
Correct |
97 ms |
624 KB |
Output is correct |
48 |
Correct |
101 ms |
600 KB |
Output is correct |
49 |
Correct |
96 ms |
628 KB |
Output is correct |
50 |
Correct |
81 ms |
604 KB |
Output is correct |
51 |
Correct |
80 ms |
632 KB |
Output is correct |
52 |
Correct |
79 ms |
636 KB |
Output is correct |
53 |
Correct |
80 ms |
628 KB |
Output is correct |
54 |
Correct |
79 ms |
856 KB |
Output is correct |
55 |
Correct |
80 ms |
604 KB |
Output is correct |
56 |
Correct |
4 ms |
348 KB |
Output is correct |
57 |
Correct |
75 ms |
584 KB |
Output is correct |
58 |
Correct |
74 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
5080 ms |
8552 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
5083 ms |
5368 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
5006 ms |
5552 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
3 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
464 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
488 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
348 KB |
Output is correct |
29 |
Correct |
3 ms |
348 KB |
Output is correct |
30 |
Correct |
59 ms |
600 KB |
Output is correct |
31 |
Correct |
72 ms |
580 KB |
Output is correct |
32 |
Correct |
119 ms |
604 KB |
Output is correct |
33 |
Correct |
121 ms |
612 KB |
Output is correct |
34 |
Correct |
119 ms |
608 KB |
Output is correct |
35 |
Correct |
80 ms |
856 KB |
Output is correct |
36 |
Correct |
82 ms |
604 KB |
Output is correct |
37 |
Correct |
81 ms |
632 KB |
Output is correct |
38 |
Correct |
99 ms |
688 KB |
Output is correct |
39 |
Correct |
104 ms |
604 KB |
Output is correct |
40 |
Correct |
96 ms |
600 KB |
Output is correct |
41 |
Correct |
103 ms |
604 KB |
Output is correct |
42 |
Correct |
101 ms |
600 KB |
Output is correct |
43 |
Correct |
95 ms |
672 KB |
Output is correct |
44 |
Correct |
101 ms |
636 KB |
Output is correct |
45 |
Correct |
100 ms |
640 KB |
Output is correct |
46 |
Correct |
98 ms |
640 KB |
Output is correct |
47 |
Correct |
97 ms |
624 KB |
Output is correct |
48 |
Correct |
101 ms |
600 KB |
Output is correct |
49 |
Correct |
96 ms |
628 KB |
Output is correct |
50 |
Correct |
81 ms |
604 KB |
Output is correct |
51 |
Correct |
80 ms |
632 KB |
Output is correct |
52 |
Correct |
79 ms |
636 KB |
Output is correct |
53 |
Correct |
80 ms |
628 KB |
Output is correct |
54 |
Correct |
79 ms |
856 KB |
Output is correct |
55 |
Correct |
80 ms |
604 KB |
Output is correct |
56 |
Correct |
4 ms |
348 KB |
Output is correct |
57 |
Correct |
75 ms |
584 KB |
Output is correct |
58 |
Correct |
74 ms |
604 KB |
Output is correct |
59 |
Correct |
0 ms |
344 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
4 ms |
348 KB |
Output is correct |
62 |
Execution timed out |
5080 ms |
8552 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |