使用一个标记数组,标记 节点是否已访问

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计算联通分量的更多相关文章

  1. 1013 Battle Over Cities (25分) DFS | 并查集

    1013 Battle Over Cities (25分)   It is vitally important to have all the cities connected by highways ...

  2. PAT 解题报告 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...

  3. PAT 1013 Battle Over Cities

    1013 Battle Over Cities (25 分)   It is vitally important to have all the cities connected by highway ...

  4. PAT甲级1013. Battle Over Cities

    PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...

  5. 图论 - PAT甲级 1013 Battle Over Cities C++

    PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...

  6. PAT 1013 Battle Over Cities(并查集)

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  7. pat 1013 Battle Over Cities(25 分) (并查集)

    1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways i ...

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

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

随机推荐

  1. R和Python,对抗or融合?

    来源商业新知网,原标题:从对抗到融合,教你充分利用R+Python! 我们应该将关注点放在技能上,而不是工具上. 如果你从事数据科学的工作,可能会立即想到两种编程语言:R和Python. 事实上,R和 ...

  2. 关于ASP.NET 服务器报错 Server Error in '/' Application Runtime Error 错误及解决方法

    今天遇到一个错误 程序在服务器上运行时报错 先贴上错误代码 自己也在网上找了一些解决方法,把错误定位到服务器的配置文件也就是Web.config的问题, 于是在system.web节点下 加上cust ...

  3. (十)操作数据库、xlrd、xlwt补充

    一.补充操作数据库: 1.建立游标时,指定返回的类型是字典 cur = coon.cursor(cursor=pymysql.cursors.DictCursor) 2.cur.fetchall()  ...

  4. ActionScript3.0(AS3)中的泛型数组Vector

    Adobe官方并没有"泛型数组"的叫法,这是我自己对Vector的叫法(有点标题党),不过Vector在使用上确实跟c#中的泛型数组有些相似之处. 原作者:菩提树下的杨过出处:ht ...

  5. 自己实现HashMap

    一载体 HashMap是由数组组成,数组元素为哈希链. 数组 public class MyHashMap<K, V> { transient Node<K, V>[] tab ...

  6. 在Html页面中调用ajax代码

    以最原始的XMLHttpRequest形式,实现ajax. 封装的方法 1 /** 2 * 发送一个 AJAX 请求 3 * @param {String} method 请求方法 4 * @para ...

  7. Ubuntu 16.04 安装的那点事

    通常,Ubuntu都是与windows共存——安装成双系统的 如果在虚拟机上安装,请参照 https://blog.csdn.net/wyx100/article/details/51582617 U ...

  8. OJ题解记录计划

    容错声明: ①题目选自https://acm.ecnu.edu.cn/,不再检查题目删改情况 ②所有代码仅代表个人AC提交,不保证解法无误 E0001  A+B Problem First AC: 2 ...

  9. Eclipse oxygen 版本汉化教程

    Eclipse oxygen 版本汉化步骤如下: 第一步:打开Eclipse 第二步:浏览器打开网址 http://www.eclipse.org/babel/downloads.php 1.复制对应 ...

  10. php使用redis的几种常见方式和用法

    一.简单的字符串缓存 比如针对一些sql查询较慢,更新不频繁的数据进行缓存. <?php $redis = new Redis(); $redis->connect('127.0.0.1' ...