#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
PAT (Advanced) 1034. Head of a Gang (30)
最新推荐文章于 2024-02-14 16:53:20 发布