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
解决方法
题目大意
给你N个通话记录,通话记录分别是通话的两个人A和B的Name,以及通话时间Time,如果两个人有通话记录,认定他们是一个团伙的,权重以通话时长为标准,比如A和B通话,B和C通话,那么A、B、C是一个团伙的,规定团伙必须超过两个人。一个团伙中,通话时间最长且超过给定阈值K的人是该团伙的老大。
解题方法
DFS
#include<iostream>
#include<string>
#include<map>
using namespace std;
const int maxn = 2010;
int weight[maxn];
bool visit[maxn];
int graph[maxn][maxn];
map<string, int>stringtoint,ans;
map<int, string>inttostring;
int n, k, idnum = 1;
int stoifunc(string s)
{
if (stringtoint[s] == 0)
{
stringtoint[s] = idnum;
inttostring[idnum] = s;
return idnum++;
}
else return stringtoint[s];
}
void dfs(int i, int &head, int &personnum, int &totalweight)
{
visit[i] = true;
personnum++;
if (weight[i] > weight[head]) head = i;
for (int j = 1; j < idnum; j++)
{
if (graph[i][j] > 0)
{
totalweight += graph[i][j];
graph[i][j] = graph[j][i] = 0;
if (visit[j] == false) dfs(j, head, personnum, totalweight);
}
}
}
void dfstraverse()
{
for (int i = 1; i < idnum; i++)
{
if (visit[i] == false)
{
int head = i, personnum = 0, totalweight = 0;
dfs(i, head, personnum, totalweight);
if (totalweight > k&&personnum > 2) ans[inttostring[head]] = personnum;
}
}
}
int main()
{
int call, v1, v2;
string s1, s2;
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++)
{
cin >> s1 >> s2 >> call;
v1 = stoifunc(s1), v2 = stoifunc(s2);
graph[v1][v2] += call;
graph[v2][v1] += call;
weight[v1] += call;
weight[v2] += call;
}
dfstraverse();
printf("%d\n", ans.size());
for (auto it = ans.begin(); it != ans.end(); it++)
{
cout << it->first << " " << it->second << endl;
}
return 0;
}
并查集
#include<iostream>
#include<string>
#include<map>
#include<vector>
using namespace std;
map<string, int>stringtoint, ans;
map<int, string>inttostring;
const int maxn = 2010;
int n, k, v1, v2, w, idnum = 1;
int visit[maxn], weight[maxn],father[maxn];
int findfather(int root)
{
int son = root;
while (root != father[root]) root = father[root];
while (son != father[son])
{
int tmp = son;
son = father[son];
father[tmp] = root;
}
return root;
}
void Union(int a,int b)
{
int faa = findfather(a);
int fab = findfather(b);
if (faa != fab) father[faa] = fab;
}
void init()
{
for (int i = 0; i < maxn; i++) father[i] = i;
fill(visit, visit + maxn, false);
}
int stoifun(string s)
{
if (stringtoint[s] == 0)
{
stringtoint[s] =idnum;
inttostring[idnum] = s;
return idnum++;
}
else return stringtoint[s];
}
int main()
{
scanf("%d %d", &n, &k);
string s1, s2;
init();
for (int i = 0; i < n; i++)
{
cin >> s1 >> s2 >> w;
v1 = stoifun(s1), v2 = stoifun(s2);
weight[v1] += w;
weight[v2] += w;
Union(v1, v2);
}
for (int i = 1; i < idnum; i++)
{
if (visit[i] == false)
{
visit[i] = true;
int p = findfather(i);
int totalweight = weight[i];
int personnum = 1;
int head = i;
for (int j = i + 1;j<idnum;j++)
{
if (visit[j] == false && findfather(j) == p)
{
visit[j] = true;
personnum++;
totalweight += weight[j];
if (weight[j] > weight[head]) head = j;
}
}
totalweight /= 2;
if (personnum > 2 && totalweight > k) ans[inttostring[head]] = personnum;
}
}
printf("%d\n", ans.size());
for (auto it = ans.begin(); it != ans.end(); it++)
{
cout << it->first << " " << it->second << endl;
}
return 0;
}
后记
这道题难度还是有的,但是也不是到了那种无法入手的难度。这道题的并查集着实让我搞了一天多。。。并查集还需要多加练习o(╥﹏╥)o