1034 Head of a Gang
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
题意:
给出N个通话记录,以及一个阈值K,找出成员大于两个且通话时长大于阈值的集团,并按照字典序输出集团中的头目(在集团中通话时间最长的那个)。
思路:
这道题应该是一道DFS的问题,运用DFS找出一个图中的连通分量。因为所给的都是字符串,所以我们要通过两个map来实现字符串和数字之间的转换。DFS的过程中使用引用来传递变量,这样在遍历的过程中能够实时的更新集团中的人数及通话时长。用一个visited来标记该人是否已经被访问过,用一个二维数组G[i][j]来存储两个点之间的权重,如果这两点已经被访问过,则将两点之间的权重置零,防止死循环。
Code:
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 map<string, int> strToi; 6 map<int, string> iToStr; 7 map<string, int> ans; 8 int countId = 1, k; 9 vector<bool> visited(1005, false); 10 int G[2005][2005] = {0}; 11 int weight[2005] = {0}; 12 13 int strToiFac(string str) { 14 if (strToi[str] == 0) { 15 strToi[str] = countId; 16 iToStr[countId] = str; 17 return countId++; 18 } else { 19 return strToi[str]; 20 } 21 } 22 23 void dfs(int u, int& head, int& numMember, int& totalWeight) { 24 visited[u] = true; 25 numMember++; 26 if (weight[u] > weight[head]) head = u; 27 for (int i = 1; i < countId; ++i) { 28 if (G[u][i] > 0) { 29 totalWeight += G[u][i]; 30 G[u][i] = G[i][u] = 0; 31 if (!visited[i]) dfs(i, head, numMember, totalWeight); 32 } 33 } 34 } 35 36 void dfsTrav() { 37 for (int i = 1; i < countId; ++i) { 38 if (!visited[i]) { 39 int head = i, numMember = 0, totalWeight = 0; 40 dfs(i, head, numMember, totalWeight); 41 if (numMember > 2 && totalWeight > k) ans[iToStr[head]] = numMember; 42 } 43 } 44 } 45 46 int main() { 47 int N; 48 cin >> N >> k; 49 for (int i = 0; i < N; ++i) { 50 string Name1, Name2; 51 int Time; 52 cin >> Name1 >> Name2 >> Time; 53 int id1 = strToiFac(Name1); 54 int id2 = strToiFac(Name2); 55 weight[id1] += Time; 56 weight[id2] += Time; 57 G[id1][id2] += Time; 58 G[id2][id1] += Time; 59 } 60 dfsTrav(); 61 cout << ans.size() << endl; 62 for (auto s : ans) cout << s.first << " " << s.second << endl; 63 64 return 0; 65 }
参考:
https://www.liuchuo.net/archives/2331