#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define all(x) (x).begin(), (x).end()
const int N = 2e5 + 100 , LOG = 21;
vector <vector <pair <int , int>>> edj;
map <pair <int , int> , int> idx;
int anc[N][LOG];
vector <int> lvl;
int n , s , Q , e ;
void dfs(int node , int par) {
anc[node][0] = par;
for(int i = 1 ; i < LOG ; i ++) {
if(anc[node][i - 1] > n) continue;
anc[node][i] = anc[anc[node][i - 1]][i - 1];
}
for(auto [w , to] : edj[node]) if(par != to) lvl[to] = lvl[node] + 1 , dfs(to , node);
}
int Kth(int node , int k) {
for(int i = 0 ; i < LOG ; i ++) {
if((k >> i) & 1) node = anc[node][i];
}
return node;
}
int LCA(int u , int v) {
if(lvl[u] < lvl[v]) swap(u , v);
u = Kth(u , lvl[u] - lvl[v]);
if(u == v) return v;
for(int i = LOG - 1 ; i >= 0 ; i --) {
if(anc[u][i] != anc[v][i]) u = anc[u][i] , v = anc[v][i];
}
return anc[u][0];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> s >> Q >> e;
edj = vector <vector <pair <int , int>>> (n + 1);
vector <tuple <int , int , int>> vec(n - 1); lvl = vector <int>(n + 1 , {});
for(int i = 0 ; i < n - 1 ; i ++) {
int u , v , w; cin >> u >> v >> w;
vec[i] = {u , v , w};
idx[{u , v}] = idx[{v , u}] = i + 1;
edj[u].pb({w , v});
edj[v].pb({w , u});
}
dfs(e , e);
vector <int> shops(s);
for(int i = 0 ; i < s ; i ++) {
cin >> shops[i];
}
while( Q-- ) {
int l , r ; cin >> l >> r; int a , b , w;
tie(a , b , w) = vec[l - 1];
if(LCA(a , r) == LCA(b , r)) cout << "escaped\n";
else cout << "0\n";
// cout << a << ' ' << b << ' ' << r << ' ' << LCA(a , r) << ' ' << LCA(b , r) << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |