Head of a Gang(并查集,多连通图)

题目描述

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.
示例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
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;
}

猜你喜欢

转载自blog.csdn.net/qq_31342997/article/details/79993562