一个点连几条边就是度为几?所以度为1就是树最外边的那个点..还有题目并不要考虑删完后的情况.先插个图
题目就是要让4 5 6 8 9这些点道不了1.让你删除一些边,使得删除的边权最小.
那么怎么做呢..这个题目还是很简单的..设立dp[i]为底面的点都到不了i点的最小代价.
那么dp[i]= min(vul[i][son[i]],dp[son[i]]);要么选联通它的子节点断开,要么继续往下搜..找更优的断开方法.
#include<bits/stdc++.h> #define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define pb push_back #define inf 132913423039 typedef long long ll; const ll mod=1e9+7; const ll N=1e5+5; const ll M=10+5; const double eps=1e-7; using namespace std; ll gcd(ll a,ll b) {return b==0?a:gcd(b,a%b);} ll lcm(ll a,ll b) { return a*b/gcd(a,b); } ll qp(ll a,ll b, ll p){ll ans = 1;while(b){if(b&1){ans = (ans*a)%p;--b;}a = (a*a)%p;b >>= 1;}return ans%p;} ll Inv(ll x) { return qp(x,mod-2,mod);} ll C(ll n,ll m){if (m>n) return 0;ll ans = 1;for (int i = 1; i <= m; ++i) ans=ans*Inv(i)%mod*(n-i+1)%mod;return ans%mod;} ll A(ll n,ll m,ll mod){ll sum=1; for(int i=n;i>=n-m+1;i--) sum=(sum*i)%mod; return sum%mod;} ll lowbit(ll x) {return x&(-x);} ll c[N],sum1[N],sum2[N],a[N],lsh[N],n,m,s; void add1(ll i,ll k){ while(i<=n) {c[i]+=k;i+=lowbit(i);}}//预处理ai单点修改 区间查询***预处理a[i]-a[i-1]区间修改单点查询 ll Sum1(ll i) {ll res=0; while(i>0) res+=c[i],i-=lowbit(i);return res;}//预处理ai单点修改 区间查询***预处理a[i]-a[i-1]区间修改单点查询 void add2(ll i,ll k){ ll x=i;while(i<=n) {sum1[i]+=k;sum2[i]+=k*(x-1);i+=lowbit(i);}}//区间修改,区间查询 ll Sum2(ll i) {ll res=0,x=i;while(i>0){ res+= x * sum1[i]-sum2[i]; i -= lowbit(i);}return res;}//区间修改,区间查询 void ls(){ll cnt; for(ll i=1;i<=n;i++) lsh[i]=a[i]; sort(lsh+1,lsh+n+1);cnt = unique(lsh+1,lsh+n+1)-lsh-1; for(int i=1;i<=n;i++)a[i]=lower_bound(lsh+1,lsh+cnt+1,a[i])-lsh;} ll dp[N]; struct dd { ll son,val;//存边权和子节点 }; vector<dd>v[N]; ll dfs(ll x,ll fa) { if(v[x].size()==1&&x!=s) return inf;//递归出口,假如这个点没有子节点那么return inf for(ll i=0;i<v[x].size();i++) { ll so=v[x][i].son; if(so==fa) continue; ll va=v[x][i].val; dp[x]+=min(va,dfs(so,x)); } return dp[x]; } signed main() { ios; ll x,y,z; cin>>n>>m>>s;//n个点m条边,s为根 for(ll i=1;i<=m;i++) { cin>>x>>y>>z; v[x].pb(dd{y,z}); v[y].pb(dd{x,z}); } dfs(s,0); cout<<dp[s]<<endl; return 0; }
注意是个无向图就好了