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

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. ABAP 程序/接口调用其他程序的数据

    在ABAP遇到的业务场景中,可能会遇到一种情况,需要调用其他报表的数据来发送或者二次加工,这个时候又不想对源程序做大的改动.有以下几种思路解决. 1.修改源程序,将需要展示的数据存储到DB中,然后主程 ...

  2. appium+夜神模拟器+python安卓app爬虫初体验

    环境搭建:Windows 7 64bit jdk包:jdk-8u171-windows-x64.exe(http://www.oracle.com/technetwork/java/javase/do ...

  3. MongoDB基本语法

    建立连接 client = pymongo.MongoClient() 新建数据库 db = client["db_name"] 新建表 tble=db["table_n ...

  4. docker容器跑redis

    命令行配置: $ docker search redis $ docker pull docker.io/redis $ mkdir -p /redis/etc/conf /redis/lib  /r ...

  5. 【JDBC】java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone.

    在使用阿里的druid 时,报了一个异常java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized ...

  6. maven工程 添加本地jar依赖

    和第三方平台对接的时候要用到对方提供的一个jar包,jar包怎么直接添加到pom文件的依赖中呢? jar包一般都是公共的,要上传到私服仓库.我们都是直接登录私服,操作仓库. 登录私服可以在项目的pom ...

  7. select简单循环嵌套

    访问学生的物理最高成绩,并且打印出来,单个要打印出所有的信息 在添加几个 and 就可以啦. select  student.gender,student.sname from student whe ...

  8. 安装IDEA的历程

    安装IDEA的历程 写这篇博客的主要目的就是记录自己安装IDEA的"复杂"过程,顺便给一些需要帮助的人提供一些帮助,以及让他们可以少走一些弯路.之所以说"复杂" ...

  9. MTD的认识

    MTD即内存计数设备,是linux中对ROM.NORFLASH.NAND  Flash等存储设备抽象出来的一个设备层,它向上提供统一的访问接口:读写.擦除等:屏蔽了底层硬件的操作.各类存储设备的差别. ...

  10. 题目--统计一行文本的单词个数(PTA预习题)

    PTA预习题——统计一行文本的单词个数 7-1 统计一行文本的单词个数 (15 分) 本题目要求编写程序统计一行字符中单词的个数.所谓“单词”是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以 ...