#include <bits/stdc++.h>
using namespace std;
template<class T>
int len(T &a){
return a.size();
}
using ll = long long;
using pii = pair<int,int>;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
string fileio = "";
mt19937 mt(time(nullptr));
const int mod = 1e9 + 7;
const int inf = 1e9;
const ll infl = 1e18;
const int maxn = 1e5 + 1;
struct Node{
int l, r, i;
};
struct Sparsemin{
vector<vector<Node>> t;
int n, logn;
void init(int _n){
n = _n;
logn = 32 - __builtin_clz(n);
t.assign(logn, vector<Node> (n));
}
Node merge(Node a, Node b){
if(a.l < b.l) return a;
if(b.l < a.l) return b;
if(a.r > b.r) return a;
if(b.r > a.r) return b;
if(a.i < b.i) return a;
return b;
}
void build(vector<int> &l, vector<int> &r){
init(len(l));
for(int i = 0; i < n; i ++){
t[0][i] = {l[i], r[i], i};
}
for(int j = 1; j < logn; j ++){
for(int i = 0; i + (1 << j) - 1 < n; i ++){
t[j][i] = merge(t[j - 1][i], t[j - 1][i + (1 << (j - 1))]);
}
}
}
int get(int l, int r){
int k = 31 - __builtin_clz(r - l + 1);
return merge(t[k][l], t[k][l + (1 << k)]).i;
}
};
struct Sparsemax{
vector<vector<Node>> t;
int n, logn;
void init(int _n){
n = _n;
logn = 32 - __builtin_clz(n);
t.assign(logn, vector<Node> (n));
}
Node merge(Node a, Node b){
if(a.r > b.r) return a;
if(b.r > a.r) return b;
if(a.l < b.l) return a;
if(b.l < a.l) return b;
if(a.i > b.i) return a;
return b;
}
void build(vector<int> &l, vector<int> &r){
init(len(l));
for(int i = 0; i < n; i ++){
t[0][i] = {l[i], r[i], i};
}
for(int j = 1; j < logn; j ++){
for(int i = 0; i + (1 << j) - 1 < n; i ++){
t[j][i] = merge(t[j - 1][i], t[j - 1][i + (1 << (j - 1))]);
}
}
}
int get(int l, int r){
int k = 31 - __builtin_clz(r - l + 1);
//cout << l << ' ' << r << ' ' << k << endl;
return merge(t[k][l], t[k][r - (1 << k) + 1]).i;
}
};
void solve(){
int n, m, q; cin >> n >> m >> q;
vector<int> sight(m);
vector<vector<int>> g(n);
for(int i = 1; i < n; i ++){
int u, v; cin >> u >> v;
u --; v --;
g[u].push_back(v); g[v].push_back(u);
}
for(int i = 0; i < m; i ++){
cin >> sight[i]; sight[i] --;
}
vector<int> par(n);
auto dfs = [&](auto &dfs, int i, int p = -1)->void{
par[i] = p;
for(auto u : g[i]){
if(u == p) continue;
dfs(dfs, u, i);
}
};
while(q --){
int l, r; cin >> l >> r;
l --; r --;
dfs(dfs, sight[l]);
int cnt = 1;
vector<int> vis(n);
vis[sight[l]] = 1;
for(int j = l + 1; j <= r; j ++){
int cur = sight[j];
while(vis[cur] == 0){
vis[cur] = 1;
//cout << "cur " << cur + 1 << endl;
cnt ++; cur = par[cur];
}
}
cout << cnt << '\n';
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
auto start = chrono::high_resolution_clock::now();
#ifndef LOCAL
if(fileio.size()){
freopen((fileio + ".in").c_str(), "r", stdin);
freopen((fileio + ".out").c_str(), "w", stdout);
}
#endif
int testcases = 1;
#ifdef Tests
cin >> testcases;
#endif
while(testcases --){
solve();
cout << '\n';
#ifdef LOCAL
cout << "_________________________" << endl;
#endif
}
#ifdef LOCAL
auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
cout << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
#endif
return 0;
}
Compilation message
tourism.cpp: In function 'int main()':
tourism.cpp:161:10: warning: variable 'start' set but not used [-Wunused-but-set-variable]
161 | auto start = chrono::high_resolution_clock::now();
| ^~~~~
tourism.cpp:164:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
164 | freopen((fileio + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:165:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
165 | freopen((fileio + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
2 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
456 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
2 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
456 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
404 KB |
Output is correct |
30 |
Correct |
26 ms |
468 KB |
Output is correct |
31 |
Correct |
33 ms |
344 KB |
Output is correct |
32 |
Correct |
54 ms |
604 KB |
Output is correct |
33 |
Correct |
57 ms |
1104 KB |
Output is correct |
34 |
Correct |
57 ms |
860 KB |
Output is correct |
35 |
Correct |
60 ms |
620 KB |
Output is correct |
36 |
Correct |
55 ms |
836 KB |
Output is correct |
37 |
Correct |
53 ms |
620 KB |
Output is correct |
38 |
Correct |
41 ms |
600 KB |
Output is correct |
39 |
Correct |
46 ms |
976 KB |
Output is correct |
40 |
Correct |
41 ms |
600 KB |
Output is correct |
41 |
Correct |
45 ms |
604 KB |
Output is correct |
42 |
Correct |
44 ms |
600 KB |
Output is correct |
43 |
Correct |
51 ms |
604 KB |
Output is correct |
44 |
Correct |
42 ms |
652 KB |
Output is correct |
45 |
Correct |
42 ms |
600 KB |
Output is correct |
46 |
Correct |
42 ms |
648 KB |
Output is correct |
47 |
Correct |
45 ms |
604 KB |
Output is correct |
48 |
Correct |
45 ms |
604 KB |
Output is correct |
49 |
Correct |
45 ms |
612 KB |
Output is correct |
50 |
Correct |
20 ms |
600 KB |
Output is correct |
51 |
Correct |
23 ms |
876 KB |
Output is correct |
52 |
Correct |
20 ms |
656 KB |
Output is correct |
53 |
Correct |
22 ms |
644 KB |
Output is correct |
54 |
Correct |
20 ms |
600 KB |
Output is correct |
55 |
Correct |
21 ms |
604 KB |
Output is correct |
56 |
Correct |
2 ms |
344 KB |
Output is correct |
57 |
Correct |
26 ms |
600 KB |
Output is correct |
58 |
Correct |
43 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 |
2 ms |
344 KB |
Output is correct |
4 |
Execution timed out |
5017 ms |
7284 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
440 KB |
Output is correct |
2 |
Correct |
1945 ms |
5368 KB |
Output is correct |
3 |
Correct |
2262 ms |
5992 KB |
Output is correct |
4 |
Correct |
2461 ms |
6224 KB |
Output is correct |
5 |
Correct |
32 ms |
8784 KB |
Output is correct |
6 |
Correct |
41 ms |
9096 KB |
Output is correct |
7 |
Correct |
55 ms |
8916 KB |
Output is correct |
8 |
Correct |
112 ms |
8908 KB |
Output is correct |
9 |
Correct |
254 ms |
8904 KB |
Output is correct |
10 |
Correct |
705 ms |
8948 KB |
Output is correct |
11 |
Correct |
1948 ms |
9216 KB |
Output is correct |
12 |
Execution timed out |
5053 ms |
8980 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Execution timed out |
5080 ms |
5900 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
2 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
456 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
404 KB |
Output is correct |
30 |
Correct |
26 ms |
468 KB |
Output is correct |
31 |
Correct |
33 ms |
344 KB |
Output is correct |
32 |
Correct |
54 ms |
604 KB |
Output is correct |
33 |
Correct |
57 ms |
1104 KB |
Output is correct |
34 |
Correct |
57 ms |
860 KB |
Output is correct |
35 |
Correct |
60 ms |
620 KB |
Output is correct |
36 |
Correct |
55 ms |
836 KB |
Output is correct |
37 |
Correct |
53 ms |
620 KB |
Output is correct |
38 |
Correct |
41 ms |
600 KB |
Output is correct |
39 |
Correct |
46 ms |
976 KB |
Output is correct |
40 |
Correct |
41 ms |
600 KB |
Output is correct |
41 |
Correct |
45 ms |
604 KB |
Output is correct |
42 |
Correct |
44 ms |
600 KB |
Output is correct |
43 |
Correct |
51 ms |
604 KB |
Output is correct |
44 |
Correct |
42 ms |
652 KB |
Output is correct |
45 |
Correct |
42 ms |
600 KB |
Output is correct |
46 |
Correct |
42 ms |
648 KB |
Output is correct |
47 |
Correct |
45 ms |
604 KB |
Output is correct |
48 |
Correct |
45 ms |
604 KB |
Output is correct |
49 |
Correct |
45 ms |
612 KB |
Output is correct |
50 |
Correct |
20 ms |
600 KB |
Output is correct |
51 |
Correct |
23 ms |
876 KB |
Output is correct |
52 |
Correct |
20 ms |
656 KB |
Output is correct |
53 |
Correct |
22 ms |
644 KB |
Output is correct |
54 |
Correct |
20 ms |
600 KB |
Output is correct |
55 |
Correct |
21 ms |
604 KB |
Output is correct |
56 |
Correct |
2 ms |
344 KB |
Output is correct |
57 |
Correct |
26 ms |
600 KB |
Output is correct |
58 |
Correct |
43 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 |
2 ms |
344 KB |
Output is correct |
62 |
Execution timed out |
5017 ms |
7284 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |