九度OJ题目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 wit

h 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

思路:

1.使用并查集来统计连通分量的个数以及每个集合中的结点的个数

2.使用map完成名字与结点编号的映射,即图的抽象,同时要思考如何已知结点编号输出姓名

3.难点是理解每个节点的总的通信时间的计算(相邻结点间的权值,a,b和b,a的要重复加上);

每个集合的总的通性时间的计算和记录(所有边的权值)。
参考代码:

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
using namespace std;
#define N 1001

/*
编程中出现的小问题:
1.求一个集合中的总的通信时间时没搞清楚应该加哪些值
2.cnt[i]=1,初始化而不等于0
3.在合并集合时,谁是树根结点搞混掉
4.粗心,输入忘记写&,等号==
*/
map<string,int> ma;//结点名与编号的映射
int Tree[N];//使用双亲表示法存储所有节点
int cnt[N];//cnt[i]表示i为根节点的集合中的节点的个数
int weight[N];//weight[i]表示i结点的通信时间
int sum[N];//sum[i]表示以i为根节点的总的通信时间,
struct E {
	char name[5];
}tem[N];//存储某结点编号的名字,方便输出用的
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;
	char s[5], s2[5];
	int w;
	string a, b;
	while (scanf("%d%d", &n, &k) != EOF) {
		ma.clear();
		/*memset(Tree, -1, sizeof Tree);
		memset(cnt, 0, sizeof cnt);
		memset(weight, 0, sizeof weight);*/
		for (int i = 1; i <= n; i++) {//初始化
			Tree[i] = -1;
			cnt[i] = 1;
			weight[i] = 0;
			sum[i] = 0;
		}

		int idx = 1;
		for (int i = 1; i <= n; i++) {
			scanf("%s%s%d", s, s2, &w);
			a = s; b = s2;
			int ia, ib;
			if (ma.find(a) == ma.end()) {
				ia = idx;
				ma[a] = idx++;
				strcpy(tem[ia].name, s);
			}
			else {
				ia = ma[a];
			}
		
			if (ma.find(b) == ma.end()) {
				ib = idx;
				ma[b] = idx++;
				strcpy(tem[ib].name, s2);
			}
			else {
				ib = ma[b];
			}
			weight[ia]+= w;
			weight[ib] += w;
			//printf("%s,%d,%s,%d\n", tem[ia].name, ia, tem[ib].name, ib);
			ia = findRoot(ia);
			ib = findRoot(ib);
			if (ia != ib) {
				Tree[ib] = ia;
				cnt[ia] += cnt[ib];	
			}
			sum[ia] += w;//一个分量的总的通信时间
		}
		/*for (int i = 1; i <= n; i++) {
              printf("%d ", weight[i]);			  
		}	
		printf("\n");
		for (int i = 1; i <= n; i++) {
			if (Tree[i] == -1)
				printf("%d,%d,%d\n", i, sum[i],cnt[i]);
		}*/
		//统计满足条件的集合数
		int ans = 0;
		for (int i = 1; i <= n; i++) {
			if (Tree[i] == -1 && cnt[i] > 2&&sum[i]>k) {
				ans++;
			}
		}
		printf("%d\n", ans);

		//找出满足条件的集合中的首领和集合中元素的个数
		for (int i = 1; i <= n; i++) {
			if (Tree[i] == -1 && cnt[i] > 2 && sum[i]>k) {
				int max = weight[i];
				int head = i;
				for (int j = 1; j <= n; j++) {
					if (Tree[j] == i&&weight[j] > max) {
						max = weight[j];
						head = j;
					}
				}
				printf("%s %d\n",tem[head].name,cnt[i]);
			}
		}
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值