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
思路:我使用的是并查集,也可以用DFS做。把相互通话的结点进行合并,然后对每个群体进行鉴别,并记录群体中最长通话时间的结点以便找出首领。
#include <bits/stdc++.h>
using namespace std;
const int maxv=26*26*26+10;
vector<int> father(maxv),isroot(maxv,0);
unordered_map<int,int> weight; //各结点通话时长
void init() //数组初始化
{
for(int i=0;i<maxv;i++) father[i]=i;
}
int find_father(int x) //找父节点并路径压缩
{
int a=x;
while(x!=father[x]) x=father[x];
while(a!=father[a])
{
int z=a;
a=father[a];
father[z]=x;
}
return x;
}
void Union(int a,int b) //合并结点
{
int fa=find_father(a),fb=find_father(b);
if(fa!=fb) father[max(fa,fb)]=min(fa,fb);
}
int change(string s) //名字和数字的哈希映射
{
int sum=0;
for(char c:s) sum=sum*26+(c-'A');
return sum;
}
int main()
{
int n,k,maxpos=0,minpos=maxv,ans=0;
unordered_map<int,string> mp1; //数字->名字
unordered_map<int,int> mp2; //记录每个群体中最长通话时长的时间,以便找出首领
set<int> st; //set保存各节点
init();
scanf("%d %d",&n,&k);
//printf("%d %d\n",n,k);
for(int i=0;i<n;i++)
{
string a,b;
int time,ca,cb;
cin>>a>>b>>time;
ca=change(a);
cb=change(b);
mp1[ca]=a;
mp1[cb]=b;
weight[ca]+=time;
weight[cb]+=time;
Union(ca,cb);
st.insert(ca);
st.insert(cb);
}
unordered_map<int,int> mp;
unordered_map<int,bool> vis;
for(auto it=st.begin();it!=st.end();it++) //判别群体
{
int root=find_father(*it);
isroot[root]++; //群体个数
mp[root]+=weight[*it]; //群体总通话时间,但每个个体被计算了两次,真正的总通话时间是其值的二分之一
//cout<<mp1[root]<<" "<<isroot[root]<<" "<<mp[root]<<" "<<mp1[*it]<<endl;
mp2[root]=max(mp2[root],weight[*it]); //群体中最长的通话时长
if(vis[root]==false&&isroot[root]>2&&mp[root]/2>k) //满足条件就鉴定为一个群体
{
ans++;
vis[root]=true;
}
}
printf("%d\n",ans);
if(ans==0) return 0;
for(auto it=st.begin();it!=st.end();it++) //输出
{
int root=find_father(*it);
//printf("root=%d isroot=%d mp[root]=%d weight[i]=%d\n",root,isroot[root],mp[root],weight[*it]);
if(isroot[root]>2&&mp[root]/2>k&&weight[*it]==mp2[root]) cout<<mp1[*it]<<" "<<isroot[root]<<endl;
}
//system("pause");
return 0;
}