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>
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define ll long long
#define ii pair<int,int>
#define fi first
#define se second
#define all(v) v.begin(),v.end()
#define RRH(v) v.resize(unique(all(v)) - v.begin())
using namespace std;
const int N = 1e6+7;
const int M = 1e9+7;
const ll oo = 3e18;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long GetRandom(long long l, long long r) {
return uniform_int_distribution<long long> (l, r)(rng);
}
int n, s, q, e, timedfs = 0, tin[N], tout[N], h[N];
vector <ii> g[N];
ll d[N];
bool isAnc(int u, int v) {
return (tin[u] >= tin[v] && tin[u] <= tout[v]);
}
int sz[N], head[N], depth[N], big[N], par[N];
void DFS(int u) {
sz[u] = 1;
big[u] = 0;
for (auto [v, w]: g[u]) {
if (v == par[u]) continue;
par[v] = u;
d[v] = d[u] + w;
depth[v] = depth[u] + 1;
DFS(v);
if (sz[v] > sz[big[u]]) big[u] = v;
sz[u] += sz[v];
}
}
void HLD(int u) {
tin[u] = ++timedfs;
if (big[u]) {
head[big[u]] = head[u];
h[big[u]] = h[u];
HLD(big[u]);
}
for (auto [v, w]: g[u]) {
if (v == par[u] || v == big[u]) continue;
h[v] = h[u] + 1;
head[v] = v;
HLD(v);
}
tout[u] = timedfs;
}
int spe[N];
bool spec[N];
ll dist[N];
void dfs(int u) {
dist[u] = oo;
if (spec[u]) {
dist[u] = d[u];
}
for (auto [v, w]: g[u]) {
if (v == par[u]) continue;
dfs(v);
dist[u] = min(dist[u], dist[v]);
}
}
pair <int, int> ed[N];
ll ma[20][N];
ll get(int l, int r) {
int k = log2(r - l + 1);
return min(ma[k][l], ma[k][r - (1 << k) + 1]);
}
ll query(int u, int v) {
ll ans = oo;
for (;head[u] != head[v]; u = par[head[u]]) {
if (h[u] < h[v]) swap(u, v);
ans = min(ans, get(tin[head[u]], tin[u]));
}
if (tin[u] > tin[v]) swap(u, v);
ans = min(ans, get(tin[u], tin[v]));
return ans;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
#define TASK ""
if (fopen (".inp", "r")) {
freopen (".inp", "r", stdin);
freopen (".out", "w", stdout);
}
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
cin >> n >> s >> q >> e;
For (i, 1, n - 1) {
int u, v, w; cin >> u >> v >> w;
ed[i] = {u, v};
g[u].push_back({v, w});
g[v].push_back({u, w});
}
depth[e] = 0;
d[e] = 0;
DFS(e);
h[e] = 1;
head[e] = e;
HLD(e);
for (int i = 1; i <= s; i++) {
cin >> spe[i];
spec[spe[i]] = 1;
}
dfs(e);
for (int i = 1; i < n; i++) {
if (tin[ed[i].fi] > tin[ed[i].se]) swap(ed[i].fi, ed[i].se);
}
for (int i = 1; i <= n; i++) {
ma[0][tin[i]] = dist[i] - 2ll * d[i];
}
for (int j = 1; (1 << j) <= n; j++) {
for (int i = 1; i <= n - (1 << j) + 1; i++) {
ma[j][i] = min(ma[j - 1][i], ma[j - 1][i + (1 << j - 1)]);
}
}
while (q--) {
int id, r; cin >> id >> r;
int v = ed[id].se;
if (!isAnc(r, v)) {
cout << "escaped\n";
continue;
}
if (dist[v] == oo) {
cout << "oo\n";
continue;
}
cout << d[r] + query(r, v) << '\n';
}
cerr << "Time elapsed: " << TIME << " s.\n";
return 0;
}
Compilation message (stderr)
valley.cpp: In function 'void DFS(int)':
valley.cpp:36:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
36 | for (auto [v, w]: g[u]) {
| ^
valley.cpp: In function 'void HLD(int)':
valley.cpp:55:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
55 | for (auto [v, w]: g[u]) {
| ^
valley.cpp: In function 'void dfs(int)':
valley.cpp:72:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
72 | for (auto [v, w]: g[u]) {
| ^
valley.cpp: In function 'int main()':
valley.cpp:142:64: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
142 | ma[j][i] = min(ma[j - 1][i], ma[j - 1][i + (1 << j - 1)]);
| ~~^~~
valley.cpp:104:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen (".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~
valley.cpp:105:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen (".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
valley.cpp:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:109:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | freopen(TASK".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... |