1034

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zero_1778393206/article/details/87900899
#include<iostream>
#include<string>
#include<map>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
class Person
{
public:
	stack<string> friendname;
	int weight;
	bool flag;
};

class Ans
{
public:
	string name;
	int max;
	int sum;
	int num;
	bool operator<(const Ans& a)
	{
		return name < a.name;
	}
};

map <string, Person> P;
int n, k;
Ans ans;
vector<Ans> A;
void Find(string name);
int main()
{
	
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		string a, b;
		int t;
		cin >> a >> b >> t;
		if (P.find(a) != P.end())
		{
			P[a].weight += t;
			P[a].friendname.push(b);
		}
		else
		{
			P[a].flag = false;
			P[a].weight = t;
			P[a].friendname.push(b);
		}
		if (P.find(b) != P.end())
		{
			P[b].weight += t;
			P[b].friendname.push(b);
		}
		else
		{
			P[b].flag = false;
			P[b].weight = t;
			P[b].friendname.push(a);
		}
		
	}
	
	for (const auto& p : P)
	{
		///cout << p.first << ' ' << p.second.weight << endl;
		ans.max = 0;
		ans.num = 0;
		ans.sum = 0;
		if(!p.second.flag)
			Find(p.first);
		if (ans.sum / 2 > k&& ans.num > 2)
			A.push_back(ans);	
	}
	cout << A.size()<<endl;
	if (!A.empty())
	{
		sort(A.begin(),A.end());
		for (auto iter = A.begin(); iter != A.end(); iter++)
		{
			cout << iter->name << ' ' << iter->num << endl;
		}
	}
	system("pause");
	return 0;
}
void Find(string name)
{
	P[name].flag = true;
	ans.sum += P[name].weight;
	ans.num++;
	if (P[name].weight > ans.max)
	{
		ans.max = P[name].weight;
		ans.name = name;
	}
	while (!P[name].friendname.empty())
	{
		string friname;
		friname = P[name].friendname.top();
		P[name].friendname.pop();
		if (!P[friname].flag)
			Find(friname);
	}
}

猜你喜欢

转载自blog.csdn.net/zero_1778393206/article/details/87900899