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 <map> 3 #include <iostream> 4 #include <string> 5 using namespace std; 6 #define MAX 2010 7 //#define INF 0x3fffffff 8 int G[MAX][MAX]={ 9 0 10 }; 11 bool visited[MAX]={ 12 false 13 }; 14 int weight[MAX]={ 15 0 16 }; 17 map<string,int>str2num,gang; 18 map<int,string>num2str; 19 int numstr=0; 20 int totalvalue,number,head; 21 int N,K; 22 int Change(string str) 23 { 24 if(str2num.find(str)!=str2num.end()) 25 return str2num[str]; 26 else 27 { 28 num2str[numstr]=str; 29 str2num[str]=numstr; 30 return numstr++; 31 } 32 } 33 void DFS(int index) 34 { 35 visited[index]=true; 36 number++; 37 if(weight[index]>weight[head]) 38 { 39 head=index; 40 } 41 for(int i=0;i<numstr;i++) 42 { 43 if(G[index][i]>0) 44 { 45 totalvalue+=G[index][i]; 46 G[index][i]=G[i][index]=0; 47 if(!visited[i]) 48 DFS(i); 49 } 50 } 51 } 52 void DFSTraverse() 53 { 54 for(int i=0;i<numstr;i++) 55 { 56 if(!visited[i]) 57 { 58 totalvalue=0; 59 number=0; 60 head=i; 61 DFS(i); 62 if(number>2&&totalvalue>K) 63 { 64 gang[num2str[head]]=number; 65 } 66 } 67 } 68 } 69 int main() 70 { 71 72 cin>>N>>K; 73 for(int i=0;i<N;i++) 74 { 75 string str1,str2; 76 int length; 77 int id1,id2; 78 cin>>str1>>str2>>length; 79 id1=Change(str1); 80 id2=Change(str2); 81 G[id1][id2]+=length;//注意。。。。 82 G[id2][id1]+=length; 83 weight[id1]+=length; 84 weight[id2]+=length; 85 } 86 DFSTraverse(); 87 cout<<gang.size()<<endl; 88 map<string,int>::iterator it; 89 for(it=gang.begin();it!=gang.end();it++) 90 cout<<it->first<<" "<<it->second<<endl; 91 92 }