abc
  • 板块灌水区
  • 楼主chty
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/7/22 13:38
  • 上次更新2024/7/22 13:41:00
查看原帖
abc
1162593
chty楼主2024/7/22 13:38

#include <bits/stdc++.h> using namespace std; const int N = 105; struct node{ int weight; int sub[3]; } tree[N]; bool flag[N]; int n,w,u,v; int sum, ans = INT_MAX; void dfs(int idx, int depth){ flag[idx] = true; if(depth>0) sum+=tree[idx].weight*depth; for(int i=0;i<3;i++){ if(!flag[tree[idx].sub[i]]) dfs(tree[idx].sub[i], depth + 1); } } int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>w>>u>>v; tree[i].weight=w; tree[i].sub[1]=u; tree[i].sub[2]=v; if(u!=0) tree[u].sub[0]=i; if(v!=0) tree[v].sub[0]=i; }

for(int i=1;i<=n;i++) {
    sum = 0; 
    memset(flag,0,sizeof(flag)); 
    dfs(i, 0);
    ans=min(ans,sum);
}
cout<<ans;

return 0; }``` abc

2024/7/22 13:38
加载中...