1034 Head of a Gang(30 分)

1034 Head of a Gang(30 分)

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

题目意思看不懂搜博客的

gang的定义就是人数 >2  并且总通话人数>k

求有多少个gang  并且还要求输出每个gang 里面通话时间最长的那个的名字还有这个gang 的人数

代码

#include
using namespace std;
int pre[20000];
mapmp;//这个已经没有用处了
mapgg; 
int n,k,x,score[20000];
setst;
struct Node
{
	int num; //这个祖先有多少人
	int score;//这个帮派的总通话数是多少; 
	Node(){num = 0;score = 0;}
}kk[20000];
struct cpu
{
	string s;
	int num;	
}s[2500];
bool cmp(cpu a,cpu b)
{
	return a.s < b.s;
}
vectorvec;
vectorv;
/*--------------------并查集----------------------*/ 
void init(){
	for(int i = 1; i <= n; i++)
		pre[i] = i ;
}
int find(int x)
{
	if (x == pre[x])
		return x;
	return pre[x] = find(pre[x]);
}
void merge(int x,int y)
{
	int fx = find(x);
	int fy = find(y);
	if(fx != fy)
		pre[fx] = fy; 
}

int main() {
	scanf("%d%d",&n,&k);
	init();
	k = k * 2 ; //因为我重复算最后分数的原因,所以说要*2 贪图方便 
	char s1[5],s2[5];
	int num = 0 ; 
	for(int i = 1; i <= n; i++)
	{
		scanf("%s %s %d",s1,s2,&x);
		if(mp[s1] == 0)
			mp[s1] = ++num; //先离散化 
		score[mp[s1]] += x; //将他们的分数都加进去 
		gg[mp[s1]] = s1;    //因为最后还要输出他们的字符串 所以还要gg反向存一次 
		
		if(mp[s2] == 0)
			mp[s2] = ++num;
		score[mp[s2]] += x;
		gg[mp[s2]] = s2;
		
		merge(mp[s1],mp[s2]);//找到他们共同的祖先之后 
	}
	
	for(int i = 1; i <= num; i++)
	{
		find(i);//先把他们的祖先都筛选出来 
		kk[pre[i]].num++; //祖先人数相加 
		kk[pre[i]].score += score[i]; //其次就是共同祖先的分数  
	} 

	int cnt = 0 ;
	for(int i = 1 ; i <= num; i++)
	{
		if(kk[i].num > 2 &&kk[i].score > k) //如果说是人数 > 2 并且说分数 > k 
		{
			cnt++;
			st.insert(i);			
		}	
	} 
	printf("%d\n",cnt);  
	set::iterator it = st.begin();//祖先的人数已经知道了
	while(it!=st.end())
	{
		int maxn = -0x3f3f3f3f,u = -1;
		for(int i = 1; i <= num; i++)  //现在找最大的 
		{
			if(*it == pre[i])
			{
				if (score[i] > maxn)
				{
					maxn = score[i];
					u = i;
				} 
			}
		}
		v.push_back({gg[u],kk[pre[u]].num});//找到最大的那个家伙了 
		it++;
	}
	sort(v.begin(),v.end(),cmp);
	for(int i = 0 ; i < v.size();i++)
		cout<

 

你可能感兴趣的:(PAT甲级题)