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
首先 要建一个 以string的 下标的 连接表,需要用map
map<string,vector<string> > mm;
表内直接存放 string 地址就行了,权值另外保存
map<string,int> node;
再 DFS 求出极大连通图的个数,及各各极大连通图的节点数,权值之和,权值最大的节点地址
坑点:
1、“A "Gang" is a cluster of more than 2 persons ” 所以节点数要大于2
2、因为每次通话每个人都权值都加了,其实总通话时间=权值之和/2;
1 #include <iostream> 2 3 #include <string> 4 5 #include <vector> 6 7 #include <map> 8 9 using namespace std; 10 11 12 13 struct Gang 14 15 { 16 17 int num,sum; 18 19 }; 20 21 22 23 string ss1[1001]; 24 25 string ss2[1001]; 26 27 28 29 map<string,vector<string> > mm; 30 31 map<string,int> visit; 32 33 map<string,int> node; 34 35 map<string,Gang> result; 36 37 38 39 void DFS(string s,int &sum,string &max,int &num) 40 41 { 42 43 if(node[s]>node[max]) max=s; 44 45 num++; 46 47 sum=sum+node[s]; 48 49 visit[s]=1; 50 51 52 53 for(int i=0;i<mm[s].size();i++) 54 55 { 56 57 if(visit[mm[s][i]]==0) 58 59 DFS(mm[s][i],sum,max,num); 60 61 } 62 63 64 65 } 66 67 68 69 70 71 int main() 72 73 { 74 75 76 77 int n,k,t; 78 79 string s1,s2; 80 81 while(cin>>n) 82 83 { 84 85 cin>>k; 86 87 88 89 mm.clear(); 90 91 visit.clear(); 92 93 node.clear(); 94 95 result.clear(); 96 97 98 99 100 101 102 103 int i; 104 105 106 107 for(i=0;i<n;i++) 108 109 { 110 111 cin>>s1>>s2>>t; 112 113 ss1[i]=s1; 114 115 ss2[i]=s2; 116 117 visit[s1]=0; 118 119 visit[s2]=0; 120 121 node[s1]+=t; 122 123 node[s2]+=t; 124 125 mm[s1].push_back(s2); 126 127 mm[s2].push_back(s1); 128 129 } 130 131 132 133 134 135 map<string,int>::iterator it; 136 137 int num; 138 139 int sum; 140 141 int count=0; 142 143 string max; 144 145 146 147 for(it=node.begin();it!=node.end();it++) 148 149 { 150 151 if(visit[it->first]==0) 152 153 { 154 155 156 157 sum=0; 158 159 num=0; 160 161 max=it->first; 162 163 DFS(it->first,sum,max,num); 164 165 if(sum/2>k&&num>2) 166 167 { 168 169 count++; 170 171 result[max].num=num; 172 173 result[max].sum=sum; 174 175 } 176 177 178 179 } 180 181 } 182 183 184 185 cout<<count<<endl; 186 187 map<string,Gang>::iterator it2; 188 189 for(it2=result.begin();it2!=result.end();it2++) 190 191 { 192 193 cout<<it2->first<<" "<<(it2->second).num<<endl; 194 195 } 196 197 } 198 199 return 0; 200 201 }