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 INF 10000000000000000
using namespace std;
typedef long long ll;
ll dist[200005],rmq[200005][20],din[200005][20];
bool viz[200005];
int niv[200005];
vector <pair <int,int>> v[200005];
void dfs(int x,int tata)
{
if (viz[x]==1)
{
din[x][0]=dist[x];
}
else
{
din[x][0]=INF;
}
rmq[x][0]=tata;
niv[x]=niv[tata]+1;
for (int i=0;i<v[x].size();i++)
{
if (v[x][i].first==tata)
{
continue;
}
int nod = v[x][i].first;
dist[nod]=dist[x]+v[x][i].second;
dfs(nod,x);
din[x][0]=min(din[x][0],din[nod][0]);
}
}
struct wow
{
int x,y,cost;
}muchie[200005];
int s,q,e,i,x,n,lg;
int lca (int x,int y)
{
if (niv[x]<niv[y])
{
return lca(y,x);
}
if (x==y)
{
return x;
}
int dif=niv[x]-niv[y],p=1;
for (int i=0;i<=lg;i++)
{
if ((dif&p))
{
x=rmq[x][i];
}
p=p*2;
}
if (x!=y)
{
for (int i=lg;i>=0;i--)
{
if (rmq[x][i]!=rmq[y][i])
{
x=rmq[x][i];
y=rmq[y][i];
}
}
x=rmq[x][0];
}
return x;
}
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(0);
#ifdef HOME
ifstream cin("date.in");
ofstream cout("date.out");
#endif // HOME
cin>>n>>s>>q>>e;
for (i=1;i<=n-1;i++)
{
int x,y,cost;
cin>>muchie[i].x>>muchie[i].y>>muchie[i].cost;
x=muchie[i].x;
y=muchie[i].y;
cost=muchie[i].cost;
v[x].push_back({y,cost});
v[y].push_back({x,cost});
}
for (i=1;i<=s;i++)
{
cin>>x;
viz[x]=1;
}
dfs(e,0);
lg=log2(n);
for (int j=1;j<=n;j++)
{
if (din[j][0]==INF)
{
continue;
}
din[j][0]=din[j][0]-2*dist[j];
}
for (i=1;i<=lg;i++)
{
for (int j=1;j<=n;j++)
{
din[j][i]=min(din[j][i-1],din[rmq[j][i-1]][i-1]);
rmq[j][i]=rmq[rmq[j][i-1]][i-1];
}
}
for (i=1;i<=q;i++)
{
int poz,x,y,ind;
cin>>ind>>poz;
x=muchie[ind].x;
y=muchie[ind].y;
if (niv[x]<niv[y])
{
swap(x,y);
}
if (lca(poz,x)!=x)
{
cout<<"escaped"<<'\n';
continue;
}
ll minim=din[poz][0];
int dif=niv[poz]-niv[x]+1;
int p=1,initialito=poz;
for (int j=0;j<=lg;j++)
{
if ((dif&p))
{
minim=min(minim,din[poz][j]);
poz=rmq[poz][j];
}
p=p*2;
}
if (minim==INF)
{
cout<<"oo"<<'\n';
continue;
}
cout<<minim+dist[initialito]<<'\n';
}
return 0;
}
Compilation message (stderr)
valley.cpp: In function 'void dfs(int, int)':
valley.cpp:21:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i=0;i<v[x].size();i++)
| ~^~~~~~~~~~~~
# | 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... |