您的位置:首页 > 其它

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

2014-08-29 11:37 344 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: