Skip to content

Instantly share code, notes, and snippets.

@degurii

degurii/.cpp Secret

Created October 14, 2019 19:58
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
#define ft first
#define sd second
int n, s1, s2;
struct Edge{
int to;
int cost;
Edge(int t=0, int c=0):to(t), cost(c){}
};
vector<vector<Edge>> p;
bool chk[110'000];
pii zero = {0, 0};
pii dfs(int now, pii ret){
if(chk[now]) return zero;
chk[now] = true;
if(now == s2) return ret;
pii tmp;
for(auto e: p[now]){
int next = e.to;
int ncost = e.cost;
if((tmp = dfs(next, {ret.ft + ncost, max(ret.sd, ncost)})).ft != 0)
return tmp;
}
return zero;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>s1>>s2;
p.resize(n+1);
for(int i=0; i<n-1; i++){
int u, v, c;
cin>>u>>v>>c;
p[u].emplace_back(v, c);
p[v].emplace_back(u, c);
}
pii ans = dfs(s1, zero);
cout<<ans.ft - ans.sd;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment