题目链接:https://codeforces.com/problemset/problem/1085/D
题目大意:
给你一棵树和一个数s, 要求你给这棵树分配边权(边权非负),使得这棵树的直径最小。(树的直径是树的最长路径)
解题思路:
要想最长路径最小,就必须把s均摊在所有不完全重合的路径上,这些路径的两端必然度数为1(边是双向的),否则最长路径会比平均长度要大。剩下的工作只需找出两端度数为1的节点的路径数即可。除用于连接端节点的边皆赋0值。
# include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n, s, x, y;
vector <int> G[maxn];
int main(){
std::ios::sync_with_stdio(false);
while(cin >> n >> s){
for(int i = 1; i <= n-1; ++i){
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
int cnt = 0;
for(int i = 1; i <= n; ++i)
if(G[i].size() == 1) cnt++;
cout << fixed << setprecision(7) << 2.0 * s / cnt << endl;
for(int i = 0; i <= n; ++i)
G[i].clear();
}
return 0;
}