一、技术总结

  1. 这一题是考查图的知识,题目的意思要理解清楚,就是考查统计图中连通块的数量,也就是没有一个结点后。
  2. 怎么删除该结点,并且统计连通块的数量成为问题解决的关键,这里可以当访问到结点时,直接返回,或则跳过,这种操作就是相当于删除了该结点。
  3. memset(inq, false, sizeof(inq));这个是初始化标记数组,可以用于反复的遍历数组。
  4. 还有这次出现了一个比较简单的问题,就是在写for循环的嵌套时,同时使用了i变量,导致答案错误,这种问题一下又检查不出来,细心点。

二、参考代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 1010;
  4. vector<int> Adj[maxn];
  5. bool inq[maxn];
  6. int node;//need to delete node
  7. void DFS(int v){
  8. if(v == node) return;
  9. inq[v] = true;
  10. for(int i = 0; i < Adj[v].size(); i++){
  11. int u = Adj[v][i];
  12. if(inq[u] == false){
  13. DFS(u);
  14. }
  15. }
  16. }
  17. int main(){
  18. int n, m, k;
  19. scanf("%d%d%d", &n, &m, &k);
  20. int a, b;
  21. for(int i = 1; i <= m; i++){
  22. scanf("%d %d", &a, &b);
  23. Adj[a].push_back(b), Adj[b].push_back(a);
  24. }
  25. for(int i = 0; i < k; i++){
  26. scanf("%d", &node);
  27. memset(inq, false, sizeof(inq));
  28. int sum = 0;
  29. for(int j = 1; j <= n; j++){
  30. if(j != node && inq[j] == false){
  31. DFS(j);
  32. sum++;
  33. }
  34. }
  35. printf("%d\n", sum-1);
  36. }
  37. return 0;
  38. }

A10131013 Battle Over Cities (25分)的更多相关文章

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

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

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

  3. PAT-1013 Battle Over Cities (25 分) DFS求连通块

    It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...

  4. 1013 Battle Over Cities (25 分)

    It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...

  5. 1013 Battle Over Cities (25分) 图的连通分量+DFS

    题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...

  6. 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)

    题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...

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

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

  8. pat1013. Battle Over Cities (25)

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

  9. Battle Over Cities (25)(DFS、连通图)

    It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...

随机推荐

  1. Go_MySQL查询插入删除

    什么是预处理? 普通SQL语句执行过程: 客户端对SQL语句进行占位符替换得到完整的SQL语句. 客户端发送完整SQL语句到MySQL服务端 MySQL服务端执行完整的SQL语句并将结果返回给客户端. ...

  2. Mysql 函数定义及批量数据脚本

    零.说在前面 在定义函数之前 需要先将 log_bin_trust_function_creators 值设为开启,原因如下 在主从复制的两台Mysql服务器中,slaver会从master复制数据, ...

  3. Oracle常用函数记录

    Oracle函数 --schema:hcf --不带任何参数 http://www.cnblogs.com/wuyisky/archive/2010/05/11/oracle_function.htm ...

  4. flutter web 配置环境及运行(windows)

    此下 操作 都是基于 windows  一, 将镜像添加到 用户环境变量中 由于在国内访问Flutter有时可能会受到限制,Flutter官方为中国开发者搭建了临时镜像,大家可以将如下环境变量加入到用 ...

  5. MySql 怎么存取 Emoji

    01.前言 Emoji 在我们生活中真的是越来越常见了,几乎每次发消息的时候不带个 Emoji,总觉得少了点什么,似乎干巴巴的文字已经无法承载我们丰富的感情了.对于我们开发者来说,如何将 Emoji ...

  6. 全网最详细!搭建Hexo+Coding/Gitee,实现一键生成,同步部署

    全网最全小白搭建Hexo+Gitee/Coding/Github 全网最全小白搭建Hexo+Gitee/Coding/Github 本站内容已全部转移到https://www.myyuns.ltd,具 ...

  7. Java+Selenium+Testng自动化测试学习(二)

    Java+Selenium+TestNG自动化测试框架整合 1.简化代码 封装一个定位元素的类,类型为ElementLocation package com.test; import org.open ...

  8. Sql Server跨服务器操作数据

    var serversSql = "select count(*) count from sys.servers WHERE name='ITSV'"; var result = ...

  9. Codeforces 1315A Dead Pixel (水题)

    Screen resolution of Polycarp's monitor is a×ba×b pixels. Unfortunately, there is one dead pixel at ...

  10. Java出现NoSuchElementException异常

    参考网址:https://blog.csdn.net/xiao_ma_csdn/article/details/78906650 出现这个异常是线程访问越界,这个时候就要检查下到底是哪里越界. 原因是 ...