`//
// Created by lenovo on 2023/1/7.
//

include<bits/stdc++.h>

using namespace std;

define ll long long

define M 300005

int n,s,t,w[M],v[M];//w是到s是源点,还是t是源点,v是边是1或2或0
int idx,h[M],vet[2M],len[M2],nxt[M*2];
ll d[M],ans;
void add(int a ,int b ,int c){
nxt[++idx]=h[a],vet[idx]=b,len[idx]=c;
h[a]=idx;
}
void dfs(int x,int pre,int tp){
for (int i = h[x]; i ; i=nxt[i]) {
int y=vet[i],z=len[i];
if(ypre)continue;
if(d[y]>d[x]+z){
d[y]=d[x]+z;
w[y]=tp;
dfs(y,x,tp);
}
}
}
void dfs1(int x,int pre,int tp){
for (int i = h[x]; i ; i=nxt[i]) {
int y=vet[i];
if(y
pre)continue;
if(w[y]==tp){
ans+=d[y];
if(i&1)
v[i/2]=1;
else
v[i/2]=2;
dfs1(y,x,tp);
}
}
}

int main(){
cin>>n>>s>>t;
idx=1;
for (int i = 1; i <=n-1 ; ++i) {
int a,b,c;
cin>>a>>b>>c;
add(a,b,c);
add(b,a,c);
}
for (int i = 1; i <=n ; ++i) {
d[i]=1e16;
}
d[s]=0;
dfs(s,0,s);
d[t]=0;
w[t]=0;
dfs(t,0,t);
dfs1(s,0,s);
dfs1(t,0,t);
cout<<ans<<'\n';
for (int i = 1; i <=n-1 ; ++i) {
cout<<v[i];
}
}`

posted on 2023-01-08 12:41  IR101  阅读(2)  评论(0编辑  收藏  举报