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

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

考察点:并查集的灵活运用

题意:

给出一组人的通话记录(两个参与人以及时长), 一个帮派的定义是其中的一个簇,它至少有2个人且它们的总共通话时间超过阈值K,其中通话时间最长的是帮派的老大。求出每个帮派的人数以及老大。

分析(问题抽象):

直观地看,通话记录可以看成图,这是一个帮派就是一个连通分支,人数是这个连通分支的结点数,总通话时间是分支内路径的权重和。帮派内拥有最多边权重的结点是老大。

因此Tree[]记录父亲结点(对于根节点Tree[root]==-1;初始化为-1,即每个结点都是根节点)。在并查集的基础上,num[]记录每个帮派的人数(初始化为1,每个帮派都是1人,当两个簇合并的时候需要更新),total[]记录每个帮派的通话时长(初始化为0,当两个簇合并的时候需要更新)。

注意到最终每个帮派的根节点,并不一定是老大(通话时长最多的)。因此还需要维护数组w[]记录每个人的通话时间。在最终判断的时候,找出每个分支的人,进而找出老大。

注意点:

1. memset不可以将数组初始化为1,只能初始化为0(内存为0x00000000)或者-1(内存为0xffffffff)或者ASCii为0~255的字符。

2. 利用数组和map<string,int>mp 实现字符和数组的相互转换。

3. 在遍历边的时候注意,代码中Tree[na]=nb意味着结点nb是na的父节点了,因此更新时total[]和num[]都应该加在nb上。

并查集的题目都可以用DFS完成,参见https://blog.csdn.net/richenyunqi/article/details/79575328

#include<iostream>
#include<cstring>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
const int MAX = 2007;
int Tree[MAX];
int w[MAX];//每个成员的权重 
int total[MAX];//帮派的权重 
int num[MAX];//帮派人数 
int findRoot(int x){
	if(Tree[x]==-1) return x;
	else{
		int temp = findRoot(Tree[x]);
		Tree[x] = temp;
		return temp;
	}
} 
struct Gang{
	string headName;int n;
	Gang(string h,int num):headName(h),n(num){}
	bool operator < (const Gang &other) const{
		return headName<other.headName;
	}
};
string name[MAX];
map<string,int> mp;//用于编号和姓名之间相互转换 
int main(void){
	int N,K;cin>>N>>K;
	for(int i=1;i<=MAX;i++){//memset不可以赋值1 !!! 
		num[i]=1;
	}
	memset(Tree,-1,sizeof(Tree));
	memset(w,0,sizeof(w));
	memset(total,0,sizeof(total)); 
	string s1,s2;int time;
	int idx = 1;//从1开始编号 
	while(N--){
		cin>>s1>>s2>>time;
		int a,b;
		if(mp.find(s1)==mp.end()){//如果没有出现过这个名字,赋予它数字编号 
			a = idx;
			mp.insert({s1,a});
			name[a]=s1;
			idx++;
		}
		else a = mp[s1];//否则直接从map中获得数字编号 
		if(mp.find(s2)==mp.end()){
			b = idx;
			mp.insert({s2,b});
			name[b]=s2;
			idx++;
		}
		else b = mp[s2];
		w[a]+=time; w[b]+=time;
		int na = findRoot(a);//返回根节点的编号,应当清楚Tree[na/nb] 
		int nb = findRoot(b);
		if(na!=nb){ 
			Tree[na] = nb;//na的父节点是nb 
			total[nb]+=(total[na]+time);//因此总的时间要加到total[nb]上 
			num[nb]+=num[na];//同上 
		}
		else{
			total[nb]+=time; 
		}
	}
	vector<Gang> G; 
	for(int i=1;i<=idx;i++){
		if(Tree[i]==-1&&total[i]>K&&num[i]>2){//如果帮派满足要求 
			int head = i;int maxCall = w[i];
			for(int j=1;j<=idx;j++){//寻找老大 
				if(findRoot(j)==i&&w[j]>maxCall){
					maxCall = w[j];
					head = j;
				}
			}
			G.push_back(Gang(name[head],num[i]));
		}
	}
	sort(G.begin(),G.end());
	cout<<G.size()<<endl;
	for(int i=0;i<G.size();i++){
		cout<<G[i].headName<<" "<<G[i].n<<endl;
	} 
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值