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

解题思路:

窝点的数量可以通过深度优先遍历寻找连通分量,符和条件的连通分量就是窝点

为了方便筛选头目,我们可以把整个图看成无向边,每次把边权分别加到两个结点上去,这样最后就得到了每个结点的通话总时长,时长最长的那个结点就是头目,如果整个连通分量的权值>K*2(因为加了两遍)且结点数>2,那么这个连通分量就是窝点

这里有一个坑点,就是结点的存储问题,我一开始认为结点是3个相同的大写字母组成,结果有可能出现ABB和BAA这样的情况,如果用哈希存储,就会出错(ABB和BAA哈希值一样),所以这里要用MAP来存

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <string.h>
#include <map>
using namespace std;
const int MAXN = 5320;
string hashMap[MAXN];
vector<int> guys;
map<string, int> headList;

map<int, string> inputNodeToName;
map<string, int> inputNameToOrder;

int members = 0;
int weightSum = 0, perSum = 0;
string head;
int N, K;

class HGraph {
public:
	int n;
	vector<int>* edges;
	int* weight;
	bool* visited;
public:
	HGraph(int _n) {
		n = _n;
		edges = new vector<int>[n];
		weight = new int[n];
		visited = new bool[n];
		memset(visited, 0, n);
		memset(weight, 0, n*sizeof(int));
	}
	//单向边
	void HInsert(int x, int y, int weigh) {
		edges[x].push_back(y);
		edges[y].push_back(x);
		weight[x] += weigh;
		weight[y] += weigh;
	}
	void DFS_GANG(int root) {
		visited[root] = true;
		members++;
		weightSum += weight[root];
		if (weight[root] > perSum) {
			perSum = weight[root];
			head = inputNodeToName[root];
		}
		for (auto adj_vertex : edges[root]) {
			if (!visited[adj_vertex]) {
				DFS_GANG(adj_vertex);
			}
		}
	}
	void DFS_ENTER() {
		int clusters = 0;
		for (auto root : guys) {
			if (!visited[root]) {
				members = 0;
				weightSum = 0;
				perSum = 0;
				head.clear();
				DFS_GANG(root);	
				if (weightSum > K*2 && members > 2) {
					clusters++;
					headList[head] = members;
				}
			}
		}
		cout << clusters << endl;
		for (auto itr = headList.begin(); itr != headList.end(); ++itr) {
			cout << itr->first << " " << itr->second << endl;
		}
	}
};
int main() {
	cin >> N >> K;
	HGraph HG(MAXN);
	int index = 0;
	for (int i = 0; i < N; ++i) {
		string x, y;
		int time;
		cin >> x >> y >> time;
		if (!inputNameToOrder[x]) {
			inputNameToOrder[x] = ++index;
			inputNodeToName[index] = x;
			guys.push_back(index);
		}
		if (!inputNameToOrder[y]) {
			inputNameToOrder[y] = ++index;
			inputNodeToName[index] = y;
			guys.push_back(index);
		}
		HG.HInsert(inputNameToOrder[x], inputNameToOrder[y],time);
	}
	HG.DFS_ENTER();
	system("PAUSE");
	return 0;
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值