1.该题目主要求团伙中通话的时间总和,有多个潜在的团伙,涉及到了并查集和一些统计。
2.算法过程
1)收集根据记录,存储每个人的总通话时间和他的联系人,同时进行并查集的合并。
2)根据并查集中分好的集合,进行集合的分类,并且分别统计各集合的名单和总通话时间
3)遍历上面的集合,找出符合条件的gang和gang的头目
注: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
有联系即可,不是gang里面每个人都相互联系。
代码利用了大量的map,set之类,可能会有点难懂。
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
[c language=”++”]
//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
struct guyNode{
set<string> contact;
int time;
string r;
guyNode() : time(0), r(""){ contact.clear(); };
};
string find(string a,map<string, guyNode>&m)
{
if (m[a].r == a)
return a;
else
{
m[a].r = find(m[a].r, m);
return m[a].r;
}
}
int main(void)
{
int recordNum, threshold;
cin >> recordNum >> threshold;
map<string, guyNode> m;//存储每个人的总通话时间和联系人
for (int i = 0; i < recordNum; i++)
{//输入联系记录
string a, b;
int time;
cin >> a >> b >> time;
if (m[a].r == "") m[a].r = a;//如果是第一次输入,则把代表设为自己
if (m[b].r == "") m[b].r = b;
m[a].contact.insert(b);//增加联系人b
m[a].time += time;//累加通话时间
m[b].contact.insert(a);//增加联系人a
m[b].time += time;//累加通话时间
if (find(a,m)!=find(b,m))
m[find(a, m)].r = find(b, m);//合并
}
map<string, set<string>> gang;//存储gang的代表和成员,注意,代表还不是头目
map<string, int> contactTime;//计算各个gang的总通话时间,注意,a和b通话,a累加了一次时间,b又累加了一次时间,所以是实际时间的两倍
for (map<string, guyNode>::iterator ite = m.begin(); ite != m.end(); ite++)
{//遍历所有的集合
gang[find(ite->first, m)].insert(ite->first);//利用并查集,分为不同的集合gang
contactTime[find(ite->first, m)] += ite->second.time;//统计各个gang的时间
}
map<string, int> ans;
for (map<string, set<string>>::iterator ite = gang.begin(); ite != gang.end(); ite++)
{
if (contactTime[ite->first] <= threshold * 2 || ite->second.size()<=2)
continue;//通话时间不满足要求,人数少于两人
bool isGang = true;//标志位
string head = "";//设置头目信息
int maxTime = 0;//通话时间为0
int sum = ite->second.size();//gang的总人数
for (set<string>::iterator ite2 = ite->second.begin(); ite2 != ite->second.end(); ite2++)
{
string name = *ite2;
if (m[name].time > maxTime)
{//查找通话时间最长的人,即head of gang
head = name;
maxTime = m[name].time;
}
}
if (isGang)
{
ans[head] = sum;
}
}
cout << ans.size() << endl;
for (map<string, int>::iterator ite = ans.begin(); ite != ans.end(); ite++)
{//输出gang的head和成员数
cout << ite->first << " " << ite->second << endl;
}
return 0;
}
[/c]