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 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
链接:
https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624
思路:
使用DFSTraverse()函数遍历每个点获取连通图的个数,使用DFS()函数获取每个连通图的信息(总权重、head)。
代码:
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
const int maxn = 2010;
int weight[maxn];
int G[maxn][maxn];
bool vis[maxn];
int m, k, cnt = 0;
int n = 0, head, totalw, num, maxV;
map<string, int> stringToInt;
map<int, string> intToString;
struct Gang{
int head, number;
string name;
}Gangs[maxn];
int change(string s){
if(stringToInt.find(s) != stringToInt.end()){
return stringToInt[s];
}
else{
stringToInt[s] = n;
intToString[n] = s;
return n++;
}
}
void DFS(int k){
vis[k] = true;
for(int i = 0; i < n; ++i){
if(!vis[i] && G[k][i] > 0){
totalw += weight[i];
// totalw += G[k][i];
num++;
if(weight[i] > maxV){
maxV = weight[i];
head = i;
}
DFS(i);
}
}
}
void DFSTraverse(){
memset(vis, 0, sizeof(vis));
for(int i = 0; i < n; ++i){
if(!vis[i]){
num = 1; //每个犯罪团伙的人数,初始化为1!因为已经有1个人在
totalw = weight[i]; //每个犯罪团伙的总权重
head = i; //犯罪团伙的head
maxV = weight[i]; //求犯罪团伙的head的最大权重
DFS(i);
totalw /= 2; //图的总边权要除以2,因为每条边都加了2次
if(totalw > k && num > 2){
Gangs[cnt].head = head;
Gangs[cnt].number = num;
cnt++;
}
}
}
}
bool cmp(Gang a, Gang b){
return a.name < b.name;
}
int main(){
// freopen("a.txt", "r", stdin);
int t;
string s1, s2;
cin >> m >> k;
fill(G[0], G[0] + maxn * maxn, 0);
memset(weight, 0, sizeof(weight));
for(int i = 0; i < m; ++i){
cin >> s1 >> s2 >> t;
int id1 = change(s1);
int id2 = change(s2);
G[id1][id2] += t;
G[id2][id1] += t;
weight[id1] += t;
weight[id2] += t;
}
DFSTraverse();
cout << cnt << endl;
for(int i = 0; i < cnt; ++i){
Gangs[i].name = intToString[Gangs[i].head];
}
sort(Gangs, Gangs + cnt, cmp);
for(int i = 0; i < cnt; ++i){
cout << Gangs[i].name << " " << Gangs[i].number << endl;
}
return 0;
}