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
提交代码
1 #include<cstdio> 2 #include<stack> 3 #include<algorithm> 4 #include<iostream> 5 #include<stack> 6 #include<set> 7 #include<map> 8 #include<vector> 9 using namespace std; 10 map<string,vector<string> > edge; 11 map<string,int> pv; 12 map<string,bool> vis; 13 map<string,int> output; 14 void DFS(string s,string &maxper,int &pernum,int &total){ 15 vis[s]=true; 16 pernum++; 17 total+=pv[s]; 18 if(pv[s]>pv[maxper]){ 19 maxper=s; 20 } 21 vector<string>::iterator it; 22 for(it=edge[s].begin();it!=edge[s].end();it++){ 23 if(!vis[*it]){ 24 DFS(*it,maxper,pernum,total); 25 } 26 } 27 } 28 int main(){ 29 //freopen("D:\\INPUT.txt","r",stdin); 30 int n,k; 31 scanf("%d %d",&n,&k); 32 int i,v; 33 string name1,name2; 34 for(i=0;i<n;i++){ 35 cin>>name1>>name2; 36 scanf("%d",&v); 37 edge[name1].push_back(name2); 38 pv[name1]+=v; 39 vis[name1]=false; 40 edge[name2].push_back(name1); 41 pv[name2]+=v; 42 vis[name2]=false; 43 } 44 map<string,vector<string> >::iterator it; 45 int count=0; 46 string maxper; 47 int pernum,total; 48 for(it=edge.begin();it!=edge.end();it++){ 49 if(!vis[it->first]){ 50 maxper=it->first; 51 pernum=0; 52 total=0; 53 DFS(it->first,maxper,pernum,total); 54 if(pernum>2&&total*1.0/2>k){ 55 output[maxper]=pernum; 56 count++; 57 } 58 } 59 } 60 map<string,int>::iterator itt; 61 if(count){ 62 printf("%d\n",count); 63 for(itt=output.begin();itt!=output.end();itt++){ 64 cout<<itt->first; 65 printf(" %d\n",itt->second); 66 } 67 } 68 else{ 69 printf("0\n"); 70 } 71 return 0; 72 }