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

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. 【读书笔记】segment routing mpls数据平面-1

  2. Spring再接触 简单属性注入

    <bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl"> <property ...

  3. MySql中order by和union all同时使用

    () UNION ALL () 两边的语句加上括号就可以了

  4. woff/woff2字体404找不到

    每次控制台都报这个错,很纳闷,服务器上明明放了字体文件,怎么找不到呢 今天突然想起来,IIS的MIME类型要配置一下 之前部署网站,一个链接下载app的时候,IIS就不识别apk格式的文件,尽管服务器 ...

  5. HC-05蓝牙模块配对步骤

    参考:https://blog.csdn.net/m0_37182543/article/details/76383247

  6. NET(C#):关于正确读取中文编码文件

    https://blog.csdn.net/ma_jiang/article/details/53213442 首先如果读者对编码或者BOM还不熟悉的话,推荐先读这篇文章:.NET(C#):字符编码( ...

  7. easyui的datagrid改变整行颜色

    easyui的datagrid改变单元格颜色方法1:https://www.cnblogs.com/raitorei/p/10395233.html easyui的datagrid改变单元格颜色方法2 ...

  8. LAB12 Transaction

    思路:就是把aotocommit()里面的东西改改就行了. 查询要求可用房间>=所需要的房间. SQL里面查数字时,不要加单引号.字符串才要. 查询里的set ONHAND=要改成自己的变量名s ...

  9. freeze

    当我们开发项目的时候,会用virtualenv创建很多python独立环境这时候会出现在不同环境下安装相同的模块的情况,为了避免我们通过联网所需模块,不如我们直接从之前Python环境已有的模块中直接 ...

  10. Linux进程的实际用户ID和有效用户ID

    转自:https://blog.csdn.net/hulifangjiayou/article/details/47400943 在Linux中,每个文件都有其所属的用户和用户组,默认情况下是文件的创 ...