#1034. Head of a Gang【DFS + 并查集】

原题链接

Problem Description:

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 A A and B B B, we say that A A A and B B 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 K 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 N N and K K K (both less than or equal to 1000), the number of phone calls and the weight threthold, respectively. Then N N 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

Problem Analysis:

题目大致意思是给定一系列的关系(具有传递性),并且每个点都有点权,让我们求出每个连通块内点权最大的那个点(称为“头目”),并且让我们输出符合一定条件(团伙人数不少于 3 3 3 人,且连通块总权值和大于 k k k )的所有连通块及其连通块内的点数,并按照头目的字典序进行输出。

由于题目说明了,所有人的 ID 都是三个大写英文字母组成的,因此我们可以将其转化为 26 26 26 进制的整数,这样可以保证编号不会有重复。

对于每一个连通块内的头目,可以用 DFS 遍历整个连通块得到:

int dfs(int u) // 返回这个连通块具有的最大的权重点数的编号,当前搜索到的具有最大权重点数的编号
{
	st[u] = true;
	int maxid = u;
	for (int i = h[u]; ~i; i = ne[i])
	{
		int j = e[i];
		if (!st[j])
		{
			int tmpid = dfs(j);
			if (w[tmpid] > w[maxid]) maxid = tmpid;
		}
	}
	return maxid;
}

至于每个连通块的权值和,由于在读入每个点的点权时,我们并不能知道其具体属于哪个连通块,因此我们可以将其用 pair<pair<int, int>, int> 存入,然后离线处理:

for (auto e : edge)
{
	int p = find(e.first.first), t = e.second;
	total[p] += t;
}

注意,这里合并点时,点的祖先并不一定是团伙中的头目,本题的做法是先处理完并查集,再用 DFS 遍历每一个连通块寻找其头目。

Code

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

typedef pair<int, int> PII;
typedef pair<pair<int, int>, int> PIII;

const int N = 17576;

int n, k; // n组条件,权重阈值k
int p[N], w[N], siz[N], total[N];
int h[N], e[N], ne[N], idx;
bool st[N];
PII ans[N];
int cnt1, cnt2;
PIII edge[N];

void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

int get_c(int a, int k)
{
	int res = 1;
	for (int i = 0; i < k; i ++ )
		res *= a;
	return res;
}

int get_number(string name)
{
	int res = 0;
	for (int i = 0; i < name.size(); i ++ )
	{
		res += (name[i] - 'A') * get_c(26, abs(i - 2));
	}
	return res;
}

int find(int x)
{
	if (p[x] != x) p[x] = find(p[x]);
	return p[x];
}

string get_name(int id)
{
	string name = "";
	while (id)
	{
		name += 'A' + id % 26;
		id /= 26;
	}
	if (name.size() < 3)
	{
		int len = name.size();
		for (int i = 0; i < 3 - len; i ++ )
			name += 'A';
	}
	reverse(name.begin(), name.end());
	return name;
}

int dfs(int u) // 返回这个连通块具有的最大的权重点数的编号,当前搜索到的具有最大权重点数的编号
{
	st[u] = true;
	int maxid = u;
	for (int i = h[u]; ~i; i = ne[i])
	{
		int j = e[i];
		if (!st[j])
		{
			int tmpid = dfs(j);
			if (w[tmpid] > w[maxid]) maxid = tmpid;
		}
	}
	return maxid;
}

int main()
{
	for (int i = 0; i <= 17575; i ++ ) p[i] = i, siz[i] = 1; // 初始化并查集
	memset(h, -1, sizeof h);

	cin >> n >> k;
	for (int i = 0; i < n; i ++ )
	{
		string a, b;
		int t;
		cin >> a >> b >> t;
		int ida = get_number(a), idb = get_number(b);
		w[ida] += t, w[idb] += t;
		add(ida, idb), add(idb, ida);

		edge[cnt2 ++ ] = {{ida, idb}, t};
		
		int pa = find(ida), pb = find(idb);

		if (pa != pb) 
		{
			p[pa] = pb;
			siz[pb] += siz[pa];
		}
	}

	for (auto e : edge)
	{
		int p = find(e.first.first), t = e.second;
		total[p] += t;
	}

	for (int i = 0; i <= 17575; i ++ )
		if (!st[i] && w[i]) // 只遍历所有有点权的,并且没有被遍历过的
		{
			int maxid = dfs(i);
			int p = find(maxid);

			//cout << get_name(maxid) << ' ' << total[p] << endl;

			if (siz[p] >= 3 && total[p] > k)
				ans[cnt1 ++ ] = {maxid, siz[p]};
		}
	
	cout << cnt1 << endl;
	if (cnt1)
	{
		sort(ans, ans + cnt1);
		for (int i = 0; i < cnt1; i ++ )
			cout << get_name(ans[i].first) << ' ' << ans[i].second << endl;
	}
	return 0;
}
上一篇:vue项目之数量占比进度条实现


下一篇:C Primer Plus 6 第十四章 习题6