PAT 1034. Head of a Gang(30)
2018-02-14 12:49
525 查看
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 threshold, 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
时间限制
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 threshold, 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<cstdio> #include<map> #include<algorithm> #include<cstring> #include<iostream> using namespace std; const int maxn=1010;//这里写1010会有段错误,图一。改成2010即可,图二。 //邻接矩阵存储图 int G[maxn][maxn]; bool vis[maxn]; int weight[maxn]={0}; //由于给出的id是字符串,需建立stirng->int及int->string的索引 map<string,int>mp; string inttostr[maxn]; int k;//记录索引记录个数 int n,t;//总边数及时间限制定值 int cnt;//某gang的人数 int id;//某gang的头目id int tt;//某gang的总通话时间 //结果需排序,构造结果结构数组 struct Ans{ int pnum; string s; }ans[maxn]; int countans=0;//记录结果个数 void dfs(int s){ vis[s]=true; cnt++; if(weight[s]>weight[id]){ id=s; } for(int i=0;i<k;i++){ if(G[s][i]!=0){ tt+=G[s][i]; G[s][i]=G[i][s]=0; if(vis[i]==false) dfs(i); } } } void DFST(){ fill(vis,vis+maxn,false); for(int i=0;i<k;i++){ if(vis[i]==false){ cnt=0; id=i; tt=0; dfs(i); if(cnt>=3&&tt>t){ ans[countans].s=inttostr[id]; ans[countans].pnum=cnt; countans++; } } } } bool cmp(Ans a,Ans b){ return a.s<b.s; } int main(){ scanf("%d %d",&n,&t); k=0; fill(G[0],G[0]+maxn*maxn,0); for(int i=0;i<n;i++){ string s1,s2; int x; cin>>s1>>s2>>x; getchar(); if(mp.find(s1)==mp.end()){ mp[s1]=k; inttostr[k]=s1; k++; } if(mp.find(s2)==mp.end()){ mp[s2]=k; inttostr[k]=s2; k++; } G[mp[s1]][mp[s2]]+=x; G[mp[s2]][mp[s1]]+=x; weight[mp[s1]]+=x; weight[mp[s2]]+=x; } DFST(); printf("%d\n",countans); sort(ans,ans+countans,cmp); for(int i=0;i<countans;i++){ cout<<ans[i].s<<' '<<ans[i].pnum<<endl; } return 0; }
相关文章推荐
- 1034. Head of a Gang (30)-PAT甲级真题(图的遍历dfs)
- pat 1034. Head of a Gang (30)
- pat甲级1034. Head of a Gang (30)
- PAT甲级 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30) 并查集
- 【PAT甲级】1034. Head of a Gang (30)
- Python实现Pat 1034. Head of a Gang (30)
- 【PAT】【Advanced Level】1034. Head of a Gang (30)
- 【PAT】1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30) DFS,删边
- PAT (Advanced Level) 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- 1034. Head of a Gang (30) PAT甲级刷题
- pat 1034. Head of a Gang (30)
- PAT - 甲级 - 1034. Head of a Gang (30)(DFS求连通块)
- PAT 1034. Head of a Gang (30)
- PAT_A 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)-PAT甲级真题
- PAT (Advanced Level) 1034. Head of a Gang (30) 黑帮头目,并查集
- pat-a1034. Head of a Gang (30)