PAT 甲级 1034  Head of a Gang

1034 Head of a Gang (30 point(s))

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

经验总结:

emmmm   英文实在写的太不爽,而且自己水平有限还词不达意,以后还是用中文啦~   程序猿何苦为难程序猿嘛~
这一题,首先要注意,人家说的是 N<=1000,所以总共可能的最大人数为2000(所有行的两个人都不一样),其他的,就....还有一点吧,这一题可以使用map<string, struct> 的形式进行并查集搜索,也可以将string 转换成对应的唯一的号码进行常规的并查集搜索,当然,因为STL的速度显然没有数组快,所以后者的速度会更快一些~

AC代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <iostream>
#include <vector>
using namespace std;
const int maxn=2010;
int n,k,num[maxn],total[maxn];
struct person
{
	int time;
	int father;
	person():time(0){}
}P[maxn];
map<string,int> mp,rec;
map<int,string> rmp;

int findFather(int x)
{
	int a=x;
	while(x!=P[x].father)
		x=P[x].father;
	while(a!=P[a].father)
	{
		int temp=a;
		a=P[a].father;
		P[temp].father=x;
	}
	return x;
}
void Union(int a,int b)
{
	int a1=findFather(a);
	int b1=findFather(b);
	if(a1!=b1)
	{
		if(P[a1].time<P[b1].time)
			P[a1].father=b1;
		else
			P[b1].father=a1;
	}
}
int main()
{
	scanf("%d %d",&n,&k);
	string a,b;
	for(int i=0;i<maxn;++i)
		P[i].father=i;
	int t,no=0;
	vector<pair<int,int> > ope;
	for(int i=0;i<n;++i)
	{
		cin>>a>>b>>t;
		if(mp.count(a)==0)
		{
			rmp[no]=a;
			mp[a]=no++;
		}
		if(mp.count(b)==0)
		{
			rmp[no]=b;
			mp[b]=no++;
		}
		P[mp[a]].time+=t;
		P[mp[b]].time+=t;
		ope.push_back(make_pair(mp[a],mp[b]));
	}
	for(int i=0;i<ope.size();++i)
	{
		Union(ope[i].first,ope[i].second);
	}
	memset(num,0,sizeof(num));
	memset(total,0,sizeof(total));
	for(int i=0;i<no;++i)
	{
		int temp=findFather(i);
		++num[temp];
		total[temp]+=P[i].time;
	}
	for(int i=0;i<no;++i)
	{
		if(num[i]>2&&total[i]/2>k)
			rec[rmp[i]]=num[i];
	}
	printf("%d\n",rec.size());
	for(map<string,int > ::iterator it=rec.begin();it!=rec.end();++it)
	{
		printf("%s %d\n",it->first.c_str(),it->second);
	}
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值