题目1446:Head of a Gang 九度OJ

题目1446:Head of a Gang

时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:2655

解决:609

题目描述:

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.

输入:

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.

输出:

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.

样例输入:
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
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
样例输出:
2
AAA 3
GGG 3
0
来源:
2012年浙江大学计算机及软件工程研究生机试真题
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define MAX 2020
using namespace std;
int tree[MAX];//并查集数组
string name[MAX];//用来存名字
int weight[MAX];//记录对应guy的通话时长
int size;
void clearStr(){//初始化name[]数组
	size=0;//总人数为0
	for(int i=0;i<MAX;i++){
		name[i]="";//置空
	}
}

int findStr(string &a){//查找该名字的编号
	if(size==0){//如果还没有录入名字(名字数组为空),则分配编号1 给该名字
		size++;
		name[size]=a;
		return size;
	} else{     //名字数组里有名字
		int flag=0;
		for(int i=1;i<=size;i++){//先遍历名字数组
			if(name[i]==a){    //如果已经对该名字进行了记录,则返回编号
				flag=1;
				return i;
			}
		}
		if(flag==0){  //如果名字数组里面,没有该名字的记录,则在名字数组尾部添加该名字
			size++;
			name[size]=a;
			return size;
		}
	}
	return 0;
}

int findRoot2(int x){//并查集求老大函数,本来有递归方法的,但是在这里也试一试循环方法吧。
	int reg=x;
	while(tree[x]!=-1)
		x=tree[x];

	int root=x;
	while(tree[reg]!=-1){
		int j=tree[reg];
		tree[reg]=root;
		reg=j;
	}
	return root;
}
struct gang{        //用来记录 团伙 老大 和  人数
	string name;    //老大名字
	int member;     //团伙人数
	void reset(){   //重置团伙结构体
		name="";
		member=0;
	}
}gangs[MAX];

bool cmp(struct gang a,struct gang b){
	return a.name<b.name;
}
int main(){
	int n,k;
	while(scanf("%d%d",&n,&k)!=EOF){
		clearStr();
		memset(weight,0,sizeof(weight));
		for(int i=0;i<MAX;i++){
			tree[i]=-1;
		}
		for(int i=0;i<MAX;i++){
			gangs[i].reset();
		}
		string name1,name2;
		int a,b,time;
		while(n--){
			cin>>name1>>name2>>time;
			a=findStr(name1);         //找名字 name1 的编号
			b=findStr(name2);         //找名字 name2 的编号
			weight[a]+=time;          //记录名字 name1 的通话时长
			weight[b]+=time;          //记录名字 name2 的通话时长
			a=findRoot2(a);
			b=findRoot2(b);
			if(a!=b){
				tree[a]=b;            //合并 团伙 集合 
			}
		}
		for(int i=1;i<=size;i++){
			findRoot2(i);             //再一次 压缩 并查集
		//	cout<<"i :"<<i<<" name: "<<name[i]<<"weight :"<<weight[i]<<" tree: "<<tree[i]<<endl;

		}

		int ans=0;
		for(int i=1;i<=size;i++){
			if(tree[i]==-1){
//				cout<<"i :"<<i<<endl;
				int number=0;
				int sum=weight[i],cmpweight=weight[i];
				string head=name[i];
				for(int j=1;j<=size;j++){
					if(tree[j]==i){//如果是 name[i]领队的团伙 成员
						number++;  //成员数
						sum+=weight[j];  //总通话数
						if(weight[j]>cmpweight){  //记录下通话时长最长的成员
							head=name[j];
							cmpweight=weight[j];
						}
					}
				}
				if(number<2||(sum/2)<=k){  //团伙人数必须大于2人,总通话时长必须大于阈值
					continue;
				}
				gangs[ans].member=number+1;
				gangs[ans].name=head;
				ans++;
			}
		}
		sort(gangs,gangs+ans,cmp);
		printf("%d\n",ans);
		for(int i=0;i<ans;i++){
			cout<<gangs[i].name<<" "<<gangs[i].member<<endl;
		}

	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值