PAT A1034 Head Of Gang
用并查集分割团伙,判断输出~
#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的更多相关文章
- 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 ...
- PAT (Advanced Level) Practice(更新中)
Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...
- 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 ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- PAT 1034. Head of a Gang (30)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...
- PAT甲级1034. Head of a Gang
PAT甲级1034. Head of a Gang 题意: 警方找到一个帮派的头的一种方式是检查人民的电话.如果A和B之间有电话,我们说A和B是相关的.关系的权重被定义为两人之间所有电话的总时间长度. ...
- PAT甲级1034 Head of a Gang【bfs】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624 题意: 给定n条记录(注意不是n个人的 ...
- 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 ...
- PAT甲题题解-1034. Head of a Gang (30)-并查集
给出n和k接下来n行,每行给出a,b,c,表示a和b之间的关系度,表明他们属于同一个帮派一个帮派由>2个人组成,且总关系度必须大于k.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...
随机推荐
- EVE-NG镜像模板资源占用统计
转:http://www.emulatedlab.com/forum.php?mod=viewthread&tid=432&extra=page%3D1 EVE Image fold ...
- ssh复制秘钥成功后仍然需要输入密码
执行免秘钥操作 ssh-copy-id -i ~/.ssh/id_rsa.pub lyg@192.168.1.65 被登录机器的文件权限: //用户权限 chmod 700 /home/usernam ...
- Docker - 命令 - docker container
概述 整理 docker 容器的命令 1. 分类(25个) 查看 ls diff logs inspect port stats top 生命周期 pause prune create kill re ...
- py2
函数相关的 # 生成器相关的# 例1 ll = sum(i for i in range(100000000)) #生成器占资源少 # 例2 def demo(): for i in range(4) ...
- python3中的正则表达式
精确匹配: \d: 匹配一个数字 \w: 匹配一个字母或数字 . : 匹配任意一个字符 \s: 匹配一个空格(包括tab等空白符) 匹配变长的字符: * : 匹配任意个 ...
- ThreadLocal是什么?
早在JDK 1.2的版本中就提供Java.lang.ThreadLocal,ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以很简洁地编写出优美的多线程程序. 当使 ...
- 16,div+css的布局较table布局有什么优点?
改版的时候更加方便,只要改css文件 页面加载速度更快,结构化清晰,页面显示简洁 表现与结构相分离 易于搜索引擎优化,排名更靠前
- Educational Codeforces Round 76 (Rated for Div. 2)E(最长上升子序列)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[200007],b[200007] ...
- VMware虚拟磁盘修复
最近VMware虚拟机老是断掉提示无法完成同步,后来提示虚拟磁盘需要修复,经过一番查询,找到了相关检查与修复口令,先记录如下. vmware-vdiskmanager -R “PATH” 说明: PA ...
- workspace 打开的是我的电脑
在system tree板块的空白处右键-->set root-->current workspace 即可恢复workspace.