PAT_Advanced Level_1034 Head of a Gang (C++_dfs_邻接矩阵)

丶灬走出姿态 提交于 2020-03-01 09:32:26

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

Process

刚开始是打算用并查集的,一边输入一边查,后来就发现不行,因为在输入的时候数值还在改变,后来就说是先输入都存起来之后再用并查集刷一遍,后来还是有两个点过不了,无奈之下就换了dfs,简单+好用+正确率高!
还是第一次用邻接矩阵存图emmm………

Code

#include<bits/stdc++.h>
using namespace std;
int n, k, cnt;
int f[2010][2010], vis[2010], v[2010];
string s[2010];
map<string, int>m, ans;
//map<int, string>s;
void dfs(int cur, int& head, int& member, int& sum)
{
	vis[cur] = 1;
	member++;
	if (v[cur] > v[head])
		head = cur;
	for (int i = 1; i <= cnt; i++)
		if (f[cur][i] == 1 && vis[i] == 0)
		{
			sum += v[i];
			dfs(i, head, member, sum);
		}
}
int main()
{
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		string t1, t2;
		int t;
		cin >> t1 >> t2 >> t;
		if (m[t1] == 0)
		{
			m[t1] = ++cnt;
			s[cnt] = t1;
		}
		if (m[t2] == 0)
		{
			m[t2] = ++cnt;
			s[cnt] = t2;
		}
		v[m[t1]] += t;
		v[m[t2]] += t;
		f[m[t1]][m[t2]] = 1;
		f[m[t2]][m[t1]] = 1;
	}
	for(int i=1;i<=cnt;i++)
		if (vis[i] == 0)
		{
			int head = i, member = 0, sum = v[head];
			dfs(i, head, member, sum);
			if (sum > 2 * k && member > 2)
				ans[s[head]] = member;
		}
	cout << ans.size() << endl;
	for (map<string, int>::iterator it = ans.begin(); it != ans.end(); it++)
		cout << it->first << " " << it->second << endl;
	return 0;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!