PAT 甲级 1034. Head of a Gang (30) C++版

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

我是用深搜解决的,实际上是有个连通图的节点的个数要大于2,每个节点的权值是时间,一个连通图的节点的权值总和要大于给出的k

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
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

const int len = 27 * 27 * 27;

vector<vector<int> > net(len);
int times[len], isVisited[len];

struct Gang {
int index, cnt;

Gang(int m_index, int m_cnt) {
index = m_index;
cnt = m_cnt;
}
};

bool cmp(struct Gang a, struct Gang b) {
return a.index < b.index;
}

int getIndex(char *name) {
int index = 0;
for (int i = 0; i < 3; i++) { index = index * 26 + name[i] - 'A'; }
return index;
}

void printName(int index) {
printf("%c", index / (26 * 26) + 'A');
printf("%c", index % (26 * 26) / 26 + 'A');
printf("%c", index % 26 + 'A');
}

void dfs(int &cnt, int cur, int &head, int &total) {
for (auto it = net[cur].begin(); it != net[cur].end(); it++) {
if (!isVisited[*it]) {
isVisited[*it] = 1;
if (times[head] < times[*it]) head = *it;
cnt++;
total += times[*it];
dfs(cnt, *it, head, total);
}
}
}

int main() {
int n = 0, k = 0;
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) {
char name1[4], name2[4];
int time = 0;
scanf("%s %s %d", name1, name2, &time);
int index1 = getIndex(name1), index2 = getIndex(name2);
net[index1].push_back(index2);
net[index2].push_back(index1);
times[index1] += time;
times[index2] += time;
}
vector<struct Gang> ans;
for (int i = 0; i < len; i++) {
int cnt = 1, head = i, total = times[i];;
if (!isVisited[i] && net[i].size() != 0) {
isVisited[i] = 1;
dfs(cnt, i, head, total);
}
if (cnt > 2 && total > 2 * k) {
struct Gang g(head, cnt);
ans.push_back(g);
}
}
printf("%lu\n", ans.size());
sort(ans.begin(), ans.end(), cmp);
for (auto it = ans.begin(); it != ans.end(); it++) {
printName(it->index);
printf(" %d\n", it->cnt);
}
return 0;
}