这题写得比较痛苦。首先有点不在状态,其次题目比较难读懂。

“Gang”成立的两个条件:①成员数大于两个  ②边权总和大于阈值K

首先,在录数据的时候通过map或者字符串哈希建立string到int的映射。

然后,这个题的数据结构其实是带权无向图。在录数据的时候就要处理好点权边权

最后,对所有顶点做一遍dfs,汇总边权,找出最大点权和最大点权所在的点,把数据录进ans里。

注意的点一个是环路边权的汇总。方法是在dfs中通过先加边权判断vis是否遍历过。此时又要注意用不搜前驱点的dfs结果,及dfs(p,s) //p是前驱点,s是当前点

还有一个是邻接数组要开大一点。虽然给出的图的边数是小于1000,但是顶点数不一定。

#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <map> #define I scanf
#define OL puts
#define O printf
#define F(a,b,c) for(a=b;a<c;a++)
#define FF(a,b) for(a=0;a<b;a++)
#define FG(a,b) for(a=b-1;a>=0;a--)
#define LEN 10000
#define MAX 0x06FFFFFF
#define V vector<int> using namespace std; string ID2name[LEN];
map<string,int> name2ID;
int g[LEN][LEN];
int nID=;
int w[LEN];
int vis[LEN];
int cnt;
int maxV;
int maxID;
int gang[LEN];
int sum;
set<string> ans; int getID(string s){
int id=name2ID[s];
if(id){
return id;
}else{
name2ID[s]=nID;
ID2name[nID]=s;
return nID++;
}
} void dfs(int p,int s){
vis[s]=;
cnt++;
if(w[s]>maxV){
maxV=w[s];
maxID=s;
}
for(int i=;i<nID;i++) if(g[s][i]> && i!=p){
sum+=g[s][i];
if(vis[i]) continue;
dfs(s,i);
}
} int main(){
// freopen("D:\\CbWorkspace\\PAT\\图的遍历\\1034_2.txt","r",stdin);
int n,k,i,j;
I("%d%d",&n,&k);
FF(i,n){
char buf[];
I("%s",buf);
string a(buf);
I("%s",buf);
string b(buf);
I("%d",&j);
int ai=getID(a);
int bi=getID(b);
g[ai][bi]+=j;
g[bi][ai]+=j;
w[ai]+=j;
w[bi]+=j;
}
F(i,,nID+) if(!vis[i]){
cnt=;
maxV=-;
sum=;
dfs(-,i); //深搜
if(cnt> && sum>k){
ans.insert(ID2name[maxID]);
gang[maxID]=cnt;
}
}
printf("%d\n",ans.size());
set<string>::iterator it=ans.begin();
while(it!=ans.end()){
printf("%s %d\n",it->c_str(),gang[name2ID[*it]]);
it++;
}
return ;
}

图的遍历 | 1034 map处理输入数据,连通块判断的更多相关文章

  1. 1034 Head of a Gang 图的遍历,map使用

    命名冲突,导致编译失败.这大概就是之前看到的最好不要using namespace std:的原因

  2. ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...

  3. CodeForces 690D1 The Wall (easy) (判断连通块的数量)

    题意:给定一个图,问你有几个连通块. 析:不用说了,最简单的DFS. 代码如下: #include <bits/stdc++.h> using namespace std; const i ...

  4. 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 ...

  5. 图的遍历——DFS和BFS模板(一般的图)

    关于图的遍历,通常有深度优先搜索(DFS)和广度优先搜索(BFS),本文结合一般的图结构(邻接矩阵和邻接表),给出两种遍历算法的模板 1.深度优先搜索(DFS) #include<iostrea ...

  6. 图的遍历——DFS(矩形空间)

    首先,这里的图不是指的我们一般所说的图结构,而是大小为M*N的矩形区域(也可以看成是一个矩阵).而关于矩形区域的遍历问题经常出现,如“寻找矩阵中的路径”.“找到矩形区域的某个特殊点”等等之类的题目,在 ...

  7. ZOJ 3781 - Paint the Grid Reloaded - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds      Me ...

  8. [图的遍历&多标准] 1087. All Roads Lead to Rome (30)

    1087. All Roads Lead to Rome (30) Indeed there are many different tourist routes from our city to Ro ...

  9. 图的遍历:DFS和BFS

    图的遍历一般由两者方式:深度优先搜索(DFS),广度优先搜索(BFS),深度优先就是先访问完最深层次的数据元素,而BFS其实就是层次遍历,每一层每一层的遍历. 1.深度优先搜索(DFS) 我一贯习惯有 ...

随机推荐

  1. 新的部署架构之下,如何拿shell?

    和朋友聊起一个话题,服务器部署架构升级对安全的影响.从最简单的一台服务器,到应用.数据库.文件服务器分离:从本地机房服务器到云服务器产品矩阵:从虚拟化到容器化部署,一直在往更安全的方向改变. 本文试图 ...

  2. 【java】查看Java字节码文件内容的方法+使用javap找不到类 解决方法

    研究synchronized底层实现,涉及到查看java字节码的需要 前提是,你的PC已经成功安装了JDK并别配置了环境变量. ==========查看方法========= 一.javap查看简约字 ...

  3. StackExchange.Redis 封装

    博主最近开始玩Redis啊~~ 看了很多Redis的文章,感觉有点云里雾里的,之前看到是ServiceStack.Redis,看了一些大佬封装的Helper类,还是懵懵的QAQ 没办法啊只能硬着**上 ...

  4. DP之背包

    一.01背包: (以下均可用一维来写 即只能选择一次的物品装在一定容积的背包中.f[i][j]表示前i件物品在容积为j时的最大价值. for(int i = 1; i <= n ;  i++){ ...

  5. iOS11里判断Safari浏览器是无痕模式还是正常模式?

    var isPrivate = false; try { window.openDatabase(null, null, null, null); } catch (_) { isPrivate = ...

  6. Bloom’S Taxonomy

    引用:https://www.learning-theories.com/blooms-taxonomy-bloom.html Bloom's Taxonomy is a model that is ...

  7. Python数据分析工具:Pandas之Series

    Python数据分析工具:Pandas之Series Pandas概述Pandas是Python的一个数据分析包,该工具为解决数据分析任务而创建.Pandas纳入大量库和标准数据模型,提供高效的操作数 ...

  8. OKGo vs RxHttpUtils ...

    jeasonlzy/okhttp-OkGohttps://github.com/jeasonlzy/okhttp-OkGo Android OkGo基本操作https://www.jianshu.co ...

  9. LabWindows/CVI第一章:基本规则

    一. #include<stdio.h>      //头文件,#号是预处理指令,standard input output header的缩写. void main()          ...

  10. 【前端_React】Node.js和webpack的安装

    第一步——安装Node.js 首先要安装Node.js,Node.js自带了软件包管理工具npm,可以使用npm安装各种插件.Node.js的下载地址 可以自定义安装到指定的路径下,待安装完成后命令行 ...