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
这题的难点主要是给字符串编号问题。
思路:
1.数据如何存储
map的应用+vector+dfs
map1对字符串编号,map2相反把编号转化为字符串。
mapforcount用于记录每个人的通话时间。
2.如何选出人数大于3,且总通话时间>m的团体。
(1)dfs遍历关系图。每次把一个联通分量存储到vector中。
void dfs(int index) {
vis[index] = true;
temp.push_back(map2[index]);
for (int i = 0; i < g[index].size(); i++) {
int x = map1[g[index][i]];
if(vis[x]==false)
dfs(x);
}
}
(2)判断vector的大小,如果大于2且所有人的通话时间和/2>m,那么表示这个团体非法。找出vector中那个通话时间最长的人。
for (it = map1.begin(); it != map1.end(); it++) {
if (vis[it->second] == false) {
dfs(it->second);
if (temp.size() > 2) {
int max = -1, sumtemp = 0;
string s;
for (int i = 0; i < temp.size(); i++) {
sumtemp += mapforcount[temp[i]];
if (max < mapforcount[temp[i]]) {
s = temp[i];
max = mapforcount[temp[i]];
}
}
if (sumtemp/2 > m) {
ss temp1;
temp1.name = s;
temp1.sum = temp.size();
v.push_back(temp1);
}
}
temp.clear();
}
}
写完思路粘贴代码:
#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
#include <string>
#include <memory.h>
#include <set>
#include <stack>
#include <queue>
#include <unordered_map>
#include <iomanip>
#include <algorithm>
#include <cmath>
using namespace std;
struct ss {
int sum;
string name;
};
unordered_map<string, int > map1, mapforcount;
unordered_map<int, string > map2;
vector<string > g[2010], temp;
vector<ss > v;
bool vis[2010] = { false };
bool cmp(ss a, ss b) {
return a.name < b.name;
}
void dfs(int index) {
vis[index] = true;
temp.push_back(map2[index]);
for (int i = 0; i < g[index].size(); i++) {
int x = map1[g[index][i]];
if(vis[x]==false)
dfs(x);
}
}
int main() {
string s1, s2;
int n, k, count = 0, num, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s1 >> s2 >> num;
if (map1.find(s1) == map1.end()) {
map1[s1] = count;
map2[count++] = s1;
}
if (map1.find(s2) == map1.end()) {
map1[s2] = count;
map2[count++] = s2;
}
mapforcount[s1] += num;
mapforcount[s2] += num;
g[map1[s1]].push_back(s2);
g[map1[s2]].push_back(s1);
}
unordered_map<string, int>::iterator it;
for (it = map1.begin(); it != map1.end(); it++) {
if (vis[it->second] == false) {
dfs(it->second);
if (temp.size() > 2) {
int max = -1, sumtemp = 0;
string s;
for (int i = 0; i < temp.size(); i++) {
sumtemp += mapforcount[temp[i]];
if (max < mapforcount[temp[i]]) {
s = temp[i];
max = mapforcount[temp[i]];
}
}
if (sumtemp/2 > m) {
ss temp1;
temp1.name = s;
temp1.sum = temp.size();
v.push_back(temp1);
}
}
temp.clear();
}
}
cout << v.size() << endl;
sort(v.begin(), v.end(), cmp);
for (int i = 0; i < v.size(); i++) {
cout << v[i].name << " " << v[i].sum << endl;
}
}