您的位置:首页 > 其它

1034. Head of a Gang (30)

2014-11-15 10:07 288 查看
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 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.

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

Sample Input 2:
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

Sample Output 2:

0

这一题我用并查集做的,由于并查集不是太熟,导致有不少东西没考虑到,代码改来改去很蛋疼。。。

题目要求的是只要有任意两个人有过通话,那么这两人就有关系,如果三个或以上的人都有通话,并且这相关的所有人的通话时间大于k,那么这就是一个团伙。团伙中通话时间最多的人就是头目。

要注意的是,拿通话时间与k比的不是团伙头目的时间,而是团伙通话的总时间。

解题方法就是先把每个人看作是一个并查集,父节点为-1,然后标准的并查集合并操作,如果有两个人有通话,那么这两个人就合并。并且在合并的时候要计算每个人的通话总时间。

根节点在合并的时候保存有团伙的通话总时间和团伙人数,但是根结点不是头目!!!!因为在合并之前是不知道谁是头目的。所以最后要在每个符合条件的集合中找到头目,并且用排序将其按字典顺序排序。

代码如下,虽然能AC,不过代码写的一般。。。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define PRINT_I(x) printf("%s = %d\n",#x,x);
#define PRINT_F(x) printf("%s = %f\n",#x,x);
#define PRINT_S(x) printf("%s = %s\n",#x,x);

typedef struct
{
char name[4];
int p;
int time; //the total time of the set
int mytime; // the total time of the person
}Set;

typedef struct
{
char name1[4];
char name2[4];
int time;
}Record;

int find_elem(Set *s,int *n,char *name);
int cmp(const void *a,const void *b);

int main(int argc,char* argv[])
{
int i,j,n,k,time,index1,index2,count,num,p,max,head;
int root1,root2;
char name1[4],name2[4];
Set *set,*result;
Record *rcd;

//freopen("input","r",stdin);
scanf("%d%d",&n,&k);
set = (Set *)malloc(sizeof(Set)*n*2);
rcd = (Record *)malloc(sizeof(Record)*n);

//initial the parent of each node
for(i=0;i<2*n;i++)
set[i].p = -1;

count = 0;
for(i=0;i<n;i++)
{
scanf("%s%s%d",name1,name2,&time);
find_elem(set,&count,name1);
find_elem(set,&count,name2);
strcpy(rcd[i].name1,name1);
strcpy(rcd[i].name2,name2);
rcd[i].time = time;
}
for(i=0;i<n;i++)
{
index1 = find_elem(set,&count,rcd[i].name1);
index2 = find_elem(set,&count,rcd[i].name2);
set[index1].mytime += rcd[i].time;
set[index2].mytime += rcd[i].time;
if(set[index1].p != -1 && set[index2].p != -1)
{
root1 = index1;
root2 = index2;
while(set[root1].p > -1)
root1 = set[root1].p;
while(set[root2].p > -1)
root2 = set[root2].p;
//combine 2 set
if(root1 != root2)
{
set[root1].p += set[root2].p;
set[root1].time += set[root2].time;
set[root1].time += rcd[i].time;
set[root2].p = root1;
}
else{
p = index1;
while(set[p].p > -1)
p = set[p].p;
set[p].time += rcd[i].time;
}
continue;
}
else if(set[index1].p==-1&&set[index2].p!=-1)
{
p = index1;
index1 = index2;
index2 = p;
}
set[index2].p = index1;
p = index1;
while(set[p].p > -1)
p = set[p].p;
set[p].p -= 1;
set[p].time += rcd[i].time;
}

result = (Set *)malloc(sizeof(Set)*count);
num = 0;
for(i=0;i<count;i++)
{
if(set[
ac19
i].p<-2 && set[i].time>k)
{
max = set[i].mytime;
head = i;
for(j=0;j<count;j++)
{
p = j;
while(set[p].p>-1)
p = set[p].p;
if(p != i)
continue;
else
{
if(set[j].mytime > max)
{
max = set[j].mytime;
head = j;
}
}
}
strcpy(result[num].name,set[head].name);
result[num++].p = -set[i].p;
}
}

if( !num )
{
printf("0\n");
return 0;
}
else
printf("%d\n",num);

qsort(result,num,sizeof(Set),cmp);
for(i=0;i<num;i++)
printf("%s %d\n",result[i].name,result[i].p);

free(result);
free(rcd);
free(set);
return 0;
}

int find_elem(Set *s,int *n,char *name)
{
int i;

for(i=0;i<*n;i++)
if( !strcmp(s[i].name,name) )
return i;
strcpy(s[*n].name,name);
s[*n].time = 0;
s[*n].mytime = 0;
*n += 1;
return *n;
}

int cmp(const void *a,const void *b)
{
return strcmp(((Set *)a)->name,((Set *)b)->name);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  并查集 PAT