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>
using namespace std;
#define popcount(n) (__builtin_popcountll((n)))
#define clz(n) (__builtin_clzll((n)))
#define ctz(n) (__builtin_ctzll((n)))
#define lg(n) (63 - __builtin_clzll((n)))
#define BIT(n, i) (((n) >> (i)) & 1ll)
#define MASK(i) (1ll << (i))
#define FLIP(n, i) ((n) ^ (1ll << (i)))
#define ON(n, i) ((n) | MASK(i))
#define OFF(n, i) ((n) & ~MASK(i))
#define Int __int128
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long long, int> pli;
typedef pair<int, long long> pil;
typedef vector<pair<int, int>> vii;
typedef vector<pair<long long, long long>> vll;
typedef vector<pair<long long, int>> vli;
typedef vector<pair<int, long long>> vil;
template <class T1, class T2>
bool maximize(T1 &x, T2 y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template <class T1, class T2>
bool minimize(T1 &x, T2 y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <class T>
void remove_duplicate(vector<T> &ve) {
sort (ve.begin(), ve.end());
ve.resize(unique(ve.begin(), ve.end()) - ve.begin());
}
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
template <class T> T random(T l, T r) {
return uniform_int_distribution<T>(l, r)(rng);
}
template <class T> T random(T r) {
return rng() % r;
}
const int N = 1e5 + 5, LG = 18;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const ll INF = 1e18;
const ll MAX_ANS = 1e15;
int n, s, q, root, dfs_time = 0;
int tin[N], tout[N], up[N][LG];
ll dist[N], mi[N][LG], opt[N];
vii adj[N];
pii edges[N];
bitset<N> mark;
void dfs(int u, int fa) {
tin[u] = ++dfs_time;
if (mark[u]) opt[u] = 0;
for (auto [v, w] : adj[u]) if (v != fa) {
up[v][0] = u, dist[v] = dist[u] + w;
for (int i = 1; i < LG; ++i) up[v][i] = up[up[v][i - 1]][i - 1];
dfs(v, u); minimize(opt[u], opt[v] + w);
}
tout[u] = dfs_time;
}
bool is_anc(int u, int v) {
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#ifdef hwe
freopen("input.inp", "r", stdin);
freopen("output.out", "w", stdout);
#else
#define taskname ""
if (fopen(taskname".inp", "r")) {
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
#endif
cin >> n >> s >> q >> root;
for (int i = 1; i < n; ++i) {
int u, v, w; cin >> u >> v >> w;
adj[u].emplace_back(v, w), adj[v].emplace_back(u, w);
edges[i] = make_pair(u, v);
}
for (int i = 1; i <= s; ++i) {
int u; cin >> u;
mark[u] = true;
}
memset(opt, 0x0f, sizeof opt);
memset(mi, 0x0f, sizeof mi);
// cerr << "mk\n";
dfs(root, -1);
for (int i = 1; i <= n; ++i) mi[i][0] = opt[up[i][0]] - dist[up[i][0]];
for (int i = 1; i < LG; ++i) {
for (int j = 1; j <= n; ++j) {
mi[j][i] = min(mi[j][i - 1], mi[up[j][i - 1]][i - 1]);
}
}
// for (int i = 1; i <= n; ++i) for (int j = 0; j <= lg(n); ++j) {
// cout << i << ' ' << j << ' ' << mi[i][j] << '\n';
// }
// cerr << up[6][2] << '\n';
for (int i = 1; i <= q; ++i) {
int id, r; cin >> id >> r;
auto [u, v] = edges[id];
if (up[u][0] == v) swap(u, v);
// cerr << u << ' ' << v << '\n';
if (!is_anc(v, r)) cout << "escaped\n";
else {
ll ans = opt[r], init = dist[r];
for (int j = LG - 1; j >= 0; --j) if (is_anc(v, up[r][j])) {
// cout << r << ' ' << j << ' ' << up[r][j] << ' ' << mi[r][j] << '\n';
minimize(ans, init + mi[r][j]);
r = up[r][j];
}
// cout << ans << '\n';
if (ans < MAX_ANS) cout << ans << '\n';
else cout << "oo\n";
}
}
cerr << '\n'; return 0;
}
Compilation message (stderr)
valley.cpp: In function 'int main()':
valley.cpp:98:20: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:99:20: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | freopen(taskname".out", "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... |