#include <bits/stdc++.h>
using namespace std;
int par[100005][20], depth[100005], tin[100005], val[100005], c[100005], n, m, q, t;
vector<pair<int, int> > vec;
vector<int> adj[100005];
void dfs(int u, int p)
{
par[u][0]=p;
for (int i=1; i<20; i++)
par[u][i]=par[par[u][i-1]][i-1];
tin[u]=(++t);
for (int v:adj[u])
{
if (v==p)
continue;
depth[v]=depth[u]+1;
dfs(v, u);
}
}
void dfs2(int u, int p)
{
for (int v:adj[u])
{
if (v==p)
continue;
dfs2(v, u);
val[u]+=val[v];
}
}
int lca(int u, int v)
{
if (depth[u]<depth[v])
swap(u, v);
for (int i=19; i>=0; i--)
if ((depth[u]-depth[v])&(1<<i))
u=par[u][i];
if (u==v)
return u;
for (int i=19; i>=0; i--)
if (par[u][i]!=par[v][i])
u=par[u][i], v=par[v][i];
return par[u][0];
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> q;
for (int i=1; i<n; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 1);
for (int i=1; i<=m; i++)
cin >> c[i];
for (int i=1; i<=q; i++)
{
int l, r, ans=0;
cin >> l >> r;
if (l==r)
{
cout << 1 << '\n';
continue;
}
for (int j=1; j<=n; j++)
val[j]=0;
vec.clear();
for (int j=l; j<=r; j++)
vec.push_back({tin[c[j]], c[j]});
sort(vec.begin(), vec.end());
for (int j=1; j<vec.size(); j++)
{
val[vec[j-1].second]++;
val[vec[j].second]++;
// cout << "val " << vec[j-1].second << " +1\n";
// cout << "val " << vec[j].second << " +1\n";
int LCA=lca(vec[j-1].second, vec[j].second);
if (LCA!=1)
{
val[par[LCA][0]]-=2;
// cout << "val " << par[LCA][0] << " -2\n";
}
}
dfs2(1, 1);
for (int j=1; j<=n; j++)
if (val[j])
ans++;
cout << ans << '\n';
}
}
Compilation message
tourism.cpp: In function 'int main()':
tourism.cpp:75:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for (int j=1; j<vec.size(); j++)
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
4 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2672 KB |
Output is correct |
6 |
Correct |
4 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2680 KB |
Output is correct |
8 |
Correct |
3 ms |
2680 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
5 ms |
2684 KB |
Output is correct |
11 |
Correct |
5 ms |
2688 KB |
Output is correct |
12 |
Correct |
9 ms |
2644 KB |
Output is correct |
13 |
Correct |
13 ms |
2736 KB |
Output is correct |
14 |
Correct |
11 ms |
2740 KB |
Output is correct |
15 |
Correct |
5 ms |
2644 KB |
Output is correct |
16 |
Correct |
5 ms |
2684 KB |
Output is correct |
17 |
Correct |
5 ms |
2644 KB |
Output is correct |
18 |
Correct |
4 ms |
2644 KB |
Output is correct |
19 |
Correct |
5 ms |
2688 KB |
Output is correct |
20 |
Correct |
4 ms |
2644 KB |
Output is correct |
21 |
Correct |
4 ms |
2644 KB |
Output is correct |
22 |
Correct |
4 ms |
2644 KB |
Output is correct |
23 |
Correct |
4 ms |
2644 KB |
Output is correct |
24 |
Correct |
5 ms |
2684 KB |
Output is correct |
25 |
Correct |
5 ms |
2664 KB |
Output is correct |
26 |
Correct |
4 ms |
2740 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
2 ms |
2684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
4 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2672 KB |
Output is correct |
6 |
Correct |
4 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2680 KB |
Output is correct |
8 |
Correct |
3 ms |
2680 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
5 ms |
2684 KB |
Output is correct |
11 |
Correct |
5 ms |
2688 KB |
Output is correct |
12 |
Correct |
9 ms |
2644 KB |
Output is correct |
13 |
Correct |
13 ms |
2736 KB |
Output is correct |
14 |
Correct |
11 ms |
2740 KB |
Output is correct |
15 |
Correct |
5 ms |
2644 KB |
Output is correct |
16 |
Correct |
5 ms |
2684 KB |
Output is correct |
17 |
Correct |
5 ms |
2644 KB |
Output is correct |
18 |
Correct |
4 ms |
2644 KB |
Output is correct |
19 |
Correct |
5 ms |
2688 KB |
Output is correct |
20 |
Correct |
4 ms |
2644 KB |
Output is correct |
21 |
Correct |
4 ms |
2644 KB |
Output is correct |
22 |
Correct |
4 ms |
2644 KB |
Output is correct |
23 |
Correct |
4 ms |
2644 KB |
Output is correct |
24 |
Correct |
5 ms |
2684 KB |
Output is correct |
25 |
Correct |
5 ms |
2664 KB |
Output is correct |
26 |
Correct |
4 ms |
2740 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
2 ms |
2684 KB |
Output is correct |
30 |
Correct |
79 ms |
2924 KB |
Output is correct |
31 |
Correct |
115 ms |
2924 KB |
Output is correct |
32 |
Correct |
174 ms |
3000 KB |
Output is correct |
33 |
Correct |
169 ms |
3004 KB |
Output is correct |
34 |
Correct |
167 ms |
3004 KB |
Output is correct |
35 |
Correct |
439 ms |
3004 KB |
Output is correct |
36 |
Correct |
429 ms |
3032 KB |
Output is correct |
37 |
Correct |
424 ms |
3004 KB |
Output is correct |
38 |
Correct |
155 ms |
3116 KB |
Output is correct |
39 |
Correct |
154 ms |
3028 KB |
Output is correct |
40 |
Correct |
155 ms |
3128 KB |
Output is correct |
41 |
Correct |
388 ms |
3140 KB |
Output is correct |
42 |
Correct |
379 ms |
3156 KB |
Output is correct |
43 |
Correct |
385 ms |
3096 KB |
Output is correct |
44 |
Correct |
156 ms |
3028 KB |
Output is correct |
45 |
Correct |
158 ms |
3044 KB |
Output is correct |
46 |
Correct |
169 ms |
3060 KB |
Output is correct |
47 |
Correct |
389 ms |
3048 KB |
Output is correct |
48 |
Correct |
385 ms |
3028 KB |
Output is correct |
49 |
Correct |
391 ms |
3048 KB |
Output is correct |
50 |
Correct |
138 ms |
3028 KB |
Output is correct |
51 |
Correct |
152 ms |
3020 KB |
Output is correct |
52 |
Correct |
143 ms |
3008 KB |
Output is correct |
53 |
Correct |
137 ms |
3016 KB |
Output is correct |
54 |
Correct |
141 ms |
3028 KB |
Output is correct |
55 |
Correct |
142 ms |
3016 KB |
Output is correct |
56 |
Correct |
49 ms |
2740 KB |
Output is correct |
57 |
Correct |
2 ms |
2900 KB |
Output is correct |
58 |
Correct |
3 ms |
2868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2680 KB |
Output is correct |
3 |
Correct |
50 ms |
2736 KB |
Output is correct |
4 |
Execution timed out |
5040 ms |
16528 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2153 ms |
10664 KB |
Output is correct |
3 |
Correct |
2474 ms |
11524 KB |
Output is correct |
4 |
Correct |
2782 ms |
12392 KB |
Output is correct |
5 |
Correct |
88 ms |
18140 KB |
Output is correct |
6 |
Correct |
92 ms |
17752 KB |
Output is correct |
7 |
Correct |
101 ms |
17184 KB |
Output is correct |
8 |
Correct |
171 ms |
16968 KB |
Output is correct |
9 |
Correct |
336 ms |
17140 KB |
Output is correct |
10 |
Correct |
826 ms |
17032 KB |
Output is correct |
11 |
Correct |
2294 ms |
17040 KB |
Output is correct |
12 |
Execution timed out |
5039 ms |
17060 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
3 ms |
2644 KB |
Output is correct |
3 |
Correct |
53 ms |
2752 KB |
Output is correct |
4 |
Execution timed out |
5038 ms |
12300 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
4 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2672 KB |
Output is correct |
6 |
Correct |
4 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2680 KB |
Output is correct |
8 |
Correct |
3 ms |
2680 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
5 ms |
2684 KB |
Output is correct |
11 |
Correct |
5 ms |
2688 KB |
Output is correct |
12 |
Correct |
9 ms |
2644 KB |
Output is correct |
13 |
Correct |
13 ms |
2736 KB |
Output is correct |
14 |
Correct |
11 ms |
2740 KB |
Output is correct |
15 |
Correct |
5 ms |
2644 KB |
Output is correct |
16 |
Correct |
5 ms |
2684 KB |
Output is correct |
17 |
Correct |
5 ms |
2644 KB |
Output is correct |
18 |
Correct |
4 ms |
2644 KB |
Output is correct |
19 |
Correct |
5 ms |
2688 KB |
Output is correct |
20 |
Correct |
4 ms |
2644 KB |
Output is correct |
21 |
Correct |
4 ms |
2644 KB |
Output is correct |
22 |
Correct |
4 ms |
2644 KB |
Output is correct |
23 |
Correct |
4 ms |
2644 KB |
Output is correct |
24 |
Correct |
5 ms |
2684 KB |
Output is correct |
25 |
Correct |
5 ms |
2664 KB |
Output is correct |
26 |
Correct |
4 ms |
2740 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
2 ms |
2684 KB |
Output is correct |
30 |
Correct |
79 ms |
2924 KB |
Output is correct |
31 |
Correct |
115 ms |
2924 KB |
Output is correct |
32 |
Correct |
174 ms |
3000 KB |
Output is correct |
33 |
Correct |
169 ms |
3004 KB |
Output is correct |
34 |
Correct |
167 ms |
3004 KB |
Output is correct |
35 |
Correct |
439 ms |
3004 KB |
Output is correct |
36 |
Correct |
429 ms |
3032 KB |
Output is correct |
37 |
Correct |
424 ms |
3004 KB |
Output is correct |
38 |
Correct |
155 ms |
3116 KB |
Output is correct |
39 |
Correct |
154 ms |
3028 KB |
Output is correct |
40 |
Correct |
155 ms |
3128 KB |
Output is correct |
41 |
Correct |
388 ms |
3140 KB |
Output is correct |
42 |
Correct |
379 ms |
3156 KB |
Output is correct |
43 |
Correct |
385 ms |
3096 KB |
Output is correct |
44 |
Correct |
156 ms |
3028 KB |
Output is correct |
45 |
Correct |
158 ms |
3044 KB |
Output is correct |
46 |
Correct |
169 ms |
3060 KB |
Output is correct |
47 |
Correct |
389 ms |
3048 KB |
Output is correct |
48 |
Correct |
385 ms |
3028 KB |
Output is correct |
49 |
Correct |
391 ms |
3048 KB |
Output is correct |
50 |
Correct |
138 ms |
3028 KB |
Output is correct |
51 |
Correct |
152 ms |
3020 KB |
Output is correct |
52 |
Correct |
143 ms |
3008 KB |
Output is correct |
53 |
Correct |
137 ms |
3016 KB |
Output is correct |
54 |
Correct |
141 ms |
3028 KB |
Output is correct |
55 |
Correct |
142 ms |
3016 KB |
Output is correct |
56 |
Correct |
49 ms |
2740 KB |
Output is correct |
57 |
Correct |
2 ms |
2900 KB |
Output is correct |
58 |
Correct |
3 ms |
2868 KB |
Output is correct |
59 |
Correct |
2 ms |
2644 KB |
Output is correct |
60 |
Correct |
2 ms |
2680 KB |
Output is correct |
61 |
Correct |
50 ms |
2736 KB |
Output is correct |
62 |
Execution timed out |
5040 ms |
16528 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |