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. push到Git时常见的失败

           之前学用git的时候,不想记命令,总是gui和bash交互的用,但是发现总出现push失败的问题,用gui来fetch的时候,显示下拉成功,但事实上并没有,这时候得在bash上用命令来下 ...

  2. vue-自定义pc端键盘-动画

    <template> <div class="keyboard"> <div class="keyboard_key"> & ...

  3. css实现右侧固定宽度,左侧宽度自适应

    https://blog.csdn.net/qq_22889599/article/details/78414040 反过来也可以:左侧宽度固定,右侧自适应.不管是左是右,反正就是一边宽度固定,一边宽 ...

  4. DataFrame WordCount

    测试数据: ** * 使用DataFrame实现WordCount */ object DataFrameWordCount { def main(args: Array[String]): Unit ...

  5. [vue]vue路由篇vue-router

    spa单页开发及vue-router基础: https://www.cnblogs.com/iiiiiher/p/9034496.html url两种传参方式 query: $route.query ...

  6. emq共享订阅

    emqtt 试用(二)验证 emq 和 mosquito 的共享订阅 1. 多个订阅者都订阅以下主题形式 clientA 订阅  $queue/topic 发布主题名称为 topic1   clien ...

  7. WireShark过滤器选项

    首先说几个最常用的关键字,"eq" 和 "=="等同,可以使用 "and" 表示并且,"or"表示或者."!& ...

  8. jenkins借助winscp传本地文件到远程服务器上

    有这样的场景,我们的ftp上都是些重要的资料,所以大家基本只有可看的权限,只有部分管理人员有可读可写的权限,但是jenkins上基本使用的都是ftp的路径,这个时候就存在一些问题,某些开发需要将自己构 ...

  9. 基于jquery ajax的多文件上传进度条

    效果图 前端代码,基于jquery <!DOCTYPE html> <html> <head> <title>主页</title> < ...

  10. JAVA代码MD5加密方法

    PwdEncoder.java 接口类 package com.common.security.encoder; /** * 密码加密接口 */ public interface PwdEncoder ...