码迷,mamicode.com
首页 > 其他好文 > 详细

1034. Head of a Gang (30)

时间:2017-10-28 21:10:30      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:cst   imu   cin   guarantee   ica   rom   mtime   print   mes   

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 threshold 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

解题思路:初看题目,求类别肯定用并查集,然后在数据处理上对应不上。看了map的用法后,还是很强大的STL。

#include<iostream>
#include <cstdio>
#include<map>
#include <vector>
#include <string>
using namespace std;
struct Node{
	char name1[10];
	char name2[10];
	int time; 
}node[1002];
struct Result{
	string name;
	int maxtime,sumtime,member;
	Result():maxtime(0),sumtime(0),member(0){
	}
};
map<int,Result>result;
map<string,int>name_time,union_tmp;
vector<int> index(2004);
int find(int a){
	return index[a] = ( index[a]==a ) ? a : find(index[a]);
}
void union_m(int a,int b){
	index[find(a)]=index[find(b)];
}
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	int i,j;
	char name1[5],name2[5];
	int time;
	for(i=0;i<n;++i){
		//scanf("%s%s%d",node[i].name1,node[i].name2,node[i].time);
		cin>>node[i].name1>>node[i].name2>>node[i].time;
		name_time[node[i].name1]+=node[i].time;
		name_time[node[i].name2]+=node[i].time;
	}
	int cnt=0;
	for(map<string,int>::iterator it=name_time.begin();it!=name_time.end();it++){
		index[cnt]=cnt;
		union_tmp[it->first]=cnt++;
	}
	for(i=0;i<n;i++){
		union_m(union_tmp[node[i].name1],union_tmp[node[i].name2]);
	}
	for(map<string,int>::iterator it=name_time.begin();it!=name_time.end();it++){
		int root=find(union_tmp[it->first]);
		if(result[root].maxtime<it->second){
			result[root].maxtime=it->second;
			result[root].name=it->first;
		}
		result[root].sumtime+=it->second;
		result[root].member+=1;
	}
	map<string,int>tmp;
	for(map<int,Result>::iterator it=result.begin();it!=result.end();it++){
		if(it->second.member>2 && it->second.sumtime/2 > k){
			tmp[it->second.name]=it->second.member;
		}
	}
	printf("%d\n",tmp.size());
	for(map<string,int>::iterator it=tmp.begin();it!=tmp.end();it++){
		printf("%s %d\n",it->first.c_str(),it->second);
	}
	return 0;
}

  

1034. Head of a Gang (30)

标签:cst   imu   cin   guarantee   ica   rom   mtime   print   mes   

原文地址:http://www.cnblogs.com/grglym/p/7747925.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!