https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624
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### Aand### B, we say that### Aand### Bis 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
whereName1andName2are the names of people at the two ends of the call, andTimeis the length of the call. A name is a string of three capital letters chosen fromA-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
题目大意:
给出若干个人的通话长度,分为若干组
每个组总边权设为该组内的所有通话的长度之和,每个人的点权设为该人参与的通话长度之和。
给顶一个阈值K,并满足成员人数超过2,则将该组视为“犯罪团伙(Gang)”,而该组内点权最大的人视为头目。
要求输出“犯罪团伙的个数”,并按头目名字字典序从小到大的顺序输出每个犯罪团伙头目姓名和成员人数
边稠密,用邻接矩阵
使用map做名字到编号与编号到名字的映射,双向映射不好做就用两个map
#include <iostream>
#include <map>
using namespace std;
const int maxn = 2010;
map<int, string> int2str;
map<string, int> str2int;
map<string, int> Gang;
int E[maxn][maxn], weight[maxn];
int n, k, tot;
bool vis[maxn] = {false};
// 当前访问编号、头目、成员数、总边权
void dfs(int cur, int &head, int &numMember, int &totValue)
{
numMember ++;
vis[cur] = true;
if (weight[cur] > weight[head])
head = cur;
for (int i = 0; i < tot; i ++ )
{
if (E[cur][i] > 0)
{
totValue += E[cur][i];
E[cur][i] = E[i][cur] = 0;
if (!vis[i]) dfs(i, head, numMember, totValue);
}
}
}
void dfsTrave()
{
for (int i = 0; i < tot; i ++)
{
if (!vis[i])
{
// 头目、成员数、总边权
int head = i, numMember = 0, totValue = 0;
dfs(i, head, numMember, totValue);
if (numMember > 2 && totValue > k)
Gang[int2str[head]] = numMember;
}
}
}
int change(string str)
{
if(str2int.find(str)!=str2int.end())
return str2int[str];
else
{
str2int[str] = tot;
int2str[tot] = str;
return tot ++;
}
}
int main()
{
int w;
string str1, str2;
cin >> n >> k;
for (int i = 0; i < n; i ++)
{
cin >> str1 >> str2 >> w;
int id1 = change(str1);
int id2 = change(str2);
weight[id1] += w;
weight[id2] += w;
E[id1][id2] += w;
E[id2][id1] += w;
}
dfsTrave();
cout << Gang.size() << endl;
for (auto &g : Gang)
cout << g.first << " " << g.second << endl;
return 0;
}