#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxv=2000+5;
const int maxe=2*1e6;
char str[maxv][maxv];
struct Edge{
int from,to,weight;
};
bool operator<(const Edge &E1,const Edge &E2)
{
return E1.weight<E2.weight;
}
Edge edges[maxe];
int parent[maxv];
int find(int x)
{
return x==parent[x]?x:parent[x]=find(parent[x]);
}
int main()
{
int n;
while(scanf("%d",&n)==1&&n)
{
for(int i=1;i<=n;i++)
{
scanf("%s",&str[i]);
parent[i]=i;
}
int cnt=0;
for(int i=1;i<n;i++)
for(int j=i+1;j<=n;j++)
{
int dis=0;
for(int k=0;k<7;k++)
if(str[i][k]!=str[j][k]) dis++;
edges[cnt].from=i;
edges[cnt].to=j;
edges[cnt].weight=dis;
cnt++;
}
sort(edges,edges+cnt);
int sum=0;
for(int i=0;i<cnt;i++)
{
int p1=find(edges[i].from);
int p2=find(edges[i].to);
if(p1==p2) continue;
parent[p1]=p2;
sum+=edges[i].weight;
}
printf("The highest possible quality is 1/%d.\n",sum);
}
return 0;
}
poj 1789 最小生成树
最新推荐文章于 2023-02-20 09:09:49 发布