https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624
首先不难想到的是并查集,不过这里有一个关键的一点就是是字符串不是数字。
所以可以用哈希表来将字符串和数字编号来一一对应的映射起来。
其它的就是基本的并查集即可。

#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
struct node
{string name;int cnt;
}Node;
bool cmp(node a,node b) {return a.name<b.name;}
vector<node>ans;//答案
int p[N],cnt[N],sum[N],val[N],idx=1;//sum就是一个集合总的值,val是每一个人单独的值
int n,k;
unordered_map<string,int>hush1;//字符串到编号的映射
unordered_map<int,string>hush2;//编号到字符串的映射
unordered_map<int,vector<int>>mp;//每一个集合
int find(int x)
{if(x!=p[x]) p[x]=find(p[x]);return p[x];
}
int main(void)
{cin>>n>>k;for(int i=1;i<=n;i++) p[i]=i,cnt[i]=1;for(int i=1;i<=n;i++){string a,b;int t; cin>>a>>b>>t;if(!hush1.count(a)) hush1[a]=idx++;//映射成唯一的编号hush2[hush1[a]]=a;if(!hush1.count(b)) hush1[b]=idx++;hush2[hush1[b]]=b;if(find(hush1[a])!=find(hush1[b]))//不再同一个集合{cnt[find(hush1[b])]+=cnt[find(hush1[a])];sum[find(hush1[b])]+=sum[find(hush1[a])]+t;val[hush1[a]]+=t;val[hush1[b]]+=t;p[find(hush1[a])]=find(hush1[b]);}else //在同一个集合{val[hush1[a]]+=t;val[hush1[b]]+=t;sum[find(hush1[b])]+=t;}}for(int i=1;i<=n;i++) mp[find(i)].push_back(i);//将其每一个值都存到对应的集合里for(auto i=mp.begin();i!=mp.end();i++)//枚举所有的集合{auto temp=i->second;if(temp.size()<3) continue;//人数不够if(sum[i->first]<=k) continue;//总分不够int index=0;for(int j=0;j<temp.size();j++)//找到集合中个人分最大的值{if(val[temp[j]]>val[temp[index]]) index=j;}Node.name=hush2[temp[index]];Node.cnt=temp.size();ans.push_back(Node);}cout<<ans.size()<<endl;sort(ans.begin(),ans.end(),cmp);for(int i=0;i<ans.size();i++) cout<<ans[i].name<<" "<<ans[i].cnt<<endl;return 0;
}

1034 Head of a Gang (30 分) 【难度: 中 / 知识点: 并查集】相关推荐

  1. 1107 Social Clusters (30 分)【难度: 中 / 知识点: 并查集】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805361586847744 很不错的一个并查集的操作.你会发现我们需要处 ...

  2. 1114 Family Property (25 分)【难度: 中/ 知识点: 并查集】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805356599820288 挺好的一个并查集,先读入然后再处理.注意:在 ...

  3. PAT:1034 Head of a Gang (30分)

    1034 Head of a Gang (30分) One way that the police finds the head of a gang is to check people's phon ...

  4. PAT甲级1107 Social Clusters (30 分):[C++题解]并查集,爱好、人数

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 凭爱好,分人群.注意点:爱好可传递.什么意思?意思是A和B的有共同爱好1, B和C有共同爱好2,那么认为A和C也是同一群人. 按照爱 ...

  5. 1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people‘s pho

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 One way that the police finds the head of a gang is to check peop ...

  6. 1034 Head of a Gang (30 分)

    题目 题目链接 题解 并查集. 注意坑点:帮派的人数必须大于2. 代码 #include<bits/stdc++.h> using namespace std; #define PSI p ...

  7. pat 甲级 1034. Head of a Gang (30)

    1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...

  8. 【PAT - 甲级1034】Head of a Gang (30分)(并查集)

    题干: One way that the police finds the head of a gang is to check people's phone calls. If there is a ...

  9. 分珠(dfs+并查集)

    1140 分珠 时间限制:500MS  内存限制:65536K 提交次数:24 通过次数:18 题型: 编程题   语言: G++;GCC Description 如下图所示,有若干珠子,每颗珠子重量 ...

最新文章

  1. Wince 启动程序并最小化
  2. 内存管理范围和@property
  3. NOIP模拟测试19「count·dinner·chess」
  4. 两台linux之间互传php脚本,linux下两台服务器实现同步的方法
  5. java web mysql 登陆_javaweb登录功能实现,eclipse开发工具,mysql作为数据库
  6. linux中 字符串,linux内核驱动中对字符串的操作
  7. Matlab 之GUIDE界面设计:不同函数之间使用相同变量
  8. MongoDB可视化工具Robomongo 和 Mongochef
  9. c语言题库及详解答案 免费下载,C语言题库(带详解答案).pdf
  10. mac 安装ffmpeg以及各种编码器
  11. 计算小于100的最大素数
  12. 分治策略-股票获取最大收益-最大子数组问题
  13. 中移动Open NFV实验室迎新合作伙伴
  14. 静态网页的广告banner部分
  15. 面试题——正则表达式
  16. NopCommerce 在Category 显示Vendor List列表
  17. L1正则化与L2正则化
  18. 短视频系统开发时如何实现在iOS系统相册中选取视频
  19. javascript之雪花特效
  20. ORACLE (5): buffer cache(CBC LATCH实验)

热门文章

  1. JavaScript优化基本篇
  2. Linux cron 定时执行任务
  3. mysql odbc 配置详解
  4. SQL语句like子句中的转义符
  5. ubuntu在不重装系统情况下关于调整分区,或是从现有根目录划分/home等分区
  6. 网页变灰配合全国哀悼日的css代码
  7. 用Visual C#开发简单的复合控件
  8. 瑞友天翼4.0在XP下安装注意事项
  9. VC中退出应用程序-几种很有用的方法
  10. IAP升级功能编写初期的一些困惑与疑问---完成功能后的总结