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
题意:n个通话记录,k为59。a和b通话t分钟。
判断将人们分为几个群体,群体的要求是:人数大于2,总通话时间大于k。
输出群体数和每个群体的头目(通话时间最久的人)以及团体中人数。
思路:根据通话记录建立连通块,用并查集找出每个团体,然后累加时间和人数。
输入的时候用sum数组存每个人累计的通话时间,但是这样一个通话会被算2次,所有最后判断的时候k*2
#include <stdio.h>
#include <string.h>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <iostream>
#define INF 0x3f3f3f3f
using namespace std;
int ma[2500][2500];
int fa[2500];//记录祖先
int vis[2500];
int sum[2500];//记录每个人的通话时间
int tot[2500],num[2500];//记录团体的通话时间和人数
map<string,int> ys1;
map<int,string> ys2;
int p;
int find(int x)
{
int r=x,i=x,j;
while(fa[r]!=r)
r=fa[r];
while(fa[i]!=i)
j=fa[i],fa[i]=r,i=j;
return r;
}
void combine(int x,int y)
{
int xx=find(x);
int yy=find(y);
if(sum[xx]<sum[yy])
fa[xx]=yy;
else
fa[yy]=xx;
}
int main()
{
int n,k,t,i,j,l;
p=1;
string a,b;
scanf("%d%d%d%d",&n,&k);
memset(ma,INF,sizeof ma);
memset(sum,0,sizeof sum);
memset(vis,0,sizeof vis);
for(i=0;i<n;i++)
{
cin>>a>>b>>t;
if(ys1[a]==0)
{
ys1[a]=p;
ys2[p]=a;
p++;
}
if(ys1[b]==0)
{
ys1[b]=p;
ys2[p]=b;
p++;
}
sum[ys1[a]]+=t;
sum[ys1[b]]+=t;
ma[ys1[a]][ys1[b]]=ma[ys1[b]][ys1[a]]=t;
}
for(i=1;i<p;i++)
fa[i]=i;
for(i=1;i<p;i++)
{
for(j=1;j<p;j++)
{
if(ma[i][j]!=INF)
{
combine(i,j);
}
}
}
memset(tot,0,sizeof tot);
memset(num,0,sizeof num);
for(i=1;i<p;i++)
{
int f=find(i);
tot[f]+=sum[i];
num[f]++;
}
vector<string> v;
for(i=0;i<p;i++)
{
if(tot[i]>(k*2)&&num[i]>2)
v.push_back(ys2[i]);
}
sort(v.begin(),v.end());
printf("%d\n",v.size());
for(i=0;i<v.size();i++)
cout<<v[i]<<" "<<num[ys1[v[i]]]<<endl;
}