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:

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.

浅析

Q:找出联通子图,其结点数大于 2,边的总权值大于 k

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

Code

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
#include 
#include
#include
#include
#include
using namespace std;

const int maxn=2010;
int G[maxn][maxn]={0}; //邻接矩阵表示图
struct node{
int id;
int weight = 0;
}P[maxn]; //记录每个顶点的点权
int num_person=0,num_group=0,weight; //num_person表示当前人数,num_group表示极大子图数,weight表示指定的权重
bool vis[maxn]={false}; //用于深搜
bool is_prime[maxn]={false}; //是否是符合要求的犯罪团体
vector group[maxn]; //用于保存每个极大子图所包含的结点信息
map <int,string> i_to_s;
map <string,int> s_to_i;

int change(string s){
if(s_to_i.find(s)!=s_to_i.end())
return s_to_i[s];
else{
s_to_i[s] = num_person;
i_to_s[num_person] = s;
return num_person++;
}
}
bool cmp(node a,node b){
return a.weight>b.weight;
}
void DFS(int s){
vis[s] = true;
group[num_group].push_back(P[s]);
for(int i=0;i
if(!vis[i]&&(G[s][i]||G[i][s])){
DFS(i);
}
}
}
//下面这堆玩意儿是最后补的,为的是按照名字字典序输出
struct out{
string str;
int num;
out(string _str,int _num):str(_str),num(_num){}
};
vector o;
bool cmp_o(out a,out b){
return a.str
}

int main(){
int n;
scanf("%d%d",&n,&weight);
for(int i=0;i
string s1,s2;
int w,a,b;
cin>>s1>>s2>>w;
a = change(s1);
b = change(s2);
P[a].id = a;
P[b].id = b;
P[a].weight += w;
P[b].weight += w;
G[a][b] = w;
}
int num=0;
for(int i=0;i
if(vis[i]==false){
DFS(i);
if(group[num_group].size()>2){
int total_weight=0;
for(int j=0;j
total_weight+=group[num_group][j].weight;
if(total_weight>2*weight){
num++;
is_prime[num_group] = true;
}
}
num_group++;
}
}
printf("%d\n",num);
for(int j=0;j
if(is_prime[j]){
sort(group[j].begin(),group[j].end(),cmp);
int id = group[j][0].id;
o.push_back(out(i_to_s[id],group[j].size()));
// cout<
}
sort(o.begin(),o.end(),cmp_o);
for(int j=0;j
printf("%s %d\n",o[j].str.c_str(),o[j].num);
}
}
丨fengche丨 wechat
来找我聊天吧~
-------------本文结束感谢您的阅读-------------