1034 Head of a Gang (30)(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 threthold 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
思路:
我一开始想的是用两个map分别记录name-id和id-name之间的映射,但是在提交的时候始终会有一组段错误,后来看到别人提醒才知道要手动hash。然后就是dfs跑一遍了,参考了一下大佬的代码,这位大佬全程map过了,虽然我不知道为啥我用map就过不去_(:з」∠)_
代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
vector<int> g[17576];
map<string,int> ans;
int per[17576], tim[17576], vis[17576];
int sum, cnt, head;
int name_to_num(string s)
{
return (s[0] - 'A') * 26 * 26 + (s[1] - 'A') * 26 + (s[2] - 'A');
}
string num_to_name(int n)
{
string s = "";
int i = 3;
while (i--)
{
s = char(n % 26 + 'A') + s;
n /= 26;
}
return s;
}
void dfs(int x)
{
vis[x] = 1;
sum += tim[x];
cnt++;
if (tim[x] > tim[head])
head = x;
for (int i = 0; i < g[x].size(); i++)
{
if (!vis[g[x][i]])
dfs(g[x][i]);
}
}
int main()
{
int n, k, id = 0;
cin >> n >> k;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++)
{
string name1, name2;
int t, n1, n2;
cin >> name1 >> name2 >> t;
n1 = name_to_num(name1);
n2 = name_to_num(name2);
if (!vis[n1])
{
vis[n1] = 1;
per[id++] = n1;
}
if (!vis[n2])
{
vis[n2] = 1;
per[id++] = n2;
}
g[n1].push_back(n2);
g[n2].push_back(n1);
tim[n1] += t;
tim[n2] += t;
}
memset(vis, 0, sizeof(vis));
for (int i = 0; i < id; i++)
{
if (!vis[per[i]])
{
sum = 0;
cnt = 0;
head = per[i];
dfs(head);
if (cnt > 2 && sum / 2 > k)
ans[num_to_name(head)] = cnt;
}
}
cout << ans.size() << endl;
for (map<string, int>::iterator iter = ans.begin(); iter != ans.end(); iter++)
cout << iter->first << " " << iter->second << endl;
return 0;
}