# PAT---甲级1034 Head of a Gang 30分 并查集使用练习题

One way that the police finds the head of a gang is to check people’s phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls made between the two persons. A “Gang” is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threshold K. In each gang, the one with maximum total weight is the head. Now given a list of phone calls, you are supposed to find the gangs and the heads.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive numbers N and K (both less than or equal to 1000), the number of phone calls and the weight threshold, respectively. Then N lines follow, each in the following format:

Name1 Name2 Time

where Name1 and Name2 are the names of people at the two ends of the call, and Time is the length of the call. A name is a string of three capital letters chosen from A-Z. A time length is a positive integer which is no more than 1000 minutes.

Output Specification:

For each test case, first print in a line the total number of gangs. Then for each gang, print in a line the name of the head and the total number of the members. It is guaranteed that the head is unique for each gang. The output must be sorted according to the alphabetical order of the names of the heads.

Sample Input 1:

8 59
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10

Sample Output 1:

2
AAA 3
GGG 3

如图，当画出了所给示例的图后，我们发现每个团体就是一个连通块，我们要做的就是遍历连通块获取相关信息。本题可以用DFS搜索图的解法做，不过在这里我主要介绍下并查集解决此类问题的解法。

为了找到每个连通块的其他信息，可以在创建好并查集之后再从并查集中分别寻找每个连通块的成员再从中找到人数和总通话量信息。不过有个更好的一步到位的方法，就是再设置两个数组ufs_num和ufs_total，分别表示成员i所在的团体的人员数量和团体总通话量，这样我们只用在处理好输入数据的同时对数组元素进行修改，之后就得到了所有信息。
要注意的是在做并查集union操作的时候逻辑有些多，如果两个点是在同一个连通块里，要更新ufs_total数组不更新ufs_num数组，然后退出，如果不在同一个连通块里，更新并查集，更新ufs_num和ufs_total

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
using namespace std;
//结构体edge保存输入信息
struct edge{
int from;
int to;
int value;
};

const int maxn=2010;            //题目说输入边的数量最多1000，最坏情况是每次都不相同那么就是2000人
int ufs[maxn],weight[maxn];     //ufs并查集数组，weight数组存储每个人的点权
int n,kk;                       //n表示输入的通话数，kk表示团体满足一个gang的阈值
int countt=0,edgecount=0,gangcount=0;   //分别表示人员总数，通话总数，满足gang团体总数
vector<edge> alledge;           //存储所有通话边的向量
int ufs_num[maxn]={1};          //表示人员i所在的团体的人员数
int ufs_total[maxn]={0};        //表示人员i所在的团体的通话总量
map<string,int> STI;            //将题目输入的人名转换成int
map<int,string> ITS;            //将代表人的int和名字的键值对

bool cmp(int a,int b)
{
string sa=ITS[a];
string sb=ITS[b];
for(int i=0;i<3;i++)
{
char ca=sa[i];
char cb=sb[i];
if(ca!=cb) return ca<cb;
}
}

//并查集查找 因为是quick-find所以直接一步查找到位
int find(int k)
{
return ufs[k];
}
//并查集合并操作，在本题里不止做合并，还有ufs_total和ufs_num数组的更新，传入的参数分别是通话记录里两个人和本次通话权值
void unionn(int a,int b,int v)
{
int ra=find(a);
int rb=find(b);
if(ra==rb)               //如果两个人在同一个连通块里，那么找到该连通块所有的人，把ufs_total加上这次边的权值
{
for(int i=0;i<maxn;i++)
{
if(ufs[i]==ra) ufs_total[i]+=v;
}
return;
}
int oldat=ufs_total[ra],oldbt=ufs_total[rb];
int oldan=ufs_num[ra],oldbn=ufs_num[rb];
if(weight[ra]>=weight[rb])
{
for(int i=0;i<maxn;i++)
{
if(ufs[i]==rb) {
ufs[i]=ra;ufs_total[i]+=oldat+v;ufs_num[i]+=oldan;
}
else if(ufs[i]==ra){
ufs_total[i]+=oldbt+v;ufs_num[i]+=oldbn;
}
}
} else if(weight[ra]<weight[rb]){
for(int i=0;i<maxn;i++)
{
if(ufs[i]==ra)
{
ufs[i]=rb;ufs_total[i]+=oldbt+v;ufs_num[i]+=oldbn;
}
else if(ufs[i]==rb)
{
ufs_total[i]+=oldat+v;ufs_num[i]+=oldan;
}

}
}
}
//在第一次读入输入数据时调用，把人名转换成int
int nametoint(string s)
{
if(STI.find(s)!=STI.end()) return STI[s];
else {
STI[s]=countt;
ITS[countt]=s;
return countt++;
}
}

int main()
{
//初始化并查集，每个人指向自己
for(int i=0;i<maxn;i++)
{
ufs[i]=i;
}
scanf("%d %d",&n,&kk);
char a[4],b[4];
string sa,sb;
int ia,ib,value;
for(int i=0;i<n;i++)  //第一次处理输入数据。至于为什么要用char数组转一次string。。因为不喜欢用cin
{
scanf("%s %s %d",a,b,&value);
sa=a;sb=b;
//把string的名字转换成int
ia=nametoint(sa);
ib=nametoint(sb);
//把通话信息整合成结构体存在向量里
edge e;edgecount++;
e.from=ia;e.to=ib;e.value=value;
alledge.push_back(e);
//更新点权
weight[ia]+=value;
weight[ib]+=value;
}

fill(ufs_num,ufs_num+maxn,1);
//第二次处理输入数据，做并查集合并
for(int i=0;i<alledge.size();i++)
{
ia=alledge[i].from;
ib=alledge[i].to;
value=alledge[i].value;
unionn(ia,ib,value);
}
int visit[maxn]={0}; //用数组简单做判断：这个团体的信息是否已经处理过
int gangsize=0;
for(int i=0;i<maxn;i++)
{
if(visit[ufs[i]]==0)
{
visit[ufs[i]]=1;
if(ufs_num[ufs[i]]>2)
{
if(ufs_total[ufs[i]]>kk)
{
}
}
}
}
sort(gangi,gangi+gangsize,cmp);//按字典序排序
printf("%d\n",gangsize);
for(int i=0;i<gangsize;i++)
{
cout<<ITS[gangi[i]]<<" "<<ufs_num[gangi[i]]<<endl;
}
}