PAT 甲级 1034 Head of a Gang(30 分) 低时耗AC

原题地址:PAT Adv 1034

原题正文:

1034 Head of a Gang(30 分)

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

题目说明:

这道题的数据量还是很小的,主要考的图的连通分量,使用逐点遍历即可,容易想到深度优先,或者广度优先。然而,连通分量的在这里的概念和并查集存在异曲同工之妙,我在这里给出并查集的解法,达到了很高的时间效率,当然,这和题意的设定存在很大的关系,在这里我们只需要提取集合的特定信息,而非全部或路径信息。

题意给出的的人名全部为三位的大写字母,所有hash可能为26^3<2W,为我们提前建立并查集初始化后的数组提供了遍历。当然如果人名的设定再宽泛一点,比如20位内的不定长字符串,执意使用并查集求解时,就需要动态的修改高效的并查集的长度了,例如使用不定长数组vector来存储并查集结构等,map来存储字符串和vector索引映射信息。

必要的说明可以在代码注释中找到。

完整AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=26*26*26+10;
struct person{
    int fa,time;
};
struct gang{
    int cnt,headId,headTime,totalTime;  //gang-person-count, gang-head-id, gang-head-time, gang-total-time
    gang(){
        this->cnt=0, this->headTime=0, this->headId=0, this->totalTime=0;
    }
    bool operator < (const gang &a) const{
        return this->headId<a.headId;
    }
};
int encrypt(char *str){             //hashing of a string with 3 capital letters
    int sum=0;
    for(int i=0;i<3;i++)
        sum=sum*26+str[i]-'A';
    return sum;
}
void decrypt(char *str,int sum){    //de-hashing of a string with 3 capital letters
    for(int i=2;i>=0;i--){
        str[i]=sum%26+'A';
        sum/=26;
    }
    str[3]='\0';
}
person s[maxn];                     //set of the relation table
int Find(int x){
    return x==s[x].fa?x:s[x].fa=Find(s[x].fa);
}
void Union(int x,int y){
    x=Find(x),y=Find(y);
    s[x].fa=y;
}
int main(){
    int n,k,p1,p2,time;             //p1, id of person-1; p2, id of person-2
    scanf("%d%d",&n,&k);
    char str1[4],str2[4];           //string of person-1, string of person-2
    for(int i=0;i<maxn;i++)         //initialize relation table
        s[i].fa=i,s[i].time=0;
    for(int i=0;i<n;i++){
        scanf("%s %s %d",str1,str2,&time);
        p1=encrypt(str1), p2=encrypt(str2);
        s[p1].time+=time, s[p2].time+=time;
        Union(p1,p2);
    }
    map<int,gang> mp;               //mp[set-fa-id]=gang-info, including gang person-count, gang-head-id, gang-head-time, gang-total-time
    for(int i=0;i<maxn;i++){
        if(s[i].time>0){            //if this person has tele-time
            int setid=Find(i);      //find out which set it belongs
            if(mp.find(setid)==mp.end())
                mp[setid]=gang();   //if not found this set-id, insert a new node with this set-id
            auto it=mp.find(setid);
            it->second.cnt++;
            it->second.totalTime+=s[i].time;
            if(s[i].time>it->second.headTime){
                it->second.headTime=s[i].time;
                it->second.headId=i;
            }
        }
    }
    set<gang> ans;                  //sort the legal gangs according to gang-head-id, i.e. alphabetical order
    for(auto it=mp.begin();it!=mp.end();it++){
        if(it->second.cnt>2 && it->second.totalTime>>1>k){
            ans.insert(it->second);
        }
    }
    printf("%d\n",ans.size());
    for(auto it=ans.begin();it!=ans.end();it++){
        decrypt(str1,(*it).headId); //the priority of struct-member operator is higher than indexing operator
        printf("%s %d\n",str1,it->cnt);
    }
    return 0;
}

AC截图:

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值