题目描述:
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 threthold 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.
输入:
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 threthold, 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.
输出:
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.
样例输入:
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
8 70
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10
样例输出:
2
AAA 3
GGG 3
0
解题思路:
这个题花了较长时间,综合性还是比较强(其实想明白了感觉还是比较简单~),大体思路是这样:
1,首先利用并查集找到所有的连通分支数,这里为了方便,建议直接用当前节点作为根节点,而不采用-1同一作为所有节点的根节点,前者在后面遍历寻找同一团伙里面通话时间最长的头目时有优势。
2,其次,对于同一个团伙,寻找里面通话时长最大的,注意,这里每个连通分支的头儿并不一定是通话时间最长的。然后这里面的K是指每个团伙通话时间总长,而保存的却是团伙里面每个成员的通话时长,所以每个团伙通话时间总长=每个成员的通话时长之和/2。
# include<stdio.h>
# include<string.h>
# include<algorithm>
using namespace std;
# define MAXSIZE 2000
struct person
{
char name[3];
int time;
}per[MAXSIZE];
struct header
{
char name[3];
int members;
bool operator < ( const header &b) const
{
int tmp=strcmp(name,b.name);
return tmp<0;//姓名升序排列
}
}head[MAXSIZE/2];
int tree[MAXSIZE];
int sum[MAXSIZE];
int findroot(int x)
{
if(tree[x]!=x)//这是其中一种并查集的思路,直接用当前的节点作为根节点,之前是用-1作为所有节点的根节点,后者在寻找
// 同一类的cluster时,不如前者方便。
tree[x]=findroot(tree[x]);
return tree[x];
}
int main()
{
int N,K,i,j;
while(scanf("%d%d",&N,&K)!=EOF)
{
char name1[3];
char name2[3];
int time0;
int num=0;
//初始化
for(i=0;i<MAXSIZE;i++)
{
//tree[i]=-1;
tree[i]=i;//采用当前节点作为初始化为根节点
sum[i]=1;//用来记录每个连通分支里面的集合数
}
while(N--!=0)
{
scanf("%s%s%d",name1,name2,&time0);
//读入第一组数据
for(i=0;i<num;i++)
{
if(strcmp(name1,per[i].name)==0)
break;
}
if(i==num)//更新名字信息
{
//per[i].name=name1;
strcpy(per[i].name,name1);
per[i].time=time0;
num++;
}
else
per[i].time+=time0;
//读入第二组数据
for(j=0;j<num;j++)
{
if(strcmp(name2,per[j].name)==0)
break;
}
if(j==num)
{
strcpy(per[j].name,name2);
per[j].time=time0;
num++;
}
else
per[j].time+=time0;
//寻找连通子集
int a=findroot(i);
int b=findroot(j);
if(a!=b)
{
tree[a]=b;
sum[b]+=sum[a];
}
}
for(i=0;i<num;i++)
{
findroot(i);//这一步很重要,将同一个团伙的成员的根节点置成一样的,方便后来的查询。
}
int index=0;
int ans=0;//总的团伙个数
int max=0;//最大的团伙头目通话的时间
int max_ind=0;//最大的团伙头目对应的索引值
int count_memer=0;//每个团伙里面的人数
int sum_talk=0;//每个团伙通话的总时间
for(i=0;i<num;i++)
{
if(tree[i]==i&&sum[i]>2)
{
for(j=0;j<num;j++)
{
if(tree[j]==i)//findroot(i)
{
if(per[j].time>max)
{
max=per[j].time;
max_ind=j;
}
count_memer++;
sum_talk+=per[j].time;
}
}
if(sum_talk/2>K)//这里之前算重了,每个人说话数目之和应该是 单次通话数目之和的两倍
{
// printf("%d\n\n",sum_talk);
strcpy(head[index].name,per[max_ind].name);
head[index].members=count_memer;
index++;
ans++;
}
max=0;
max_ind=0;
count_memer=0;
sum_talk=0;
}
}
sort(head,head+index);
printf("%d\n",ans);
for(i=0;i<index;i++)
{
printf("%s %d\n",head[i].name,head[i].members);
}
}
return 0;
}