1034 Head of a Gang (30 分)(图的遍历or并查集)

dfs
#include<bits/stdc++.h> using namespace std;
const int N=;
int mp[N][N];
int weight[N];
int vis[N];
map<string,int>si;
map<int,string>is;
map<string,int>gang;
int cnt;
//进行转换
int solve(string x)
{
if(si.find(x)!=si.end()){
return si[x];
}
else{
si[x]=cnt;
is[cnt]=x;
return cnt++;
}
}
void dfs(int now,int &head,int &number,int &num)
{
if(weight[now]>weight[head]){
head=now;//判断头目
}
number++;//增加数量
vis[now]=true;
for(int i=;i<cnt;i++){
if(mp[now][i]>){
num+=mp[now][i];//总和
mp[now][i]=mp[i][now]=;
if(!vis[i]){
vis[i]=true;
dfs(i,head,number,num);
}
}
}
}
int main()
{
fill(weight,weight+N,);
fill(mp[],mp[]+N*N,);
fill(vis,vis+N,false);
int n,k;
cin>>n>>k;
for(int i=;i<n;i++){
string a,b;
int Num;
cin>>a>>b>>Num;
int x=solve(a);
int y=solve(b);
weight[x]+=Num;//每个点都需要
weight[y]+=Num;
mp[x][y]+=Num;
mp[y][x]+=Num;
}
for(int i=;i<cnt;i++){
if(!vis[i]){
int head=i;
int number=;
int num=;
vis[i]=true;
dfs(i,head,number,num);
if(number>&&num>k){
gang[is[head]]=number;
}
}
}
cout<<gang.size()<<endl;
for(auto &it:gang){
cout<<it.first<<" "<<it.second<<endl;
}
return ;
}
并查集
自己做的时候用的是并查集 AC了 就是暴力找 后面的代码自己不仔细看也不知道都啥意思 反正很乱
#include<bits/stdc++.h> using namespace std;
const int N=;
int weight[N];
int p[N];
int sum[N];
int mp[N][N];
int findth(int x)
{
if(x==p[x]) return x;
return p[x]=findth(p[x]);
}
void unionn(int x,int y)
{
int xx=findth(x);
int yy=findth(y);
if(xx!=yy){
p[yy]=xx;
sum[xx]+=sum[yy];
}
} map<string,int>si;
map<int,string>is;
map<string,int>gang;
int cnt;
int solve(string a)
{
if(si.find(a)!=si.end()) return si[a];
else{
si[a]=cnt;
is[cnt]=a;
return cnt++;
}
}
struct node
{
int id;
int sum1;
node(int _id,int _sum):id(_id),sum1(_sum){}
}; int main()
{
int n,k;
cin>>n>>k;
for(int i=;i<n;i++){
p[i]=i;
sum[i]=;
}
cnt=;
memset(mp,,sizeof(mp));
for(int i=;i<n;i++){
string a,b;
cin>>a>>b;
int num;
cin>>num;
int x=solve(a);
int y=solve(b);
weight[x]+=num;
weight[y]+=num;
mp[x][y]+=num;
unionn(x,y);
}
set<int>st;
for(int i=;i<cnt;i++){
int x=findth(i);
st.insert(x);
}
vector<int>vec;
for(auto it:st){
if(sum[it]>){
vec.push_back(it);
}
}
if(vec.size()==){
cout<<""<<endl;
return ;
}
vector<int>ve[N];
for(int i=;i<vec.size();i++){
for(int j=;j<cnt;j++){
if(p[j]==vec[i]){
ve[i].push_back(j);
}
}
}
int su=; vector<int>pp;
map<int,int>mm;
vector<node>no;
for(int i=;i<vec.size();i++){
vector<int>tt;
for(int j=;j<ve[i].size();j++){
tt.push_back(ve[i][j]);
}
for(int z=;z<tt.size();z++){
for(int w=;w<tt.size();w++){
su+=mp[tt[z]][tt[w]];
}
}
if(su>k){
no.push_back(node(i,su));
}
su=;
}
if(no.size()==){
cout<<""<<endl;
return ;
}
cout<<no.size()<<endl;
for(int i=;i<no.size();i++){
int maxn=-;
int idd=;
for(int j=;j<ve[no[i].id].size();j++){
if(weight[ve[no[i].id][j]]>maxn){
maxn=weight[ve[no[i].id][j]];
idd=ve[no[i].id][j];
}
}
gang[is[idd]]=sum[findth(idd)];
}
for(auto it:gang){
cout<<it.first<<" "<<it.second<<endl;
}
return ;
}
1034 Head of a Gang (30 分)(图的遍历or并查集)的更多相关文章
- PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)
1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people's p ...
- 1034 Head of a Gang (30分)(dfs 利用map)
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(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=3),以及按照字典序输 ...
- 1013 Battle Over Cities (25 分)(图的遍历or并查集)
这题用并查集或者dfs都可以做 dfs #include<bits/stdc++.h> using namespace std; ; bool mp[N][N]; int n,m,k; b ...
- pat 甲级 1034. Head of a Gang (30)
1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...
- PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]
题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a ...
- 1034. Head of a Gang (30) -string离散化 -map应用 -并查集
题目如下: One way that the police finds the head of a gang is to check people's phone calls. If there is ...
- PAT 1034. Head of a Gang (30)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...
- 1034. Head of a Gang (30)
分析: 考察并查集,注意中间合并时的时间的合并和人数的合并. #include <iostream> #include <stdio.h> #include <algor ...
随机推荐
- Android学习笔记_76_AsyncQueryHandler的应用
研究AsyncQueryHandler这个类的时候遇到了几个重要的不清楚的知识点 1. Handler与Thread,Looper的关系 2. HandlerThread是干什么用的 3. Threa ...
- Webpack学习笔记九 webpack优化总结
webpack 优化笔记 webpack4 自带的优化包括 swingTree(摇摆树)和作用域提升 swingTree 比如入口文件 index.js引入通用方法 util, 里面有 10个方法, ...
- nodejs+express开发blog(2)
npm install -g nodemon 1,把ejs文件修改为html文件 app.engine('.html', require('ejs').__express);app.set('view ...
- ABAP术语-Interface Parameter
Interface Parameter 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/26/1081800.html Parameter t ...
- 爬虫——urllib.request库的基本使用
所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地.在Python中有很多库可以用来抓取网页,我们先学习urllib.request.(在python2.x中为urllib2 ...
- HashMap JDK1.8实现原理
HashMap概述 HashMap存储的是key-value的键值对,允许key为null,也允许value为null.HashMap内部为数组+链表的结构,会根据key的hashCode值来确定数组 ...
- Linux C语言结构体-学习笔记
Linux C语言结构体简介 前面学习了c语言的基本语法特性,本节进行更深入的学习. 预处理程序. 编译指令: 预处理, 宏定义, 建立自己的数据类型:结构体,联合体,动态数据结构 c语言表达式工具 ...
- (数据科学学习手札08)系统聚类法的Python源码实现(与Python,R自带方法进行比较)
聚类分析是数据挖掘方法中应用非常广泛的一项,而聚类分析根据其大体方法的不同又分为系统聚类和快速聚类,其中系统聚类的优点是可以很直观的得到聚类数不同时具体类中包括了哪些样本,而Python和R中都有直接 ...
- Codeforces 845 C Two TVs
参考:https://blog.csdn.net/xjh_shin/article/details/77491693 #include <iostream> #include <cs ...
- 贪心算法之Prim
Prim与Dijistra算法有异曲同工之妙,只不过Dijistra是求最短路径,每次添加到集合中的是到固定起始点的最短距离,而Prim是求最小生成树,是整个图所有权重的最小和,每次添加到集合中的是到 ...