PAT 1034 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.

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

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
const int Max=2005;
int Pre[Max],N,K,CurNum;
map<string,int> BaseMap;
char Arr[Max][5];
int CallTime[Max],SingleCallTime[Max],FatherArr[Max];
int Vis[Max];
struct Node{
  char Name[5];
  int Size;
}node[Max];
int FindFather(int v)
{
    while(Pre[v]!=v)
        v=Pre[v];
    return v;
}
void Merge(int u,int v)
{
    int x=FindFather(u);
    int y=FindFather(v);
    if(x==y) return ;
    if(x>y)
        Pre[x]=y;
    else
        Pre[y]=x;
}
void InitData()
{
    BaseMap.clear();
    memset(CallTime,0,sizeof(CallTime));
    memset(Vis,0,sizeof(Vis));
    memset(SingleCallTime,0,sizeof(SingleCallTime));
    for(int i=1;i<Max;i++)
        Pre[i]=i;
}
/*为每个名字给定一个编号*/
int GetNum(char *c)
{
    int Num=0;
    map<string,int>::iterator it=BaseMap.find(c);
    if(it==BaseMap.end())
    {
        CurNum+=1;
        Num=CurNum;
        BaseMap.insert(make_pair(c,Num));
    }
    else
        Num=it->second;
    return Num;
}
/*记录每个人打电话的时间*/
void RecordTime(char *x,char *y,int u,int v,int val)
{
    SingleCallTime[u]+=val;
    strcpy(Arr[u],x);
    strcpy(Arr[v],y);
    CallTime[u]+=val;
    CallTime[v]+=val;
}
bool C(Node x,Node y)
{
    return strcmp(x.Name,y.Name)<0;
}
int main()
{
    scanf("%d %d",&N,&K);
    InitData();
    CurNum=0;
    char x[5],y[5];
    int val;
    for(int i=0;i<N;i++)
    {
        scanf("%s %s %d",x,y,&val);
        int u=GetNum(x);
        int v=GetNum(y);
        RecordTime(x,y,u,v,val);
        Merge(u,v);
    }
    int T=0;
    for(int i=1;i<=CurNum;i++)
    {
        Pre[i]=FindFather(i);
        if(Pre[i]==i)
        {
            T+=1;
            /*存头目的编号*/
            FatherArr[T]=i;
        }
    }
    int Num=0;
    for(int i=1;i<=T;i++)
    {
        int size=0;
        int AllTime=0;
        int MaxTime=-1;
        int Max=0;
        for(int k=1;k<=CurNum;k++)
        {
            /*找编号为k的其根节点是i的人,并统计人数*/
            if(Vis[k]==0&&(Pre[k]==FatherArr[i]))
            {
                size+=1;
                AllTime+=SingleCallTime[k];
                /*找出头目*/
                if(CallTime[k]>MaxTime)
                {
                    MaxTime=CallTime[k];
                    Max=k;
                }
                Vis[k]=1;
            }
        }
        if(size<3||AllTime<=K)
            continue;
        strcpy(node[Num].Name,Arr[Max]);
        node[Num].Size=size;
        Num+=1;
    }
    sort(node,node+Num,C);
    printf("%d",Num);
    for(int i=0;i<Num;i++)
    {
        printf("\n%s %d",node[i].Name,node[i].Size);
    }
    return 0;
}

 

猜你喜欢

转载自blog.csdn.net/ZCMU_2024/article/details/84777872