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

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. 如何引入.graphql文件并优雅的使用fragment

    你还在为代码中放入长长的模版字符串所苦恼吗,如下图代码片段: ps:这个是grqphql client在nodejs后端项目的实践,如果你是在前端使用graphql,并使用了webpack,那么这些问 ...

  2. CSS 图像高级 径向渐变

    径向渐变 径向渐变使用 radial-gradient 函数语法. 这个语法和线性渐变很类似, 可以指定渐变结束时的形状 以及它的大小. 默认来说,结束形状是一个椭圆形并且和容器的大小比例保持一致. ...

  3. Taro开发之城市选择器(带坐标)

    要写个城市选择器能返回对应的城市(这里只定义到了地级市),同时返回坐标系,参考了网上资料,下面就看看具体代码吧 import Taro, { Component } from '@tarojs/tar ...

  4. Google开源软负载seesaw

    https://github.com/google/seesaw ------------------------ 在分布式系统中,负载均衡是非常重要的环节,通过负载均衡将请求派发到网络中的一个或多个 ...

  5. [Solution] 893. Groups of Special-Equivalent Strings

    Difficulty: Easy Problem You are given an array A of strings. Two strings S and T are special-equiva ...

  6. Docker registry垃圾回收

    Docker registry垃圾回收 通过: docker run -p 5000:5000 -v /netdata/xxxx/registry:/var/lib/registry registry ...

  7. Python Selenium系列学习

    以下记录刚接触Python Selenium操作Web UI的学习问题: 1.python selenium三种等待方式: ①强制等待:time.sleep(value):设置等待最简单的方法就是强制 ...

  8. jsp4

    SESSION ID=B564A2D318ECDF70EB94C6DE2080E951 SESSION ID=B564A2D318ECDF70EB94C6DE2080E951 SESSION ID=B ...

  9. 关于iOS刷新UI需要在主线程执行

    为什么一定要在主线程刷新UI? 安全+效率:因为UIKit框架不是线程安全的框架,当在多个线程进行UI操作,有可能出现资源抢夺,导致问题. 其实:在子线程是不能更新UI的, 看到能更新的结果只是个假象 ...

  10. 【LeetCode刷题系列 - 002题】Add Two Numbers

    题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...