PAT A1034. Head of a Gang (30)
来源:互联网 发布:sql loader 350 编辑:程序博客网 时间:2024/05/18 00:08
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 59AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10Sample Output 1:
2AAA 3GGG 3Sample Input 2:
8 70AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10Sample Output 2:
0
#include <cstdio>#include <algorithm>#include <map>#include <string>#include <vector>#include <iostream>using namespace std;const int Max = 2111;int n,K;map<string,int> GetN;map<int,string> GetM;map<string,int> GangM;struct Gang{string head;int num;}GangArray[Max],G;struct People{string Name;int No;vector<int> ins;int Time;}PeopleArray[Max],P;bool Visit[Max];int Gangl[Max];void DFS(int i,int t,int m,int p,int num){if(Visit[i]==true) return ;Visit[i]=true;num++;t+=PeopleArray[i].Time;if(PeopleArray[i].Time>m){m=PeopleArray[i].Time;p=i;}for(int j=0;j<PeopleArray[i].ins.size();j++){if(Visit[PeopleArray[i].ins[j]]==false){DFS(PeopleArray[i].ins[j],t,m,p,num);}}}int main(){scanf("%d%d",&n,&K);int peo=0;for(int i=0;i<n;i++){string str1,str2;int a,b,t;cin>>str1>>str2>>t;if(GetN.find(str1)!=GetN.end());else{GetN[str1]=peo;PeopleArray[peo].Time=0;PeopleArray[peo].Name=str1;GetM[peo++]=str1;}if(GetN.find(str2)!=GetN.end());else{GetN[str2]=peo;PeopleArray[peo].Time=0;PeopleArray[peo].Name=str2;GetM[peo++]=str2;}PeopleArray[GetN[str1]].ins.push_back(GetN[str2]);PeopleArray[GetN[str2]].ins.push_back(GetN[str1]);}int BangNum=0;for(int i=0;i<peo;i++){Visit[i]=false;}int Ga=0;for(int i=0;i<n;i++){if(Visit[i]==true){int PeNum=0, phead,time=0;DFS(i,time,-1,phead,PeNum);if(PeNum>2&&time>K){GangArray[Ga].head=PeopleArray[phead].Name;GangArray[Ga++].num=phead;}}}cout<<Ga<<endl;for(int i=0;i<Ga;i++){cout<<GangArray[i].head<<" "<<GangArray[i].num<<endl;}system("pause");return 0;}
- PAT A1034. Head of a Gang (30)
- pat-a1034. Head of a Gang (30)
- PAT [A1034]-Head of a Gang
- A1034. 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)
- PAT 1034. Head of a Gang (30)
- 【PAT】1034. Head of a Gang (30)
- PAT Head of a Gang
- PAT A 1034. Head of a Gang (30)
- PAT-A-1034. Head of a Gang (30)
- PAT (Advanced) 1034. Head of a Gang (30)
- PAT (Advanced Level) Practise 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)
- [jvm][面试] jvm full gc 的触发情况以及解决办法
- Laravel composer包-webUpload制作过程
- Android 高清加载巨图方案 拒绝压缩图片
- Linux文件、目录与磁盘格式
- 算法——Longest Substring Without Repeating Characters
- PAT A1034. Head of a Gang (30)
- 元数据
- UVa 101 The Blocks Problem 【STL】【vector】【模拟】
- poj 1947 Rebuilding Roads (树形背包)
- Maven 学习(三) maven问题解答
- WKWebView js交互方法以及 内存泄漏
- 信息管理系统开发遇到的问题
- Unity5 烘焙
- Struts2 之 Action类与 jsp 页面的数据交互