F - Truck History

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 2010
using namespace std;
int fa[MAXN];
int n,ans,tot;
char s[MAXN][8];
struct nond{
    int x,y,z;
}v[MAXN*MAXN];
int cmp(nond a,nond b){
    return a.z<b.z;
}
int find(int x){
    if(fa[x]==x)    return x;
    else return fa[x]=find(fa[x]);
}
int main(){
    while(scanf("%d",&n)&&n){
        for(int i=1;i<=n;i++)    cin>>s[i];
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++){
                int sum=0;
                for(int k=0;k<7;k++)
                    if(s[i][k]!=s[j][k])    sum++;
                v[++tot].x=i;v[tot].y=j;v[tot].z=sum;
            }
        sort(v+1,v+1+tot,cmp);
        for(int i=1;i<=n;i++)    fa[i]=i;
        for(int i=1;i<=tot;i++){
            int dx=find(v[i].x);
            int dy=find(v[i].y);
            if(dx==dy)    continue;
            fa[dy]=dx;
            ans+=v[i].z;
        }
        printf("The highest possible quality is 1/%d.\n",ans);
        ans=0;tot=0;
    }
}

 

 
posted @ 2018-02-23 11:34  一蓑烟雨任生平  阅读(190)  评论(0编辑  收藏  举报