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

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. neo4j配置(转)

    我的neo4j配置 # 修改第9行,去掉#,修改数据库名 dbms.active_database=wkq_graph.db # 修改第12行,去掉#.修改路径,改成绝对路径 dbms.directo ...

  2. 小A买彩票-(组合数)

    链接:https://ac.nowcoder.com/acm/contest/549/C来源:牛客网 题目描述 小A最近开始沉迷买彩票,并且希望能够通过买彩票发家致富.已知购买一张彩票需要3元,而彩票 ...

  3. vue_element_vue 引入路径@

    build/webpack.base.conf.js resolve: { extensions: ['.js', '.vue', '.json'], alias: { '@': resolve('s ...

  4. Django继承AbstractUser新建UserInfor Model时出现fields.E304错误

    错误详情: SystemCheckError: System check identified some issues: ERRORS:app01.UserInfo.groups: (fields.E ...

  5. hibernate NUMBER 精度

    通过Hibernate映射实体时会根据数据库中NUMBER类型的精度,生成相应的POJO类中相对应的主键类型.经过亲测结果如下: NUMBER(1) POJO类中生成的是Boolean publicc ...

  6. C/C++扩展Python的时候数据类型转换的对应:

  7. mysql学习5:数据库设计

    mysql学习5:数据库设计 本文转载:https://blog.51cto.com/9291927/2087925:原创为天山老妖S 一.数据库设计简介 按照规范设计,将数据库的设计过程分为六个阶段 ...

  8. CentOS7+CDH5.14.0安装CDH错误排查: HiveServer2 该角色的进程已退出。该角色的预期状态为已启动

    错误提示: HiveServer2 该角色的进程已退出.该角色的预期状态为已启动 解决办法:出现此问题应该是内存不足造成的,重启相应的组件即可.比如Hive报错,重启Hive,YARN报错,重启YAR ...

  9. 基本HTML结构

    配置:在vs code中声明页面为html,然后添加open in browser,view in browser插件通过快捷键alt+b实现在浏览器中查看编写好的html界面 基本成分: <! ...

  10. 解决InetAddress.isReachable(timeout)在windows xp始终返回false的bug

    笔者最近在做产品,其中一个环节用到ping测试主机是否在线. 开发环境:Windows 7 64bit+JDK1.8 x64 以下是检测主机是否在线,开发环境中测试通过 public static b ...