1034. Head of a Gang (30)

考察并查集,以及每个集合的信息记录

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <algorithm>
using namespace std;

typedef struct Node
{
	int weight, parent;
}Node;
typedef struct Call
{
	string a, b;
	int t;
}Call;
typedef struct Gang
{
	int head, num, sum, maxw;
	Gang(){head=-1;num=0;sum=0;maxw=-1;}
};
vector<Node> p;//disjoint set
vector<Call> call;
map<string, int> name2id;
map<int, string> id2name;
set<string> name;
int n, k;
int realn;//number of distinct node
void InitSet()
{
	p.resize(realn);
	for (int i = 0; i < realn; ++i)
	{
		p[i].parent = i; p[i].weight = 0;
	}
}
void CompressSet(int top, int x)
{
	if (top != p[x].parent)
	{
		CompressSet(top, p[x].parent);
		p[x].parent = top;
	}
}
int FindSet(int x)
{
	if (x != p[x].parent)
	{
		int top = FindSet(p[x].parent);
		CompressSet(top, x);
	}
	return p[x].parent;
}
void UnionSet(int x, int y)
{
	int a = FindSet(x);
	int b = FindSet(y);
	p[a].parent = b;
}
int main()
{
	while(scanf("%d%d",&n,&k)!=EOF)
	{
		call.resize(n);
		name.clear();
		for (int i = 0; i < n; ++i)
		{
			cin>>call[i].a; cin>>call[i].b; cin>>call[i].t;
			name.insert(call[i].a); name.insert(call[i].b); 
		}
		//get the person number
		realn = name.size();
		name2id.clear();
		id2name.clear();
		set<string>::iterator it1;
		int id = 0;
		for (it1=name.begin(); it1!=name.end(); it1++,++id)
			name2id[*it1] = id, id2name[id]=*it1;
		//build disjoint set
		InitSet();
		for (int i = 0; i < call.size(); ++i)
		{
			int u = name2id[call[i].a]; int v = name2id[call[i].b]; int t = call[i].t;
			//printf("%d %d %d\n", u, v, t);
			p[u].weight += t; p[v].weight += t;
			UnionSet(u, v);
		}
		//collect all set
		map<int,Gang> allSet;//father and weight of set
		map<int,Gang>::iterator it;
		for (int i = 0; i < realn; ++i)
		{
			int top = FindSet(i);
			it = allSet.find(top);
			if (it != allSet.end())
			{
				allSet[top].sum += p[i].weight;
				allSet[top].num++;
				if (p[i].weight > allSet[top].maxw)
				{
					allSet[top].head = i;
					allSet[top].maxw = p[i].weight;
				}
				
			}
			else
			{
				Gang tmp;
				tmp.head = i; tmp.maxw = p[i].weight; 
				tmp.num = 1;  tmp.sum = p[i].weight;
				allSet[top] = tmp;
			}
		}
		//threthold gang
		std::vector<Gang> gang;
		for (it = allSet.begin(); it!=allSet.end(); it++)
			if (it->second.sum > k*2 && it->second.num > 2) 
				gang.push_back(it->second);
		//output
		vector<pair<string, int>> head;
		for (int i = 0; i < gang.size(); ++i)
			head.push_back(make_pair(id2name[gang[i].head],gang[i].num));
		sort(head.begin(), head.end());
		printf("%d\n",head.size());
		for (int i = 0; i < head.size(); ++i)
			cout<<head[i].first<<" "<<head[i].second<<endl;
	}
	return 0;
}


 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI记忆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值