#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("Ofast,unroll-loops")
using namespace __gnu_pbds;
using namespace std;
const int N = 1e5 * 4;
#define pb push_back
vector<int> g[N];
int huy1[N],huy2[N];
void solve(){
int n,m,q;
cin >> n >> m >> q;
for(int i = 0;i < n - 1;i++){
int a,b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
for(int i = 1;i <= m;i++){
int a;
cin >> a;
huy1[i] = a;
huy2[a] = i;
}
if(n <= 2000){
while(q--){
int l,r;
cin >> l >> r;
vector<int> dis(n + 1,1e18);
queue<pair<int,int>> q;
q.push({huy1[l],-1});
vector<int> pr(n + 1);
while(!q.empty()){
auto [v,p] = q.front();q.pop();
for(auto u : g[v]){
if(u != p && dis[u] > dis[v] + 1){
dis[u] = dis[v] + 1;
pr[u] = v;
q.push({u,v});
}
}
}
vector<int> huy3(n + 1);
for(int i = l + 1;i <= r;i++){
int huy4 = huy1[i];
while(huy4 != huy1[l]){
huy3[huy4] = 1;
huy4 = pr[huy4];
}
}
int cnt = 0;
for(int i = 1;i <= n;i++){
cnt += huy3[i];
}
cout << cnt + 1 << "\n";
}
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t;cin >> t;
while(t--){
solve();
}
}
Compilation message
tourism.cpp: In function 'void solve()':
tourism.cpp:39:26: warning: overflow in conversion from 'double' to 'std::vector<int>::value_type' {aka 'int'} changes value from '1.0e+18' to '2147483647' [-Woverflow]
39 | vector<int> dis(n + 1,1e18);
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5064 ms |
12636 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5064 ms |
12636 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5065 ms |
12636 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5038 ms |
12636 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5058 ms |
12636 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5064 ms |
12636 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |