Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company has its own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (each letter on each position has a very special meaning but that is unimportant for this task). At the beginning of company's history, just a single truck type was used but later other types were derived from it, then from the new types another types were derived, and so on.

Today, ACM is rich enough to pay historians to study its history. One thing historians tried to find out is so called derivation plan -- i.e. how the truck types were derived. They defined the distance of truck types as the number of positions with different letters in truck type codes. They also assumed that each truck type was derived from exactly one other truck type (except for the first truck type which was not derived from any other type). The quality of a derivation plan was then defined as 
1/Σ(to,td)d(to,td)
where the sum goes over all pairs of types in the derivation plan such that t o is the original type and t d the type derived from it and d(t o,t d) is the distance of the types. 
Since historians failed, you are to write a program to help them. Given the codes of truck types, your program should find the highest possible quality of a derivation plan. 

Input

The input consists of several test cases. Each test case begins with a line containing the number of truck types, N, 2 <= N <= 2 000. Each of the following N lines of input contains one truck type code (a string of seven lowercase letters). You may assume that the codes uniquely describe the trucks, i.e., no two of these N lines are the same. The input is terminated with zero at the place of number of truck types.

Output

For each test case, your program should output the text "The highest possible quality is 1/Q.", where 1/Q is the quality of the best derivation plan.

Sample Input

4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0

Sample Output

The highest possible quality is 1/3.

意思是给出n个长度相同的字符串,一个字符串代表一个点,每两个字符串有多少个字符不同,则不同的个数即为两点之间的距离,要求各个点都连通求quality的最大值
 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX=;
int fa[MAX];
char s[MAX][];
int n,m;
struct Edge
{
int u,v,w;
}e[MAX*MAX/];
int cmp(Edge a,Edge b)
{
return a.w<b.w;
}
void UF_set()
{
for(int i=;i<MAX;i++)
fa[i]=i;
}
int Find(int x)
{
return x==fa[x]?x:fa[x]=Find(fa[x]);
}
void Union(int a, int b)
{
int r1=Find(a);
int r2=Find(b);
if(r1!=r2)
fa[r2] = r1;
}
void get_map()
{
for(int i=;i<n;i++){
for(int j=i+;j<n;j++){
int cnt = ;
for(int k=;k<;k++)
cnt+=(s[i][k]!=s[j][k]);
e[m].u = i;
e[m].v = j;
e[m++].w = cnt;
}
}
}
int Kruskal()
{
int num=,sum=;
UF_set();
for(int i = ; i < m; i++){
int u = e[i].u;
int v = e[i].v;
if(Find(u) != Find(v)){
Union(u, v);
sum += e[i].w;
num++;
}
if(num>=n-) break;
}
return sum;
}
int main()
{
while(scanf("%d",&n)!=EOF&&n){
for(int i=;i<n;i++)
scanf("%s",s[i]);
m=;
get_map();
sort(e,e+m,cmp);
printf("The highest possible quality is 1/%d.\n",Kruskal());
}
}

POJ 1789 Truck History (Kruskal 最小生成树)的更多相关文章

  1. POJ 1789 Truck History (Kruskal最小生成树) 模板题

    Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...

  2. POJ 1789 Truck History【最小生成树简单应用】

    链接: http://poj.org/problem?id=1789 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  3. POJ 1789 Truck History (Kruskal)

    题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...

  4. POJ 1789 Truck History (最小生成树)

    Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...

  5. poj 1789 Truck History【最小生成树prime】

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21518   Accepted: 8367 De ...

  6. poj 1789 Truck History(最小生成树)

    模板题 题目:http://poj.org/problem?id=1789 题意:有n个型号,每个型号有7个字母代表其型号,每个型号之间的差异是他们字符串中对应字母不同的个数d[ta,tb]代表a,b ...

  7. POJ 1789 Truck History【最小生成树模板题Kruscal】

    题目链接:http://poj.org/problem?id=1789 大意: 不同字符串相同位置上不同字符的数目和是它们之间的差距.求衍生出全部字符串的最小差距. #include<stdio ...

  8. Kuskal/Prim POJ 1789 Truck History

    题目传送门 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Pr ...

  9. POJ 1789 -- Truck History(Prim)

     POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...

随机推荐

  1. hdu1240/poj2225 BFS广搜的再理解

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/guodongxiaren/article/details/37756347 原题地址 pid=124 ...

  2. MySQL最优配置模板( 5.6&5.7转)

    [client] user = root --用户 password = 1111aaA_ -- 密码 [mysql] prompt = [\\u@\\p][\\d]>\\_ -- cmd控制台 ...

  3. chmod a+r file:给所有用户添加读的权限

    chmod a+r *:用户自己使用此命令,柯给所有用户添加可读的权限 超级用户给其他用户设置权限:sudo chmod a+rx /home/user   使所有人可以访问,读取文件,bu no W ...

  4. linux iscsi挂载与卸载

    iscsiadm -m discovery -t sendtargets -p 192.168.4.245:3260 #扫描ISCSI Target 列出所有LUN iscsiadm -m node ...

  5. 致敬金庸:武侠版编程语言...Java像张无忌还是令狐冲?

    我就喜欢这样,等新闻消失,热点过去,人们快要遗忘的时候, 用自己的方式,想起他.让他被人想起. 短评:夫千里之远,不足以举其大:千仞之高,不足以举其深.<倚天屠龙记> 短评:这世间和张三丰 ...

  6. 虚拟地址IP

    http://blog.csdn.net/whycold/article/details/11898249

  7. iOS 聊天表情键盘

    具体思路 通过UIKeyboardWillChangeFrameNotification通知,监听键盘的改变,同时可以得到键盘的Frame和动画的持续时间, 新建键盘顶部工具条YSComposeToo ...

  8. 答案在哪里?action config/Interceptor/class/servlet

    项目已提测,这两天我们都集中精力梳理外包团队给我司研发的这个三方支付系统的代码逻辑.今天下午爱琴海会议室,开发组里一同学分享他对支付结果回调的梳理成果. 支付结果回调的整体时序是:支付渠道方处理完用户 ...

  9. LeetCode110.平衡二叉树

    一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9,20,null,null,15,7] 3 / \ 9 20 / \ 15 7 返回 true . 示例 ...

  10. jar包的读取1

    ---恢复内容开始--- 昨天在做项目插件的时候,因为会用到jar包中的一个文件来初始化程序.并且以后还是会访问这个文件,所以就想到干脆吧文件拷贝到指定目录.在拷贝的时候也费了好一会时间,这里涉及到了 ...