1034 Head of a Gang (30 分) 并查集

本文介绍了一种利用电话记录数据进行社会关系分析的方法,通过并查集算法识别群体及其头目。输入包括通话次数、通话时长阈值及通话双方信息,输出为群体数量、头目及成员数。文章详细阐述了算法实现过程,包括数据结构设计、并查集操作及结果输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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; 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值