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:
1 2 3 4 5 6 7 8 9 |
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:
1 2 3 |
2 AAA 3 GGG 3 |
Sample Input 2:
1 2 3 4 5 6 7 8 9 |
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:
1 |
用 disjoint set 来确定 Gang ,在 union 的过程中更新数据,同时在 find 的过程中确定 Head 是谁。偷懒直接用一个数组储存了姓名信息……
注意一下,输入共有 N 组数据,但是,最多可以有 2*N 的人。一开始犯二了,没想到……测试点 3 就段错误了……
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct info_ { int related; int calling_time; int head; int total; } info; int set_find(info *set,int index) { int root,temp=index,max=index; while(set[temp].related>-1) { temp=set[temp].related; if(set[max].calling_time<set[temp].calling_time) { max=temp; } } root=temp; temp=index; while(set[temp].related>-1) { index=temp; temp=set[temp].related; set[index].related=root; } if(set[set[root].head].calling_time<set[max].calling_time) { set[root].head=max; } return root; } int set_union(info *set,int a,int b,int calling_time) { set[a].calling_time+=calling_time; set[b].calling_time+=calling_time; a=set_find(set,a); b=set_find(set,b); if(a==b) { set[a].total+=calling_time; return 0; } if(set[a].related<set[b].related) { set[a].related+=set[b].related; set[a].total+=set[b].total; set[a].total+=calling_time; if(set[set[a].head].calling_time<set[set[b].head].calling_time) { set[a].head=set[b].head; } set[b].related=a; } else { set[b].related+=set[a].related; set[b].total+=set[a].total; set[b].total+=calling_time; if(set[set[b].head].calling_time<set[set[a].head].calling_time) { set[b].head=set[a].head; } set[a].related=b; } return 1; } int find_name(char *name,char (*name_list)[4],int *n) { int i; for(i=0;i<*n;i++) { if(!strcmp(name_list[i],name)) { break; } } if(i==*n) { strcpy(name_list[i],name); (*n)++; } return i; } info *set; char name[2000][4]; int compare(const void *a,const void *b) { return strcmp(name[set[*(int *)a].head],name[set[*(int *)b].head]); } int main() { int n,k,i,calling_time,index[2],*result,offset=0; char buf[8]; scanf("%d%d",&n,&k); set=(info *)malloc(n*2*sizeof(info)); result=(int *)malloc(n*2*sizeof(int)); for(i=0;i<n;i++) { set[i].related=-1; set[i].calling_time=0; set[i].total=0; set[i].head=i; } for(i=0;i<n;i++) { scanf("%s%s%d",buf,buf+4,&calling_time); set_union(set,find_name(buf,name,&offset),find_name(buf+4,name,&offset),calling_time); } offset=0; for(i=0;i<n;i++) { if(set[i].related<-2&&set[i].total>k) { result[offset++]=i; } } qsort(result,offset,sizeof(int),compare); printf("%d\n",offset); for(i=0;i<offset;i++) { printf("%s %d\n",name[set[result[i]].head],-set[result[i]].related); } } |