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.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...
随机推荐
- 简单json 转换为 map、对象
1.如果转换的是Map.或者是简单的对象 package com.gc.action; import java.util.Map; import net.sf.json.JSONObject; /** ...
- 题解【洛谷P2853】[USACO06DEC]牛的野餐Cow Picnic
题目描述 The cows are having a picnic! Each of Farmer John's \(K (1 ≤ K ≤ 100)\) cows is grazing in one ...
- Panda的学习之路(3)——pandas 设置特定的值&处理没有数据的部分
先设定好我们的dataframe: # pandas 设置特定的值 dates=pd.date_range(',periods=6) # print(dates) df=pd.DataFrame(np ...
- LA 2995 立方体成像(模拟)
题目链接:https://vjudge.net/problem/UVALive-2995 这道题的主要难点在于三维坐标系的建立,然后在坐标系中进行迭代更新. 注意用宏定义来简化代码. AC代码: #i ...
- vscode生成文件头注释(python)
文件→首选项→用户代码片段→选python 在大括号内添加如下内容: "Print infomation": { "prefix": "prelog& ...
- 2017年陕西省网络空间安全技术大赛——人民的名义-抓捕赵德汉2——Writeup
下载下来的文件是一个jar包,用die和binwalk检查,确实是一个纯正的jar包 java -jar FileName运行jar包,观察文件的外部特征,发现也是判断password的题目 用查 ...
- Python实现将图片以二进制格式保存到MySQL数据库中,以及取出:
创建数据库表格式: CREATE TABLE photo ( photo_no int(6) unsigned NOT NULL auto_increment, image MEDIUMBLOB, P ...
- javacript onclick事件中传递参数
var user = {id:1, name:'zhangsan'}; var object = '<a onclick="conversion(' + JSON.stringify( ...
- Centos6.10-FastDFS-Tracker.conf示例配置
Centos610系列配置 FastDFS_Tracker.conf示例配置 # is this config file disabled # false for enabled # true for ...
- 架设传奇时打开DBC数据库出错或读取DBC失败解决方法
架设传奇时打开DBC数据库出错或读取DBC失败解决方法 DBC右键-属性-高级-管理员身份运行 即可