PAT 1034 Head of a Gang (30)
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 threshold, 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
题目大意: 找出图的连通分量, 这个分量的定点数大于2且点权大于一个阈值
注意点: N不是总的人数 而是通话数, 应该把数组的大小开为N的两倍, 才会保证不出现段错误
1 #include<iostream> 2 #include<map> 3 #include<vector> 4 #include<algorithm> 5 #include<string> 6 using namespace std; 7 map<int, string> int2Str; 8 map<string, int> str2Int; 9 vector<vector<int> > adj(2010); 10 vector<int> vis(2010, false); 11 vector<int> v(2010, 0); 12 struct Node{ 13 int id, num; 14 }; 15 //需要注意的是数组的大小应该是N的两倍 16 bool cmp(Node& a, Node& b){ 17 return int2Str[a.id] < int2Str[b.id]; 18 } 19 void dfs(int node, int &head, int &gangNum, int &gangTime){ 20 if(v[node]>v[head]) head = node; 21 vis[node] = true; 22 gangNum++; 23 gangTime += v[node]; 24 for(int i=0; i<adj[node].size(); i++) 25 if(!vis[adj[node][i]]) dfs(adj[node][i], head, gangNum, gangTime); 26 } 27 28 int main(){ 29 int n, i, k; 30 scanf("%d %d", &n, &k); 31 int idx=0; 32 for(i=0; i<n; i++){ 33 string a, b; 34 int time, idx1, idx2; 35 cin>>a>>b>>time; 36 //给每个人编号 37 if(str2Int.find(a)==str2Int.end()){ 38 str2Int[a] = idx; 39 int2Str[idx] = a; 40 idx++; 41 } 42 if(str2Int.find(b)==str2Int.end()){ 43 str2Int[b] = idx; 44 int2Str[idx] = b; 45 idx++; 46 } 47 //记录每个人的通话时间,以及和谁通话 48 idx1 = str2Int[a]; idx2 = str2Int[b]; 49 v[idx1] += time; v[idx2] += time; 50 adj[idx1].push_back(idx2); adj[idx2].push_back(idx1); 51 } 52 vector<Node> ans; 53 int cnt=0; 54 for(i=0; i<idx; i++){ 55 int head = i, gangNum=0, gangTime=0; 56 if(!vis[i]){//查找一个群体中的通话时间, 人数以及头目 57 dfs(i, head, gangNum, gangTime); 58 Node node; 59 if(gangNum>2 && gangTime>k*2){ 60 cnt++; 61 node.id = head; 62 node.num = gangNum; 63 ans.push_back(node); 64 } 65 } 66 } 67 cout<<cnt<<endl; 68 sort(ans.begin(), ans.end(), cmp); 69 for(i=0; i<ans.size(); i++) cout<<int2Str[ans[i].id]<<" "<<ans[i].num<<endl; 70 return 0; 71 }