이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll INF = 1e18;
const int MAXN = 1e5+7;
const int LOG = 18;
struct str{
ll fi,sc,th;
};
int pre[MAXN];
int post[MAXN];
int depth[MAXN];
int degree[MAXN];
int ojciec[MAXN];
ll dis[MAXN];
ll suma[MAXN];
bool visited[MAXN];
str jp[MAXN][LOG];
pair<int,int> in[MAXN];
vector<pair<int,ll> > g[MAXN];
queue<int> que;
int w,diff,cnt=0;
void DFS(int x,int f,int d){
pre[x]=++cnt;
depth[x]=depth[f]+1;
suma[x]=suma[f]+d;
jp[x][0]={f,dis[f],f};
for(int i=1;i<LOG;i++){
jp[x][i].fi=jp[jp[x][i-1].fi][i-1].fi;
if(jp[jp[x][i-1].fi][i-1].sc>jp[x][i-1].sc){
jp[x][i].sc=jp[x][i-1].sc;
jp[x][i].th=jp[x][i-1].th;
}
else{
jp[x][i].sc=jp[jp[x][i-1].fi][i-1].sc;
jp[x][i].th=jp[jp[x][i-1].fi][i-1].th;
}
}
for(auto sas:g[x]){
if(f==sas.first) continue;
DFS(sas.first, x, sas.second);
}
post[x]=++cnt;
}
bool anc(int a,int b){
return (pre[a] <= pre[b] && post[a] >= post[b]);
}
void DFS1(int x,int f){
visited[x]=1;
ojciec[x]=f;
for(int i=0;i<g[x].size();i++){
if(!visited[g[x][i].first]){
DFS1(g[x][i].first, x);
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll n,q,s,r,sk,a,b,x,y,b1,mini=INF,c;
cin>>n>>s>>q>>r;
for(int i=1;i<n;i++){
cin>>a>>b>>c;
in[i]={a,b};
g[a].push_back({b,c});
g[b].push_back({a,c});
degree[a]++;
degree[b]++;
}
for(int i=1;i<=n;i++){
if(degree[i]==1) que.push(i);
dis[i]=INF;
}
for(int i=1;i<=s;i++){
cin>>sk;
dis[sk]=0;
}
for(int i=1;i<=n;i++) visited[i]=0;
DFS1(r,0);
while(!que.empty()){
x=que.front();
que.pop();
dis[ojciec[x]]=min(dis[ojciec[x]], dis[x]+1);
}
DFS(r,r,0);
while(q--){
cin>>a>>b;
x=in[a].first;
y=in[a].second;
if(depth[x]>depth[y]) swap(x,y);
//cout<<y<<' '<<b<<'\n';
if(anc(y,b)==false){
cout<<"escaped\n";
continue;
}
b1=b;
diff=depth[y]-depth[b];
mini=dis[b];
w=b;
for(int i=LOG-1;i>=0;i--){
if(depth[jp[b][i].fi]<depth[y]) continue;
if(jp[b][i].sc<mini){
mini=jp[b][i].sc;
w=jp[b][i].th;
}
b=jp[b][i].fi;
}
if(mini==INF) cout<<"oo\n";
else cout<<suma[b1]-suma[w]+mini<<'\n';
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
valley.cpp: In function 'void DFS1(int, int)':
valley.cpp:51:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int i=0;i<g[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... |