이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define fast ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define all(v) v.begin(),v.end()
#define pb push_back
#define sz size()
#define ft first
#define sd second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef unsigned long long ull;
const int N = 5e5 + 5;
const ll M = 1e8;
const ll inf = 1e18;
const ll mod = 1e9;
const double Pi = acos(-1);
ll binpow(ll x, ll ti) { ll res = 1;while (ti){if(ti & 1)res *= x;x *= x;ti >>= 1; x %= mod; res %= mod;} return res;}
ll binmul(ll x, ll ti) { ll res = 0;while (ti){if(ti & 1)res += x;x += x;ti >>= 1; x %= mod; res %= mod;} return res;}
ll nok(ll a, ll b) { return (a*b)/__gcd(abs(a),abs(b)) * (a*b > 0 ? 1 : -1); }
bool odd(ll n) { return (n % 2 == 1); }
bool even(ll n) { return (n % 2 == 0); }
ll n, s, q, e, a[N], in[N], out[N], timer;
vector <pll> g[N];
pll edge[N];
bool used[N];
void dfs(ll v) {
used[v] = 1;
in[v] = ++ timer;
for (auto [to, w] : g[v]) {
if (!used[to]) {
dfs(to);
}
}
out[v] = ++ timer;
}
bool check(ll u, ll v) {
return (in[u] > in[v] && out[u] < out[v]);
}
const void solve(/*Armashka*/) {
cin >> n >> s >> q >> e;
for (int i = 1; i < n; ++ i) {
ll w;
cin >> edge[i].ft >> edge[i].sd >> w;
ll u = edge[i].ft, v = edge[i].sd;
g[u].pb({v, w});
g[v].pb({u, w});
}
for (int i = 1; i <= s; ++ i) cin >> a[i];
if (s == n) {
dfs(e);
for (int i = 1; i <= q; ++ i) {
ll num, start;
cin >> num >> start;
ll u = edge[num].ft, v = edge[num].sd;
if (start == e) cout << "escaped\n";
else if (check(start, u) && check(start, v)) cout << "0\n";
else cout << "escaped\n";
}
return;
}
for (int i = 1; i <= q; ++ i) {
ll num, start;
cin >> num >> start;
pll bl = {edge[num].ft, edge[num].sd};
queue <ll> qq;
vector <ll> d(n + 5, inf);
qq.push(start);
d[start] = 0;
while (qq.sz) {
ll v = qq.front();
qq.pop();
for (auto [to, w] : g[v]) {
if (bl == make_pair(to, v) || bl == make_pair(v, to)) continue;
if (d[to] > d[v] + w) {
d[to] = d[v] + w;
qq.push(to);
}
}
}
if (d[e] != inf) cout << "escaped\n";
else {
ll mn = inf;
for (int i = 1; i <= s; ++ i) mn = min(mn, d[a[i]]);
if (mn == inf) cout << "oo\n";
else cout << mn << "\n";
}
}
}
signed main() {
fast;
//freopen("divide.in", "r", stdin);
//freopen("divide.out", "w", stdout);
int tt = 1;
//cin >> tt;
while (tt --) {
solve();
}
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/
# | 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... |