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

“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. 调试MATLAB代码

    1.在子函数设置的断点,在运行时,不起作用: 因为在主函数开始时,使用了clear all,在运行时,会把断点给删除.

  2. python实现AES加密

    pip install pycryptodomex 需要安装pycryptodomex模块 aes加密 from Cryptodome.Cipher import AES from binascii ...

  3. 05 .NET CORE 2.2 使用OCELOT -- NLog

    加入NLog 按照官网的文档 https://github.com/NLog/NLog/wiki/Getting-started-with-ASP.NET-Core-2 一步一步操作下来,即可设置好. ...

  4. Java集合Map基本方法

    jdk1.7 api中的方法摘要: 参考java集合大全图:https://www.cnblogs.com/xkzhangsanx/p/10889114.html Map为所有Map子类的接口.

  5. Flask应用启动流程

    目录 flask应用启动流程 WSGI 启动流程 flask应用启动流程 WSGI 所有的 python web 框架都要遵循 WSGI 协议 在这里还是要简单回顾一下 WSGI 的核心概念. WSG ...

  6. iOS - 逆向调试自用工具(reveal 14 Hopper Go2Shell ifunboxmac MachOView Alfred Go2Shell iTerm)

    研究了挺长一段时间的逆向感觉没啥可弄的了,再深处对我也用处不大,渐渐兴趣有些掉头了.最近有问我要工具的朋友,分享一些常用工具给用到的朋友. 用法自己百度吧,这里不再赘述. 图例(希望看到你想要的): ...

  7. Vue-Cli 指南

    构建项目: vue create 文件夹名称 运行项目:(README文件查询) npm run serve

  8. 【译】使用WebDriver采样器将JMeter与Selenium集成

    原为地址:https://dev.to/raghwendrasonu/jmeter-integration-with-selenium-using-webdriver-sampler-176k 第一步 ...

  9. doucment的获取节点的信息

    document.activeElement 返回当前获取焦点元素 document.addEventListener() 向文档添加句柄 document.adoptNode(node) 从另外一个 ...

  10. 同步fifo与异步fifo

    参考以下帖子: https://blog.csdn.net/hengzo/article/details/49683707 https://blog.csdn.net/Times_poem/artic ...