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.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...
随机推荐
- APP测试用例
日程管理APP测试用例 测试编号 测试用例 实际结果 期望结果 测试结果(Pass/Failed) 备注 NO.1 输入正确的用户名和密码点击登录 登录成功 登录成功 Pass NO.2 点击注册界面 ...
- 设备驱动基础学习--misc device简单实现
在Linux驱动中把无法归类的五花八门的设备定义为混杂设备(用miscdevice结构体表述).miscdevice共享一个主设备号MISC_MAJOR(即10),但次设备号不同. 所有的miscde ...
- 三大查找算法(Java实现)
三大查找算法 1.二分查找(Binary Search) public class BinarySearch { public static void main(String[] args) { in ...
- 「JSOI2015」套娃
「JSOI2015」套娃 传送门 考虑贪心. 首先我们假设所有的套娃都互相不套. 然后我们考虑合并两个套娃 \(i\),\(j\) 假设我们把 \(i\) 套到 \(j\) 里面去,那么就可以减少 \ ...
- C语言与汇编的嵌入式编程:统计字符串中各字符出现的次数
原始C语言: #include<stdio.h> void main(){ ]; char pipei[] = "abcdefghijklmnopqrstuvwxyz" ...
- 微信支付开发h5调用
这两天做微信支付开发.碰到大坑.纠结死我了.好不容做完. 后台java:直接上代码:注意区分前后端的变量大小写... @RequestMapping(value = "/index" ...
- GridView 列表组件 以及动态 GridView
1.通过 GridView.builder 实现网格布局 Widget getList(context, index) { return Container( margin: EdgeInse ...
- OO Byebye
一.架构设计 1.第一次作业 首先做的就是把所有的Element全部存起来,我把UmlClass和UmlInterface重新用两个新的类来记录了一下,用于更快地找到他们的关联.其实总体思路还是比较简 ...
- Spring扫描组件的使用详解
https://blog.csdn.net/you18131371836/article/details/53691044?utm_source=blogxgwz5
- utf-8无bom格式编码
BOM——Byte Order Mark,就是字节序标记 在UCS 编码中有一个叫做"ZERO WIDTH NO-BREAK SPACE"的字符,它的编码是FEFF.而FFFE在U ...