One of 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 A and B are related. The weight of a relation is defined to being the total time length of all the phone calls made between the. A "Gang" is a cluster of the more than 2 persons The WHO be related to the other and total relation weight being greater than a Given threshold K. In each gang, the one and maximum total weight are the head. Now given a list of phones calls, you is supposed to find the gangs and the heads.
Input Specification:
Each input file contains the one test case. The first line contains positive numbers N and K (both less than or equal to), the number of the 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 "the" the "Call" and "time is the length of the". A name is a string of three capital letters chosen from A-Z. A time length is a positive integer which are no more than minutes.
Output Specification:
For each test case, first print with a line the total number of gangs. Then for each gang, print in a line the name of the "the" and the "total" number of the members. It is guaranteed, the head is a unique for each gang. The output must is 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 10
Sample Output 1:
2AAA 3GGG 3
Sample Input 2:
8 70AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10
Sample Output 2:
0
Ideas: Attention to the small traps in the problem, the weight of the edge is not a one-time input completed, there may be cumulative, another point to note is the possible existence of the ring, the existence of the ring in the case of how to add all the edges of the ring to meet this situation need to pay attention.
1#include <cstdio>2#include <map>3#include <iostream>4#include <string>5 using namespacestd;6 #defineMAX 20107 //#define INF 0X3FFFFFFF8 intg[max][max]={9 0Ten }; One BOOLvisited[max]={ A false - }; - intweight[max]={ the 0 - }; -map<string,int>Str2num,gang; -map<int,string>Num2str; + intNumstr=0; - intTotalvalue,number,head; + intn,k; A intChange (stringstr) at { - if(Str2num.find (str)! =str2num.end ()) - returnStr2num[str]; - Else - { -num2str[numstr]=str; instr2num[str]=Numstr; - returnnumstr++; to } + } - voidDFS (intindex) the { *visited[index]=true; $number++;Panax Notoginseng if(weight[index]>Weight[head]) - { theHead=index; + } A for(intI=0; i<numstr;i++) the { + if(g[index][i]>0) - { $totalvalue+=G[index][i]; $g[index][i]=g[i][index]=0; - if(!Visited[i]) - DFS (i); the } - }Wuyi } the voidDfstraverse () - { Wu for(intI=0; i<numstr;i++) - { About if(!Visited[i]) $ { -Totalvalue=0; -Number=0; -Head=i; A DFS (i); + if(number>2&&totalvalue>K) the { -gang[num2str[head]]=Number ; $ } the } the } the } the intMain () - { in theCin>>n>>K; the for(intI=0; i<n;i++) About { the stringstr1,str2; the intlength; the intId1,id2; +Cin>>str1>>str2>>length; -id1=Change (STR1); theId2=Change (STR2);BayiG[id1][id2]+=length;//pay attention to .... theg[id2][id1]+=length; theweight[id1]+=length; -weight[id2]+=length; - } the dfstraverse (); theCout<<gang.size () <<Endl; themap<string,int>:: iterator it; the for(It=gang.begin (); It!=gang.end (); it++) -cout<<it->first<<" "<<it->second<<Endl; the the}
View Code
PAT1034. Head of a Gang