| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 15445 | Accepted: 5933 |
Description
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
where the sum goes over all pairs of types in the derivation plan such that to is the original type and td the type derived from it and d(to,td) 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
Output
Sample Input
4 aaaaaaa baaaaaa abaaaaa aabaaaa 0
Sample Output
The highest possible quality is 1/3.
===============================
每两个字符串之间不同字母的个数就是边权,求最小生成树。并查集实现。
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
char str[2222][10];
int n,par[2222];
struct edge
{
int u,v,w;
}edges[4000100];
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
void makeset()
{
for(int i=0;i<n;i++) par[i]=i;
}
int findset(int x)
{
if(x!=par[x]) par[x]=findset(par[x]);
return par[x];
}
void unionset(int x,int y)
{
x=findset(x);
y=findset(y);
if(x!=y) par[x]=y;
}
int main()
{
while(~scanf("%d",&n))
{
if(n==0) break;
for(int i=0;i<n;i++)
scanf("%s",str[i]);
int cnt=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
int num=0;
for(int k=0;k<7;k++)
{
if(str[i][k]!=str[j][k])
num++;
}
edges[cnt].u=i;
edges[cnt].v=j;
edges[cnt++].w=num;
}
}
sort(edges,edges+cnt,cmp);
makeset();
int sum=0;
for(int i=0;i<cnt;i++)
{
if(findset(edges[i].u)!=findset(edges[i].v))
{
unionset(edges[i].u,edges[i].v);
sum+=edges[i].w;
}
}
printf("The highest possible quality is 1/%d.\n",sum);
}
return 0;
}
1315



被折叠的 条评论
为什么被折叠?



