PAT甲级1034. Head of a Gang (30)
2017-02-28 08:27
435 查看
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:
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
并查集的应用。
只要两个人之间有通话,就属于一个帮派,通过Union函数把他们连起来。
每个人的名字是3字母的字符串,可以把它当作一个26进制数,通过转为十进制数,得到字符串和每个人的编号的映射。最开始我是用map<string,int>来做的,发现会有段错误。
还需注意:
2个人不成帮;
如果同一个帮派内有通话时间相同的,取名字排前面的人。
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
并查集的应用。
只要两个人之间有通话,就属于一个帮派,通过Union函数把他们连起来。
每个人的名字是3字母的字符串,可以把它当作一个26进制数,通过转为十进制数,得到字符串和每个人的编号的映射。最开始我是用map<string,int>来做的,发现会有段错误。
还需注意:
2个人不成帮;
如果同一个帮派内有通话时间相同的,取名字排前面的人。
#include <cstdio> #include <vector> #include <algorithm> using namespace std; const int MAX=26*26*26+2; int parent[MAX]; int weight[MAX]={0};//每个人的通话时间 int Find(int x){ if(parent[x]<0) return x; return x=Find(parent[x]); } void Union(int x,int y,int px,int py){ if(-parent[px]>-parent[py]) { parent[px]+=parent[py]; parent[py]=px; } else{ parent[py]+=parent[px]; parent[px]=py; } } struct gang{ int memberNum; //黑帮人数 int head;//boss的编号 gang(int x,int y):memberNum(x),head(y){} }; bool compare(gang a,gang b){ return a.head<b.head; } int main(){ int N,K; scanf("%d %d",&N,&K); /*初始化并查集*/ for(int i=0;i<MAX;i++) parent[i]=-1; for(int i=0;i<N;i++){ char name1[4],name2[4]; int duration; scanf("%s %s %d",name1,name2,&duration); int n1=26*26*(name1[0]-'A') + 26*(name1[1]-'A') + (name1[2]-'A'); int n2=26*26*(name2[0]-'A') + 26*(name2[1]-'A') + (name2[2]-'A'); weight[n1]+=duration; weight[n2]+=duration; int p1=Find(n1),p2=Find(n2); if(p1!=p2) Union(n1,n2,p1,p2); } vector<int> G; //储存黑帮集合root的编号 for(int i=0;i<MAX;i++){ if(parent[i]<-2) G.push_back(i); //黑帮人数必须大于2 } vector<gang> ans; /*找到每个黑帮的boss的编号M */ for(int i=0;i<G.size();i++){ int total=weight[G[i]]; int M=G[i]; for(int j=0;j<MAX;j++){ if(parent[j]==G[i]) { total+=weight[j]; if(weight[j]>weight[M]) M=j; //更新为通话时间更长的人 else if(weight[j]==weight[M]) { //通话时间相同,更新为名字字典序在前面的人 if(j<M) M=j; } } } if(total>2*K) ans.push_back(gang(-parent[G[i]],M));//帮派的总通话时间需要大于threhold } sort(ans.begin(),ans.end(),compare); printf("%d\n",ans.size()); for(int i=0;i<ans.size();i++){ int t=ans[i].head; char c1='A'+t/26/26,c2='A'+t/26%26,c3='A'+t%26; printf("%c%c%c %d\n",c1,c2,c3,ans[i].memberNum); } return 0; }
相关文章推荐
- pat甲级1034. Head of a Gang (30)
- PAT - 甲级 - 1034. Head of a Gang (30)(DFS求连通块)
- 1034. Head of a Gang (30)-PAT甲级真题
- 1034. Head of a Gang (30) PAT甲级刷题
- PAT 甲级 1034. Head of a Gang (30)
- pat 甲级 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)-PAT甲级真题(图的遍历dfs)
- 【PAT甲级】1034. Head of a Gang (30)
- PAT甲级 1034. Head of a Gang (30)
- pat-a1034. Head of a Gang (30)
- PAT (Advanced Level) 1034. Head of a Gang (30) 黑帮头目,并查集
- PAT 1034. Head of a Gang (30)
- PAT_A 1034. Head of a Gang (30)
- PAT-A-1034. Head of a Gang (30)
- 浙大2012上机 PAT1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- PAT (Advanced Level)1034. Head of a Gang (30) map的灵活运用
- PAT 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)【离散化+并查集+搜索】——PAT (Advanced Level) Practise
- PAT 1034. Head of a Gang (30)(孤岛以及一个奇怪的cost的计算问题)