PAT (Advanced) 1034. Head of a Gang (30)

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

map<string, vector<string> > calls;
map<string, bool> visit;
map<string, int> weight;
map<string, int> result;

int cnt, totalweight;
string head;

void dfs(string str)
{
	visit[str] = true;
	cnt++;
	totalweight += weight[str];
	if (weight[str] > weight[head] || weight[str] == weight[head] && str < head)
		head = str;
	for (auto it = calls[str].begin(); it != calls[str].end(); ++it)
	{
		if (visit[*it] == false)
			dfs(*it);
	}
}

int main()
{
	int n, k, time;
	string name1, name2;
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> name1 >> name2 >> time;
		weight[name1] += time;
		weight[name2] += time;
		calls[name1].push_back(name2);
		calls[name2].push_back(name1);
		visit[name1] = false;
		visit[name2] = false;
	}

	for (auto it = visit.begin(); it != visit.end(); ++it)
	{
		if (visit[it->first] == false)
		{
			cnt = 0;
			totalweight = 0;
			head = it->first;
			dfs(it->first);
			if (cnt > 2 && totalweight / 2 > k)
				result[head] = cnt;
		}
	}
	cout << result.size() << endl;
	for (auto it = result.begin(); it != result.end(); ++it)
		cout << it->first << " " << it->second << endl;
}
参考: http://blog.csdn.net/matrix5467/article/details/8641186
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值