PAT (Advanced Level) Practise 1034 Head of a Gang (30)
2017-07-22 10:41
507 查看
1034. Head of a Gang (30)
时间限制100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
题意:给你n对人的通话时间,gang 的定义是一群人,至少有 3 个人,这群人中每个人之间都通过通话相连,且整个群体的通话时长超过一个阈值。整个 gang 的团体中,拥有的电话时长最长的人就是头目,输出一共有几个gang,每个gang的头目和每个gang有多少人
解题思路:并查集
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <cmath> #include <map> #include <cmath> #include <set> #include <stack> #include <queue> #include <vector> #include <bitset> #include <functional> using namespace std; #define LL long long const int INF = 0x3f3f3f3f; map<string,int>mp; int n,m,f[10009],sum[10009],sum1[10009],vis[10009]; char s1[10009][30],s2[10009][30],s3[10009][30]; struct node { char ch[30]; int sum; friend bool operator<(node a,node b) { return strcmp(a.ch,b.ch)<0; } }ans[10009]; int Find(int x) { return f[x]==x?x:f[x]=Find(f[x]); } int main() { while(~scanf("%d%d",&n,&m)) { int cnt=1,w,tot=0; for(int i=1;i<=10000;i++) f[i]=i; mp.clear(); memset(sum,0,sizeof sum); memset(sum1,0,sizeof sum1); for(int i=1;i<=n;i++) { scanf("%s%s%d",s1[i],s2[i],&w); if(!mp[s1[i]]) mp[s1[i]]=cnt,strcpy(s3[cnt],s1[i]),cnt++; if(!mp[s2[i]]) mp[s2[i]]=cnt,strcpy(s3[cnt],s2[i]),cnt++; int x=mp[s1[i]],y=mp[s2[i]]; sum[x]+=w,sum[y]+=w; } for(int i=1;i<=n;i++) { int x=mp[s1[i]],y=mp[s2[i]]; int xx=Find(x),yy=Find(y); if(xx!=yy&&sum[xx]>sum[yy]) f[yy]=xx; else if(xx!=yy) f[xx]=yy; } memset(vis,0,sizeof vis); for(int i=1;i<cnt;i++) { vis[Find(i)]++; sum1[Find(i)]+=sum[i]; } for(int i=1;i<cnt;i++) { if(Find(i)==i&&vis[i]>2&&sum1[i]/2>m) strcpy(ans[tot].ch,s3[i]),ans[tot++].sum=vis[i]; } printf("%d\n",tot); sort(ans,ans+tot); for(int i=0;i<tot;i++) printf("%s %d\n",ans[i].ch,ans[i].sum); } return 0; }
相关文章推荐
- PAT (Advanced Level) Practise 1034 Head of a Gang (30)
- Pat(Advanced Level)Practice--1034(Head of a Gang)
- 1034. Head of a Gang (30)【离散化+并查集+搜索】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1022. Digital Library (30)
- PAT (Advanced Level) Practise 1103 Integer Factorization (30)
- 1103. Integer Factorization (30)【搜索+剪枝】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1123 Is It a Complete AVL Tree (30)
- 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise
- 1072. Gas Station (30)【最短路dijkstra】——PAT (Advanced Level) Practise
- 1049. Counting Ones (30)【计算1 的个数】——PAT (Advanced Level) Practise
- 1057. Stack (30)【栈+树状数组】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1119 Pre- and Post-order Traversals (30)
- PAT (Advanced Level) Practise 1131 Subway Map (30)
- PAT (Advanced Level) Practise 1111. Online Map (30) Dijstra单源最短路
- PAT (Advanced Level) Practise 1127 ZigZagging on a Tree (30)
- PAT (Advanced Level) Practise 1014. Waiting in Line (30)
- PAT (Advanced Level) Practise - 1004. Counting Leaves (30)
- PAT (Advanced Level) Practise 1064 Complete Binary Search Tree (30)
- PAT (Advanced Level) Practise 1072 Gas Station (30)
- 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise