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
【分析】无向带权图,求满足要求的连通分量数量
总结:
这个问题要整整思路再来写,不然写出来就是下面这个乱七八糟的东西。
目前的结果是这样,暂时不知道问题在哪。。
下次再试试,花了好长时间了。
0 | 答案正确 | 84 ms | 16256 KB |
1 | 答案正确 | 83 ms | 16256 KB |
2 | 答案正确 | 65 ms | 16256 KB |
3 | 运行时错误 | 90 ms | 16384 KB |
4 | 运行时错误 | 76 ms | 16288 KB |
5 | 答案正确 | 87 ms | 16288 KB |
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <string.h>
#include <set>
using namespace std;
const int maxn = 2e3 + 5;
int n, k, num = 0, ans = 0;
int G[maxn][maxn];
bool used[maxn];
string person[maxn];
set<int> gang[maxn];
typedef pair<string, int> P;
vector<P> head;
int weight[maxn], total[maxn];
int locate(string s) {
for (int i = 0; i < num; i++) {
if (s == person[i]) return i;
}
return -1;
}
void dfs(int x) {
gang[ans].insert(x);
used[x] = true;
for (int i = 0; i < num; i++) {
if (G[x][i] != 0 && !used[i]) {
dfs(i);
}
}
}
void calc() {
for (int i = 0; i < ans; i++) {
int index, maxi = 0;
for (set<int>::iterator it = gang[i].begin(); it != gang[i].end(); it++) {
total[i] += weight[*it];
if (weight[*it] > maxi) {
index = *it;
maxi = weight[*it];
}
}
total[i] /= 2;
if (gang[i].size() > 2 && total[i] > k) head.push_back(make_pair(person[index], gang[i].size()));
}
}
void solve() {
fill(used, used + num, false);
for (int i = 0; i < num; i++) {
if (!used[i]) {
dfs(i);
ans++;
}
}
calc();
if (head.size() == 0) {
printf("0");
return;
}
printf("%d\n", head.size());
sort(head.begin(), head.end());
for (int i = 0; i < head.size(); i++) {
cout << head[i].first << " " << head[i].second;
printf("%s", i != head.size() - 1 ? "\n" : "");
}
}
int main() {
cin >> n >> k;
memset(G, 0, sizeof(G));
memset(weight, 0, sizeof(weight));
memset(total, 0, sizeof(total));
for (int i = 0; i < n; i++) {
char a[10], g1[5], g2[5];
int w, id1, id2;
getchar();
scanf("%[^\n]s", a);
sscanf(a, "%s %s %d", &g1, &g2, &w);
string s1 = g1, s2 = g2;
id1 = locate(s1);
id2 = locate(s2);
if (id1 == -1) {
id1 = num;
person[num++] = s1;
}
if (id2 == -1) {
id2 = num;
person[num++] = s2;
}
G[id1][id2] += w;
G[id2][id1] += w;
weight[id1] += w;
weight[id2] += w;
}
solve();
return 0;
}