Head of a Gang

  1. 1. Head of a gang
  2. 2. 思路

Head of a gang

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 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:

1
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:

1
2
3
4
5
6
7
8
9
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:

1
2
3
2
AAA 3
GGG 3

Sample Input 2:

1
2
3
4
5
6
7
8
9
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:

1
0

题目大意:警方要抓捕犯罪分子.打过电话的两个人有关系,关系的权重是通话时间.一组有关系的人,如果关系的权重和超过阈值K,并且有大于两个人,就认为是一个犯罪团伙.通话时间最长的人是头目.现在给定一组通话记录,求其中的犯罪团伙的个数,以及各个头目的名字.

思路

这是一道图论的综合题.首先要把通话记录中的名字变成下标,可以用map<string,int>实现.
然后要求图的连通分量个数.对于每个连通分量,求该连通分量的边权和,出边权值和最大的节点,连通分量的大小.

边权和可将所有边权求和除以2得出,出边权值在访问邻接点时可以计算.正好和dfs结合起来.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;
int n,k;
int weight[4000][4000];
map<string,int> f;
int ind=0;
bool visited[4000];
struct p{
string name;
int size;
};
vector<p> result;
bool cmp(p p1,p p2)
{
return p1.name<p2.name;
}
int next_index(string str)
{
if(f.find(str)==f.end())
{
f[str]=ind;
ind++;
}
return f[str];
}
bool bfs(int index,int& max_index,int& size)
{
queue<int> q;
q.push(index);
int counter = 0;
int sum=0;
int max_sum=-1;
max_index=-1;
visited[index]=true;
while(!q.empty())
{
int f = q.front();
int acc=0;
q.pop();
counter++;
for(int i=0;i<n;i++)
{
if(weight[f][i]!=0)
{
if(!visited[i])
{
visited[i]=true;
q.push(i);
}
sum+=weight[f][i];
acc+=weight[f][i];
}
}
if(acc>max_sum)
{
max_sum=acc;
max_index=f;
}
}
size = counter;
return counter>2&&sum>k*2;
}
int main()
{
cin>>n>>k;
for(int i=0;i<n;i++)
{
int w;
string s1,s2;
cin>>s1>>s2>>w;
int n1 = next_index(s1);
int n2 = next_index(s2);
weight[n1][n2]+=w;
weight[n2][n1]+=w;
}
fill(visited,visited+n,false);
for(auto it=f.begin();it!=f.end();it++)
{
if(!visited[it->second])
{
int max_index;
int size;
if(bfs(it->second,max_index,size))
{
for(auto ii=f.begin();ii!=f.end();ii++)
{
if(ii->second==max_index)
{
p pp;
pp.name=ii->first;
pp.size=size;
result.push_back(pp);
}
}
}
}
}
cout<<result.size()<<endl;
sort(result.begin(),result.end(),cmp);
for(auto p:result)
cout<<p.name<<" "<<p.size<<endl;
}

在构建从人名到下标的映射的时候可以顺便构建从下标到人名的反向映射.可以省去输出时遍历f中所有元素的反向查找,我这里就偷懒了:-P