This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pbds tree<pair<ll, ll>, null_type, less<pair<ll,ll>>,rb_tree_tag, tree_order_statistics_node_update>
using namespace __gnu_pbds;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (PGCD)
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);} // least common multiple (PPCM)
int dx[8] = {1, 0, 0, -1, 1, 1, -1, -1};
int dy[8] = {0, 1, -1, 0, 1, -1, -1, 1};
#define endl "\n"
#define ss second
#define ff first
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int,int>>
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define vdd vector<pdd>
#define speed ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
////////////////////Only Clear Code//////////////////////////
void usaco_problem(){
freopen("milkvisits.in", "r", stdin);
freopen("milkvisits.out", "w", stdout);
}
void init(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
}
const int mx = 2e5+5;
const int LOG = 25;
const ll inf = 1e18;
const ll mod = 1e8;
vi edges[mx];
bool shop[mx];
int n, s, q, e;
vector<vi> graph[mx];
int tin[mx], tout[mx];
ll res[mx];
int c = 0;
void solve(int node, int p){
tin[node] = c++;
tout[node] = 1;
for(vi adj : graph[node]){
if(adj[0] == p)continue;
solve(adj[0], node);
tout[node] += tout[adj[0]];
}
}
ll ans;
void dfs(int node, int p, ll w, int no){
if(shop[node]){
if(ans == -1){
ans = w;
}
else{
ans = min(ans, w);
}
}
for(vi adj : graph[node]){
if(adj[0] == p || adj[2] == no)continue;
dfs(adj[0], node, w + adj[1], no);
}
}
bool compare(vi &a, vi &b){
return (a[1] <= b[1]);
}
void run_case(){
cin >> n >> s >> q >> e;
for(int i = 1; i < n;i++){
int a, b, c;cin >> a >> b >> c;
edges[i] = {a, b, c};
graph[a].pb({b, c, i});
graph[b].pb({a, c, i});
}
for(int i = 0; i < s;i++){
int a;cin >> a;
shop[a] = 1;
}
solve(1, 1);
for(int i = 1; i <= n;i++){
tout[i] += tin[i]-1;
sort(all(graph[i]), compare);
}
for(int i = 0; i < q;i++){
int ind, node;cin >> ind >> node;
int a = edges[ind][0], b = edges[ind][1];
if(tin[a] > tin[b])swap(a, b);
bool in_e = tin[e] >= tin[b] && tout[e] <= tout[b];
bool in_node = tin[node] >= tin[b] && tout[node] <= tout[b];
if(in_e == in_node){
cout << "escaped" << endl;
continue;
}
if(((ll)n*q) <= 1e6){
ans = -1;
dfs(node, node, 0, ind);
if(ans == -1){
cout << "oo" << endl;
}
else{
cout << ans << endl;
}
}
else{
cout << 0 << endl;
}
}
}
int main(){
speed;
int t;
//cin >> t;
t = 1;
while(t--){
run_case();
}
}
/*
NEVER GIVE UP!
DOING SMTHNG IS BETTER THAN DOING NTHNG!!!
Your Guide when stuck:
- Continue keyword only after reading the whole input
- Don't use memset with testcases
- Check for corner cases(n=1, n=0)
- Check where you declare n(Be careful of declaring it globally and in main)
*/
Compilation message (stderr)
valley.cpp: In function 'void usaco_problem()':
valley.cpp:32:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen("milkvisits.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen("milkvisits.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp: In function 'void init()':
valley.cpp:39:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:41:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |