1 条题解
-
3赵青海 (huhe) LV 7 SU @ 2021-8-7 18:41:43
C++ :
#include <iostream> #include <algorithm> using namespace std; const int N = 1010; struct Node { int father, size, sum; double avg; }nodes[N]; int n, root; int find() //找权值最大的根节点 { double avg = 0; int res = -1; for(int i = 1; i <=n; i++) if(i != root && avg < nodes[i].avg) { avg = nodes[i].avg; res = i; } return res; } int main() { int res = 0; cin >> n >> root; for(int i = 1; i <= n; i++) { auto &nd = nodes[i]; cin >> nd.sum; nd.size = 1; nd.avg = nd.sum; res += nd.sum; //初始赋值 每个数乘一加一块,所有数的和 } for(int i = 0, a, b; i < n - 1;i++) { cin >> a >> b; nodes[b].father = a; } for(int i = 0; i < n - 1; i++) { int ver = find(); int f = nodes[ver].father; res += nodes[ver].sum * nodes[f].size; nodes[ver].avg = -1; for(int j = 1; j <= n; j++) if(nodes[j].father == ver) nodes[j].father = f; nodes[f].sum += nodes[ver].sum; nodes[f].size += nodes[ver].size; nodes[f].avg = (double)nodes[f].sum / nodes[f].size; } cout << res << endl; return 0; }
- 1
信息
- ID
- 26
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 1
- 标签
- 递交数
- 84
- 已通过
- 70
- 上传者