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<iostream>
#include<iomanip>
#include<algorithm>
#include<stack>
#include<queue>
#include<string>
#include<string.h>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<cstdio>
#include<bitset>
#include<chrono>
#include<random>
#include<ext/rope>
/* ordered_set
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
*/
#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e17;
int n, s, Q, e;
int vis[maxN], parCent[maxN], dp[maxN];
vector<int> adj[maxN];
bool is_shop[maxN];
map<pair<int, int>, int> id, dist;
int depth[maxN], lg[maxN];
int f[maxN][21];
int w[maxN], ans[maxN];
pair<int, int> edge[maxN];
struct TQuery
{
int r, id;
};
int res[maxN];
vector<TQuery> q[maxN];
void Log()
{
for(int i=2; i<maxN; i++)
lg[i] = lg[i / 2] + 1;
}
void predfs(int u, int par)
{
for(int v : adj[u])
{
if(v == par)
continue;
depth[v] = depth[u] + 1;
f[v][0] = u;
w[v] = w[u] + dist[ {u, v}];
for(int i=1; i<=lg[n]; i++)
f[v][i] = f[f[v][i - 1]][i - 1];
predfs(v, u);
}
}
int lca(int u, int v)
{
if(depth[u] < depth[v])
swap(u, v);
int k = depth[u] - depth[v];
for(int i = lg[n]; i >= 0; --i)
if(k & (1 << i))
{
u = f[u][i];
k -= 1 << i;
}
if(u == v)
return u;
for(int i = lg[n]; i >= 0; --i)
if(f[u][i] != f[v][i])
{
u = f[u][i];
v = f[v][i];
}
return f[u][0];
}
int Dist(int u, int v)
{
return w[u] + w[v] - 2 * w[lca(u, v)];
}
int dfs(int u, int par)
{
dp[u] = 1;
for(int v : adj[u])
{
if(v == par || vis[v])
continue;
dp[u] += dfs(v, u);
}
return dp[u];
}
int Find_Centroid(int u, int par, int sz)
{
for(int v : adj[u])
{
if(v == par || vis[v])
continue;
if(dp[v] > sz / 2)
return Find_Centroid(v, u, sz);
}
return u;
}
int Decompose(int u)
{
int root = Find_Centroid(u, 0, dfs(u, 0));
vis[root] = 1;
for(int v : adj[root])
{
if(vis[v])
continue;
parCent[Decompose(v)] = root;
}
return root;
}
bool Inside(int u, int v, int i)
{
int t = lca(u, v);
if(lca(u, i) == i && lca(i, t) == t)
return true;
if(lca(v, i) == i && lca(i, t) == t)
return true;
return false;
}
void update(int u)
{
for(int v=u; v; v=parCent[v])
ans[v] = min(ans[v], Dist(u, v));
}
int get(int u, int id)
{
int t1 = edge[id].fi, t2 = edge[id].se;
int res = oo;
for(int v=u; v; v=parCent[v])
{
if(!Inside(u, v, t1) || !Inside(u, v, t2))
res = min(res, ans[v] + Dist(u, v));
}
return res;
}
void DFS(int u, int par)
{
if(is_shop[u])
update(u);
int pos = id[ {u, par}];
for(int v : adj[u])
{
if(v == par)
continue;
DFS(v, u);
}
for(TQuery a : q[pos])
{
if(!Inside(a.r, e, u) || !Inside(a.r, e, par))
{
// cout << a.r << " " << e << '\n';
res[a.id] = -1;
continue;
}
res[a.id] = get(a.r, pos);
}
}
void ReadInput()
{
cin >> n >> s >> Q >> e;
for(int i=1; i<n; i++)
{
int u, v, w;
cin >> u >> v >> w;
edge[i] = {u, v};
adj[u].pb(v);
adj[v].pb(u);
dist[ {u, v}] = dist[ {v, u}] = w;
id[ {u, v}] = id[ {v, u}] = i;
}
for(int i=1; i<=s; i++)
{
int x;
cin >> x;
is_shop[x] = 1;
}
for(int i=1; i<=Q; i++)
{
int id, r;
cin >> id >> r;
q[id].pb({r, i});
}
}
void Solve()
{
predfs(Decompose(1), 0);
memset(ans, 37, sizeof ans);
DFS(1, 0);
// cout << e;return;
for(int i=1; i<=Q; i++)
{
if(res[i] == -1)
cout << "escaped\n";
else if(res[i] >= oo)
cout << "oo\n";
else
cout << res[i] << '\n';
}
}
int32_t main()
{
//freopen("x.inp", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
Log();
ReadInput();
Solve();
}
# | 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... |