Head of a Gang(並查集,多連通圖)
阿新 • • 發佈:2018-12-14
題目描述
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,對於同一個團伙,尋找裡面通話時長最大的
程式碼如下:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define MAX 2002
struct person{
char name[4];
int t;
}per[MAX];
struct header{
char name[4];
int num;
}head[MAX];
bool cmp(header a,header b){
return strcmp(a.name,b.name)<0;
}
int a[MAX],sum[MAX];
int findR(int x){
return x==a[x]?x:(a[x]=findR(a[x])); //遞迴查詢集合的代表元素,含路徑壓縮
}
int main(){
int n,k,i,j,t;
char name1[4],name2[4];
while(scanf("%d %d",&n,&k)!=EOF){
for(i=0;i<MAX;i++){
a[i]=i;
sum[i]=1;//用來記錄每個連通分支裡的人員數
}
int num=0;
while(n--){
scanf("%s %s %d",name1,name2,&t);
for(i=0;i<num;i++)
if(strcmp(name1,per[i].name)==0)
break;
if(i==num){
strcpy(per[i].name,name1);
per[i].t=t;
num++;
}else
per[i].t+=t;
for(j=0;j<num;j++)
if(strcmp(name2,per[j].name)==0)
break;
if(j==num){
strcpy(per[j].name,name2);
per[j].t=t;
num++;
}else
per[j].t+=t;
int fx=findR(i);
int fy=findR(j);
if(fx!=fy){
a[fx]=fy;
sum[fy]+=sum[fx];
}
}
for(i=0;i<num;i++)
findR(i); //很重要,將同一個團伙的成員根節點置成一樣的,方便後來查詢
//尋找人數大於2的團伙,並尋找該團伙裡面通話時長最大的
int index=0;
for(i=0;i<num;i++){
int max=0,max_id=0,count=0,talk=0;
if(a[i]==i&&sum[i]>2){
for(j=0;j<num;j++){
if(a[j]==i){
if(per[j].t>max){
max=per[j].t;
max_id=j;
}
count++;
talk+=per[j].t;
}
}
// printf("talk %d\n",talk);
if(talk/2>k){
strcpy(head[index].name,per[max_id].name);
head[index].num=count;
index++;
}
}
}
printf("%d\n",index);
sort(head,head+index,cmp);
for(i=0;i<index;i++){
printf("%s %d\n",head[i].name,head[i].num);
}
}
return 0;
}