------------------------------
------------------------------
求出最小树,边权和为minTree。
预处理出最小树上任意两点路径上的最长边maxCost(u,v)
枚举每条边(u,v),ans=max( ( pop(u)+pop(v) ) / ( minTree-maxCost(u,v) ) )
------------------------------
#include
#include
#include
#include
#include
#include
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1111;
const int maxm=1111;
const double eps=1e-12;
const double OO=1e100;
int dcmp(double x,double y){
if (abs(x-y)u=u;
this->v=v;
this->w=w;
}
bool operator<(const Road& rhs) const{
return dcmp(w,rhs.w)<0;
}
};
struct Point{
double x,y;
double pop;
Point(){}
Point(double x,double y,double pop){
this->x=x;
this->y=y;
this->pop=pop;
}
double disToPot(Point& p){
return sqrt( (x-p.x)*(x-p.x)+(y-p.y)*(y-p.y) );
}
};
class DisjointSet{
private:
int pa[maxn];
int n;
public:
void makeSet(int n){
this->n=n;
for (int i=0;i<=n;i++) pa[i]=i;
}
int findSet(int x){
if (x!=pa[x]) pa[x]=findSet(pa[x]);
return pa[x];
}
void unionSet(int x,int y){
x=findSet(x);
y=findSet(y);
if (x!=y) pa[x]=y;
}
}disjointSet;
class Graph{
private:
struct EdgeNode{
int to;
double w;
int next;
};
int head[maxn],edge,n;
EdgeNode edges[maxm*2];
double maxCost[maxn][maxn];
void dfsCost(int s,int u,int pa){
for (int i=head[u];i!=-1;i=edges[i].next){
int v=edges[i].to;
double w=edges[i].w;
if (v==pa) continue;
maxCost[s][v]=maxCost[s][u];
if (maxCost[s][v] u( %d )\n",pa,u);
for (int i=head[u];i!=-1;i=edges[i].next){
int v=edges[i].to;
if (v==pa) continue;
dfsBase(v,u);
}
}
public:
void init(int n){
this->n=n;
memset(head,-1,sizeof(int)*(n+1));
edge=0;
}
void addedge(int u,int v,double w){
edges[edge].w=w,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++;
}
void findMaxCost(){
for (int i=0;iroad;
vectorcity;
int n;
double minTree;
void initializer(){
disjointSet.makeSet(n);
road.clear();
city.clear();
minTree=0;
tree.init(n);
}
void input(){
for (int i=0;i::iterator it=road.begin();it!=road.end();it++){
int u=it->u;
int v=it->v;
double w=it->w;
if (disjointSet.findSet(u)!=disjointSet.findSet(v)){
minTree+=w;
disjointSet.unionSet(u,v);
tree.addedge(u,v,w);
tree.addedge(v,u,w);
}
}
tree.findMaxCost();
//debug
//tree.showGraph();
//tree.showMaxCost();
}
void solve(){
double ans=0;
for (vector::iterator it=road.begin();it!=road.end();it++){
int u=it->u;
int v=it->v;
double A=city[u].pop+city[v].pop;
double B=minTree-tree.getMaxCost(u,v);
double res=A/B;
if (res>ans) ans=res;
}
printf("%0.2f\n",ans);
}
int main()
{
int T;
scanf("%d",&T);
while (T--){
scanf("%d",&n);
initializer();
input();
Kruskal();
solve();
}
return 0;
}