博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1034. Head of a Gang (30)
阅读量:4151 次
发布时间:2019-05-25

本文共 2335 字,大约阅读时间需要 7 分钟。

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

#include 
#include
#include
#include
#include
#include
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
p;//disjoint setvector
call;map
name2id;map
id2name;set
name;int n, k;int realn;//number of distinct nodevoid 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
::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
allSet;//father and weight of set map
::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; 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
> 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<
<<" "<
<

 

转载地址:http://jaxti.baihongyu.com/

你可能感兴趣的文章
定时发短信(quartz框架,阿里大于)
查看>>
常用网址
查看>>
springmvc和mybatis整合(总结)
查看>>
string-boot详解
查看>>
El表达式详解
查看>>
shiro框架
查看>>
类的设计技巧
查看>>
java中跳出外循环或者跳出代码块的方法
查看>>
枚举的使用
查看>>
java的各种跳转总结
查看>>
ImageIO读图片和上传图片到OSS上的bug
查看>>
通过putty将本地文件上传到服务器
查看>>
merge 无效原因及解决方案
查看>>
MySQL数据库中时间设计
查看>>
微信相关的支付总结(微信扫码支付,公众号支付,提现(企业付款),小程序支付)
查看>>
SpringCloud 快速入门总结
查看>>
rabbitMq快速入门总结
查看>>
SpringMVC项目打包脚本
查看>>
SpringMVC项目启动脚本
查看>>
SpringBoot 项目打包脚本
查看>>