1013. Battle Over Cities 用dfs计算联通分量
使用一个标记数组,标记 节点是否已访问
int 连通度=0
dfs(node i)
{标记当前节点为以访问
for(每一个节点)
{if(当前几点未访问 并且 从i到当前节点有直接路径)
dfs(当前节点)
}}
main()
{
....
for(对于每个点)
{如果mark【i】==false;//未被访问
{连通度++;dfs(i);}
...
}
对于不管是任何带有循环性质的结构(dfs ,bfs,while,for)
由于边界问题,如果处理不当,会给思路和编码带来巨大的困难
一种解决方式是,循环当期判断的元素,就是当前要处理,但还没有处理的元素,而不在外部干预
例如:使用while(++i){...}而不是while(i++){}
比如上面的dfs (i)对于i,不是在外界先把mark[i]=true 再进dfs,而是写成dfs(i){ mark[i]=true};
这样会使得思考难度大幅度下降,同时,减少一些专门用于边界情况的判断及处理的代码
1013. Battle Over Cities 用dfs计算联通分量的更多相关文章
- 1013 Battle Over Cities (25分) DFS | 并查集
1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
- PAT 1013 Battle Over Cities
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
- PAT甲级1013. Battle Over Cities
PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...
- 图论 - PAT甲级 1013 Battle Over Cities C++
PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...
- PAT 1013 Battle Over Cities(并查集)
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- pat 1013 Battle Over Cities(25 分) (并查集)
1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways i ...
- PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
- PTA (Advanced Level) 1013 Battle Over Cities
Battle Over Cities It is vitally important to have all the cities connected by highways in a war. If ...
随机推荐
- Oracle使用学习笔记(二)_Sql语句
一.Sql语句的分类 数据操作语言,简称DML(data manipulation language),如增加,删除,修改,查询数据等 数据定义语言,简称DDL(data defination lan ...
- linux 之网络命令
write 给用户发消息,用户必须在线,以ctrl+d保存结束 语法: write 用户 wall发广播信息(write all) 语法:wall 信息 mail 查看发送电子邮件 发送语法 : m ...
- C# 异步示例代码
在 使用BackgroundWorker组件 一文中,阐述了在Winform编程中,使用BackgroundWorker组件实现异步调用,本文主要讲述利用委托实现异步. 以下描述摘抄于MSDN: 异步 ...
- 变量新声明之let、const
一.let 1.通过let声明变量不会变量声明提升 let a = 10; console.log( a ) 会报错 2. let a = 10; let a = 10; 会报错,(a 已被定义) 3 ...
- 爬取github项目。
import requests from bs4 import BeautifulSoup url = 'https://github.com/login' headers = { 'User-Age ...
- mysql链接服务器,update报错
select * from Openquery(MySQL, 'SELECT * FROM official.sys_hospital') 执行更新语句: ; 报错,错误信息: 链接服务器" ...
- 电话号自动识别之bug解决汇总
今天测试一个defect: “联系我们”页显示的电话号码,在不同浏览器显示效果不统一,有些浏览器自动识别电话号码并强制添加了样式. 网络搜索发现,其它website 也有类似问题,例如:http:// ...
- checkpoint NGFW VM安装
step1:在VMworkstation中创建虚拟机向导,选择Linux 2.6内核 64位如下图: 虚拟机的配置建议如下: RAM:至少8GB Disk:120G CPU:四核 step2:使用IO ...
- paloalto防火墙版本升级
1.准备工作:此部分不影响生产环境,可直接操作. 1)备份: 2)下载OS HA情况下,在主机下载完成后,选择 Sync To Peer(同步到对端)同步到备机. 2.安装更新 1)在备机上选择安装 ...
- linux下进程绑定cpu情况查看的几种方法
1.pidstat命令 查看进程使用cpu情况,如果绑定了多个cpu会都显示出来 pidstat -p `pidof 进程名` -t 1 2.top命令 (1)top (2)按f键可以选择下面配置选项 ...