# poj 1789

aaaaaaa

baaaaaa

abaaaaa

aabaaaa

（注：上述翻译来自discuss）

//合并
int x=find_set(left);
int y=find_set(right);
if(x!=y)p[x]=y;



//合并 x,y与上面同义 x!-y
if (rank[x]>rank[y])
p[y]=x;
else
{
if(rank[x]==rank[y])
{
rank[y]++;
p[x]=y;
}
else p[x]=y;
}

sort函数可能大家都用过，特别是懒得写快排的时候===

1、外写cmp函数

sort(test,test+N,cmp);

2、内定重载函数

bool operator (const struct_type&rhs)  const //const很重要

{

return .........;

}

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#define N 2002
#define BN 2000010
#define inf 100

using namespace std;
struct Node
{
int from,to;
int dis;
Node():from(0),to(0),dis(0){
};
bool operator < (const Node & q)const
{
return dis<q.dis;
}
}edge[BN];

int n,k,t;
char ch[N][8];
int rank[N];
int father[N];
int weigh(int a,int b)
{
int m=0;
int ans=0;
while(m<7)
{
if(ch[a][m]!=ch[b][m])ans++;
m++;
}
return ans;
}
void MakeSet()
{
for (int i=0;i<N;++i)
{
rank[i]=0;
father[i]=i;
}
}
int find(int a)
{
if(a!=father[a])
father[a]=find(father[a]);
return father[a];
}

bool Union(int x,int y)
{
int px=find(x);
int py=find(y);
if(px!=py)
{
if(rank[px]>rank[py])
father[py]=px;
else
{
if(rank[px]==rank[py])
rank[py]++;
father[px]=py;
}
return 1;
}
return 0;
}
int Kruskal()
{
int cost=0;
for (int i=0;i<t;++i)
{
int start,end;
start=edge[i].from;
end=edge[i].to;
if(Union(start,end))
cost+=edge[i].dis;
}
return cost;
}
int main()
{
while(1)
{
n=t=0;
cin>>k;
if(k==0)break;
for (int i=0;i<k;++i)
{
for (int j=0;j<7;++j)
cin>>ch[i][j];
}
for(int i=0; i<k-1;i++)
{
for(int j=i+1; j<k; j++)
{
edge[t].from=i;
edge[t].to=j;
edge[t++].dis=weigh(i,j);
}
}
MakeSet();
sort(edge,edge+t);
printf("The highest possible quality is 1/%d.\n",Kruskal());
}
return 0;
}

#### POJ1789-Truck History

2011-07-30 09:12:32

#### poj1789Truck History 最小生成树水题

2015-08-03 14:16:26

#### 【POJ】[1789]Truck History

2016-02-20 17:27:57

#### POJ 1789 解题报告

2015-08-19 01:39:41

#### POJ1789-Truck History .

2016-04-26 22:07:34

#### POJ 1789:Truck History 【Prim】

2015-08-13 09:13:10

#### HDU1789贪心

2016-02-21 10:29:54

#### noi1789 算24 解题报告

2016-06-23 13:06:12

#### POJ1007: Output Limit Exceeded的解决方法

2016-07-26 15:51:29

#### （贪心）HDU 1789 解题报告

2014-11-26 22:58:54