This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* In the name of God */
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <bitset>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <list>
#include <map>
#include <numeric>
#include <limits>
#include <limits.h>
#include <unordered_map>
#include <unordered_set>
#include <chrono>
#include <random>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef map<int, int> MPII;
typedef vector<int> VI;
typedef vector<ll> VL;
#define PB push_back
#define POP pop_back
#define MP make_pair
#define all(a) (a).begin(), (a).end()
#define sep ' '
#define endl '\n'
#define dbg(x) cerr << '[' << #x << ": " << x << "]\n"
#define dbg2(x, y) cerr << '[' << #x << ": " << x << ", " << #y << ": " << y << "]\n"
#define Yes cout << "Yes\n"
#define YES cout << "YES\n"
#define No cout << "No\n"
#define NO cout << "NO\n"
const ll INF = (ll)2e18 + 1386;
const ld eps = 0.000000000000001;
const int MOD = 1e9 + 7;
inline int mod_add(int a, int b){ int res = a + b; return (res >= MOD? res - MOD : res); }
inline int mod_neg(int a, int b){ int res = (abs(a - b) < MOD? a - b : (a - b) % MOD); return (res < 0? res + MOD : res); }
inline int mod_mlt(int a, int b){ return (1ll * a * b % MOD); }
inline string intToString(ll a){ char x[100]; sprintf(x, "%lld", a); string s = x; return s; }
inline ll stringToInt(string s){ ll res; char x[100]; strcpy(x, s.c_str()); sscanf(x, "%lld", &res); return res; }
inline void fileIO(string i, string o){ freopen(i.c_str(), "r", stdin); freopen(o.c_str(), "w", stdout); }
const int MAXN = 2e5 + 5, LOG = 20;
int n, s, q, E, h[MAXN], par[MAXN][LOG], st[MAXN], et[MAXN], timer;
vector<PII> N[MAXN];
vector<pair<PII, int>> edges;
ll dp[MAXN], w[MAXN][LOG], ansfor[MAXN][LOG];
bitset<MAXN> mark;
void dfs_init(int v, int height = 1){
mark[v] = 1;
h[v] = height++;
st[v] = timer++;
for (auto [u, _e] : N[v]){
if (!mark[u]){
par[u][0] = v;
w[u][0] = _e;
for (int i = 1; i < LOG; i++){
par[u][i] = par[par[u][i - 1]][i - 1];
w[u][i] = w[u][i - 1] + w[par[u][i - 1]][i - 1];
}
dfs_init(u, height);
dp[v] = min(dp[v], (dp[u] == INF ? INF : dp[u] + _e));
}
}
et[v] = timer++;
}
void dfs_ans(int v){
mark[v] = 1;
for (auto [u, _e] : N[v]){
if (!mark[u]){
ansfor[u][0] = dp[u];
for (int i = 1; i < LOG; i++){
ansfor[u][i] = min(ansfor[u][i - 1], w[u][i - 1] + ansfor[par[u][i - 1]][i - 1]);
}
dfs_ans(u);
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s >> q >> E;
edges.PB(MP(MP(23, 23), 23));
for (int i = 1; i < n; i++){
int u, v, x;
cin >> u >> v >> x;
N[u].PB({v, x}), N[v].PB({u, x});
edges.PB(MP(MP(u, v), x));
}
fill(dp, dp + MAXN, INF);
while (s--){
int v; cin >> v;
dp[v] = 0;
}
for (int i = 0; i < LOG; i++) par[E][i] = E;
dfs_init(E);
for (int i = 0; i < LOG; i++) ansfor[E][i] = dp[E];
mark.reset(); dfs_ans(E);
while (q--){
int cur, ind;
cin >> ind >> cur;
int u = edges[ind].first.first, v = edges[ind].first.second;
if (h[u] > h[v]) swap(u, v);
if (st[v] <= st[cur] && et[v] >= et[cur]){
ll ans = INF, path = 0;
int jump = h[cur] - h[u];
for (int i = 0; i < LOG; i++){
if (jump >> i & 1){
ans = min(ans, (ansfor[cur][i] == INF ? INF : path + ansfor[cur][i]));
path += w[cur][i];
cur = par[cur][i];
}
}
if (ans == INF) cout << "oo" << endl;
else cout << ans << endl;
} else {
cout << "escaped" << endl;
}
}
return 0;
}
# | 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... |