AcWing 1518. 团伙头目
原题链接
中等
作者:
leo123456
,
2020-08-26 10:19:12
,
所有人可见
,
阅读 328
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<unordered_map>
using namespace std;
int n,k;
unordered_map<string,vector<pair<string,int>>> g;
unordered_map<string,int> total;
unordered_map<string,bool> st;
int dfs(string ver,vector<string>& nodes)
{
st[ver]=true;
nodes.push_back(ver);
int sum=0;
for(auto edge:g[ver])
{
sum+=edge.second;
string cur=edge.first;
if(!st[cur]) sum+=dfs(cur,nodes);
}
return sum;
}
int main()
{
cin>>n>>k;
while(n--)
{
string a,b;
int t;
cin>>a>>b>>t;
g[a].push_back({b,t});
g[b].push_back({a,t});
total[a]+=t;
total[b]+=t;
}
vector<pair<string,int>> res;
for(auto item:total)
{
string ver=item.first;
vector<string> nodes;
int sum=dfs(ver,nodes)/2;
if(nodes.size()>2&&sum>k)
{
string boss=nodes[0];
for(string node:nodes)
if(total[boss]<total[node])
boss=node;
res.push_back({boss,nodes.size()});
}
}
sort(res.begin(),res.end());
cout<<res.size()<<endl;
for(auto item:res) cout<<item.first<<' '<<item.second<<endl;
return 0;
}