이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int,int> pi;
typedef vector<pi> pii;
typedef tuple<int,int,int> tii;
typedef vector<ll> li;
typedef vector<li> lii;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define LSOne(s) ((s)&(-s))
ll INF=1000000000000000010;
int inf=2e9+10;
ll M=1e9+7;
int n,m,q,e;
vector<vector<pair<int,ll>>> a;
pii rds;
vi sp,sa,se;
vii p;
vector<ll> tmp;
vector<vector<ll>> dist;
int sval=0,eval=0;
void dfs(int x){
sa[x]=sval++;
for(auto u:a[x]){
if(sa[u.F]!=-1)continue;
p[u.F][0]=x;
tmp[u.F]=tmp[x]+u.S;
dfs(u.F);
if(dist[u.F][0]!=INF)dist[x][0]=min(dist[x][0],dist[u.F][0]+u.S);
if(dist[u.F][0]!=INF)dist[u.F][0]-=tmp[u.F];
}
se[x]=eval++;
}
bool ances(int x,int y){
if(sa[x]>=sa[y]&&se[x]<=se[y])return 1;
return 0;
}
int main() {
cin>>n>>m>>q>>e;e--;
int k=log2(n)+1;
a.resize(n);p.resize(n,vi(k,-1));sa.resize(n,-1);se.resize(n,-1);dist.resize(n,vector<ll>(k,INF));tmp.resize(n,0);
REP(i,0,n-1){
int x,y;ll z;
cin>>x>>y>>z;
x--;y--;
rds.PB({x,y});
a[x].PB({y,z});
a[y].PB({x,z});
}
sp.resize(m);
REP(i,0,m)cin>>sp[i];
REP(i,0,m)sp[i]--;
REP(i,0,m)dist[sp[i]][0]=0;
dfs(e);
REP(j,1,k)REP(i,0,n)if(p[i][j-1]!=-1)p[i][j]=p[p[i][j-1]][j-1];
REP(j,1,k)REP(i,0,n)if(p[i][j-1]!=-1)dist[i][j]=min(dist[i][j],dist[p[i][j-1]][j-1]);
REP(i,0,n-1)if(sa[rds[i].F]<sa[rds[i].S])swap(rds[i].F,rds[i].S);
while(q--){
int x,y;cin>>x>>y;
x--;y--;
int z=y;
if(ances(y,rds[x].F)){
ll ans=dist[y][0];
for(int i=k-1;i>=0;i--)if(p[y][i]!=-1&&ances(p[y][i],rds[x].F)){
ans=min(ans,dist[y][i]);
y=p[y][i];
}
if(y!=rds[x].S)ans=min(ans,dist[y][0]);
if(ans==INF)cout<<"oo\n";
else cout<<ans+tmp[z]<<"\n";
}
else cout<<"escaped\n";
}
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |