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 fo(i,d,c) for(int i=d;i<=c;i++)
#define fod(i,c,d) for(int i=c;i>=d;i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout<<"\n";
#define int long long
#define inf 1000000000
#define pii pair<int,int>
#define vii vector<pii>
#define lb(x) x&-x
#define bit(i,j) ((i>>j)&1)
#define offbit(i,j) (i^(1<<j))
#define onbit(i,j) (i|(1<<j))
#define vi vector<int>
template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;}
using namespace std;
int par[maxn][20],minn[maxn][20];
int magic[maxn];
vii ke[maxn];
int n;
int d[maxn];
pii edge[maxn];
int S,E,Q;
bool shop[maxn];
int h[maxn];
void calcmagic(int u,int parent)
{
if(shop[u]) magic[u] = d[u];
for(auto [v,w] : ke[u])
{
if(v == parent) continue;
par[v][0] = u;
h[v] = h[u] + 1;
d[v] = d[u] + w;
calcmagic(v,u);
magic[u] = min(magic[u],magic[v]);
}
}
int lca(int u, int v)
{
if (h[u] < h[v])
swap(u, v);
int del = h[u] - h[v];
fod(i, 19, 0) if (bit(del, i)) u = par[u][i];
if (u == v)
return u;
fod(i, 19, 0) if (par[u][i] != par[v][i]) u = par[u][i], v = par[v][i];
return par[u][0];
}
main()
{
#define name "TASK"
if(fopen(name".inp","r"))
{
freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);
}
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> n >> S >> Q >> E;
fo(i,1,n - 1)
{
int u,v,w;
cin >> u >> v >> w;
ke[u].pb(v,w);
ke[v].pb(u,w);
edge[i] = {u,v};
}
fo(i,1,S)
{
int x;
cin >> x;
shop[x] = 1;
}
fo(i,1,n) magic[i] = 1e18;
calcmagic(E,E);
// fo(i,1,n) cout << magic[i] << ' ';en;
fo(i,1,n) if(magic[i] < 1e18) magic[i] = magic[i] - 2 * d[i];
// fo(i,1,n) cout << magic[i] << ' ';en;
fo(j,1,19) fo(i,1,n) par[i][j] = par[par[i][j - 1]][j - 1];
fo(i,1,n) minn[i][0] = magic[par[i][0]];
fo(j,1,19) fo(i,1,n) minn[i][j] = min(minn[i][j - 1],minn[par[i][j - 1]][j - 1]);
// cout << lca()
while(Q--)
{
int id,r;
cin >> id >> r;
if(lca(edge[id].se,r) != edge[id].se or lca(edge[id].fi,r) != edge[id].fi) cout << "escaped \n";
else
{
int val = d[r];
int ans = 1e18;
int del = h[r] - max(h[edge[id].fi],h[edge[id].se]);
ans = min(ans,magic[r]);
fod(i,19,0) if(bit(del,i)) ans = min(ans,minn[r][i]),r = par[r][i];
if(ans == 1e18) cout << "oo";
else cout << val + ans;
en;
}
}
}
Compilation message (stderr)
valley.cpp:55:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
55 | main()
| ^~~~
valley.cpp: In function 'int main()':
valley.cpp:60:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen(name".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
valley.cpp:61:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen(name".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... |