九度1446 Head of a gang

题目描述:

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


代码如下:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<map>
#include<cctype>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;

/*此题过了PAT,但是没过九度。。。*/

map<string, int> m;//姓名和序号的映射
map<int, string> m2;//序号和姓名的映射,用于输出时根据序号找姓名
int total_weight[2010];//记录一个gang的总权重
int total_num[2010];//记录一个gang的总人数
int personal_weight[2010];//记录每个人打的电话总时间
int Tree[2010];//并查集父节点
struct H
{
	string name;
	int number;
	bool operator < (const H &b) const
	{
		return name < b.name;
	}
}header[2010];

int findroot(int x)
{
	if (Tree[x] == -1)
		return x;
	else
	{
		int tmp = findroot(Tree[x]);
		Tree[x] = tmp;
		return tmp;
	}
}

int main()
{
	int n, k;
	while (~scanf("%d%d", &n, &k))
	{
		m.clear();
		m2.clear();
		string name1, name2;
		int time;
		int idx = 0;
		int idxa, idxb;
		memset(personal_weight, 0, sizeof(personal_weight));
		memset(total_weight, 0, sizeof(total_weight));
		for (int i = 0; i < 2010; i++)
		{
			total_num[i] = 1;
		}
		memset(Tree, -1, sizeof(Tree));
		for (int i = 0; i < n; i++)
		{
			cin >> name1 >> name2 >> time;
			//cout << name1 << " " << name2 << " " << time << endl;
			if (m.find(name1) == m.end())//map中尚无对name1的映射
			{
				idxa = idx;
				m2[idx] = name1;
				m[name1] = idx++;
			}
			else
				idxa = m[name1];
			if (m.find(name2) == m.end())
			{
				idxb = idx;
				m2[idx] = name2;
				m[name2] = idx++;
			}
			else
				idxb = m[name2];
			//printf("idxa=%d,idxb=%d\n", idxa, idxb);
			personal_weight[idxa] += time;
			personal_weight[idxb] += time;
			idxa = findroot(idxa);
			idxb = findroot(idxb);
			if (idxa != idxb) //将两个并查集合并
			{
				Tree[idxb] = idxa;
				total_num[idxa] += total_num[idxb];
				total_weight[idxa] += (total_weight[idxb] + time);
			}
			else//就算已经在一个并查集里面了,总权重也要加time!!
				total_weight[idxa] += time;

		}
		/*for (int i = 0; i < idx; i++)
		{
			cout << m2[i] << " ";
			printf("%d %d %d %d\n", personal_weight[i], total_num[i], total_weight[i],Tree[i]);
		}*/
		int ans = 0; //the number of headers
		for (int i = 0; i < idx; i++)
		{
			//printf("ans=%d\n", ans);
			if (Tree[i] == -1 && total_weight[i] > k && total_num[i] > 2)//满足一个gang的3个条件的并查集的根节点
			{
				/*因为在findroot中添加了路径压缩,因此一个并查集中的所有节点的父节点都是同一个root,而对应的
				total_num和total_weight也都存在root中,所以可以直接这样找到*/
				header[ans].number = total_num[i];
				int max = personal_weight[i];//
				int pt = i;//这两步非常重要!因为下面的遍历中不会再找到i节点(Tree[i]==-1),这里不初始化为i的话,就会WA。。。
				for (int j = 0; j < idx; j++)
					if (Tree[j] == i && personal_weight[j] > max)
					{
						max = personal_weight[j];
						pt = j;
					}
				header[ans].name = m2[pt];//根据map2找对应序号的姓名
				ans++;//gang数
				
			}
		}
		if (ans == 0)
		{
			printf("0\n");
			continue;
		}
		else
		{
			printf("%d\n", ans);
			sort(header, header + ans);
			for (int i = 0; i < ans; i++)
				cout << header[i].name << " " << header[i].number << endl;
		}
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值