用并查集分割团伙,判断输出~

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int father[maxn],isRoot[maxn]={},weight[maxn];
unordered_map<string,int> pos1;
unordered_map<int,string> pos2;
unordered_map<string,int> ans;
struct node {
string id;
int total=;
int num=;
}Node[maxn];
struct gang {
string id;
int num;
};
void init () {
for (int i=;i<maxn;i++)
father[i]=i;
}
int findfather (int x) {
int a=x;
while (x!=father[x])
x=father[x];
while (a!=father[a]) {
int z=a;
a=father[a];
father[z]=x;
}
return x;
}
void Union (int a,int b) {
int faA=findfather(a);
int faB=findfather(b);
if (faA!=faB) {
if (weight[faA]>weight[faB]) father[faB]=faA;
else father[faA]=faB;
}
}
bool cmp (gang a,gang b) {
return a.id<b.id;
}
int main () {
int N,K;
scanf ("%d %d",&N,&K);
init ();
string s1,s2;
int cnt=,x;
vector<pair<string,string>> v1;
for (int i=;i<N;i++) {
cin>>s1>>s2>>x;
if (pos1[s1]==) {
pos1[s1]=cnt;
pos2[cnt++]=s1;
}
if (pos1[s2]==) {
pos1[s2]=cnt;
pos2[cnt++]=s2;
}
weight[pos1[s1]]+=x;
weight[pos1[s2]]+=x;
v1.push_back({s1,s2});
}
for (int i=;i<v1.size();i++)
Union (pos1[v1[i].first],pos1[v1[i].second]);
for (int i=;i<cnt;i++) {
Node[findfather(i)].total+=weight[i];
Node[findfather(i)].num++;
}
for (int i=;i<cnt;i++) {
if (Node[i].total>K*&&Node[i].num>)
ans[pos2[i]]=Node[i].num;
}
printf ("%d\n",ans.size());
vector<gang> vi;
for (auto it=ans.begin();it!=ans.end();it++)
vi.push_back({it->first,it->second});
sort (vi.begin(),vi.end(),cmp);
for (int i=;i<vi.size();i++)
cout<<vi[i].id<<" "<<vi[i].num<<endl;
return ;
}

PAT A1034 Head Of Gang的更多相关文章

  1. PAT A1034 Head of a Gang (30 分)——图遍历DFS,字符串和数字的对应保存

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

  2. PAT (Advanced Level) Practice(更新中)

    Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...

  3. PAT_A1034#Head of a Gang

    Source: PAT A1034 Head of a Gang (30 分) Description: One way that the police finds the head of a gan ...

  4. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  5. PAT 1034. Head of a Gang (30)

    题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...

  6. PAT甲级1034. Head of a Gang

    PAT甲级1034. Head of a Gang 题意: 警方找到一个帮派的头的一种方式是检查人民的电话.如果A和B之间有电话,我们说A和B是相关的.关系的权重被定义为两人之间所有电话的总时间长度. ...

  7. PAT甲级1034 Head of a Gang【bfs】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624 题意: 给定n条记录(注意不是n个人的 ...

  8. A1034. Head of a Gang

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

  9. PAT甲题题解-1034. Head of a Gang (30)-并查集

    给出n和k接下来n行,每行给出a,b,c,表示a和b之间的关系度,表明他们属于同一个帮派一个帮派由>2个人组成,且总关系度必须大于k.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...

随机推荐

  1. Python - CentOS 下用 yum 安装 pip

    1. 概述 python 安装完成 发现后续需要一个 python 自己的 包管理工具 书上说默认会装, 然后我发现还是没有 命令执行的结果我就不给了, 这个判断起来, 应该是没有太大难度的 2. 环 ...

  2. Day1 面向对象编程与Java核心类

    this变量 在方法内部,可以使用一个隐含的变量this,它始终指向当前实例.如果没有命名冲突,可以省略this. 但是,如果有局部变量和字段重名,那么局部变量优先级更高,就必须加上this. 构造方 ...

  3. P2P头部平台退出后,普通人如何避开投资理财的“雷区”?

    编辑 | 于斌 出品 | 于见(mpyujian) 近期,P2P市场上不断传来不利消息,引起市场轩然大波,也打乱了投资者投资计划,是继续坚持自己的选择还是另择它路? 18日,陆金所作为千亿头部平台,宣 ...

  4. py2

    函数相关的 # 生成器相关的# 例1 ll = sum(i for i in range(100000000)) #生成器占资源少 # 例2 def demo(): for i in range(4) ...

  5. 从centos7镜像到搭建kubernetes集群(kubeadm方式安装)

    在网上看了不少关于Kubernetes的视频,虽然现在还未用上,但是也是时候总结记录一下,父亲常教我的一句话:学到手的东西总有一天会有用!我也相信在将来的某一天会用到现在所学的技术.废话不多扯了... ...

  6. 题解 P4568 【[JLOI2011]飞行路线】

    P4568 [JLOI2011]飞行路线 分层图模板题,相似的题还有P4822 [BJWC2012]冻结,P2939 [USACO09FEB]改造路Revamping Trails,其实做惯了也就不难 ...

  7. Windows 下 Hbuilder 真机调试(Android,iphone)

    概述:主要讲讲自己在使用 HBuilder 真机调试功能时遇到的问题,以及如何解决.Android 相对没有遇到什么大问题,在电脑安装如360手机助手就可以正常使用了,主要问题是在 iphone 上( ...

  8. getter和setter

    /* 对象属性是由名字.值和自足特性构成的. 属性值可以用一个或两个方法替代,这两个方法就是getter和setter. 由getter和setter定义的属性称作“存取器属性” */ /* 定义存取 ...

  9. Vue - 让水平滚动条(scroll bar)固定在浏览器的底部

    效果 踩坑经历 TLDR; 在几个小时的google和stack overflow的苦苦搜索后,无果. 经过自我思考,想到了一种实现方法: 整个页面是一个盒子,要出现滚动条,必然里面的元素要溢出.也即 ...

  10. 爬虫 - Scrapy中间件

    前提:看Scrapy架构图 不管什么Middlewares,都写在middlewares.py里面. 然后在settings.py里的DOWNLOADER_MIDDLEWARES或者SPIDER_MI ...