一、技术总结

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

二、参考代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1010;
vector<int> Adj[maxn];
bool inq[maxn];
int node;//need to delete node
void DFS(int v){
if(v == node) return;
inq[v] = true;
for(int i = 0; i < Adj[v].size(); i++){
int u = Adj[v][i];
if(inq[u] == false){
DFS(u);
}
}
}
int main(){
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
int a, b;
for(int i = 1; i <= m; i++){
scanf("%d %d", &a, &b);
Adj[a].push_back(b), Adj[b].push_back(a);
}
for(int i = 0; i < k; i++){
scanf("%d", &node);
memset(inq, false, sizeof(inq));
int sum = 0;
for(int j = 1; j <= n; j++){
if(j != node && inq[j] == false){
DFS(j);
sum++;
}
}
printf("%d\n", sum-1);
}
return 0;
}

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. winform学习(7)Label控件、Button控件、TextBox控件

    Label控件是System.Windows.Forms.Label 类提供的控件. 作用:主要用来提供其他控件的描述文字,例如:登录窗体上的用户名.密码(输入框前面的字) Button控件是Syst ...

  2. 【转载】Java中的容器讲解

    转自:http://blog.csdn.net/garfielder007/article/details/52143794 Set,List,Map,Vector,ArrayList的区别 Java ...

  3. codeforces 962F.simple cycle(tarjan/点双连通分量)

    题目连接:http://codeforces.com/contest/962/problem/F 题目大意是定义一个simple cycle为从一个节点开始绕环走一遍能经过simple cycle内任 ...

  4. servlet中Request与response使用

    服务器根据请求自动创建传入HttpServletRequest对象和HttpServletResponse对象 @Override protected void service(HttpServlet ...

  5. Maven安装与学习

    一.安装 1.下载http://maven.apache.org/download.cgi 2.选择zip格式 安装完后解压到某一位置(E:\xitong\major\apache-maven-3.6 ...

  6. 关于进程,I/O模型的文章

    PHP类 http://rango.swoole.com/archives/508 Node https://github.com/DoubleSpout/threadAndPackage

  7. php操作shee学习笔记之(一)PHP操作shell函数

    一.php操作shell 1.system函数:执行普通命令 string system (string $command [,int &$return_var]) 1)$command是命令 ...

  8. eclipse从svn导入静态文件

    1.从eclipse 选择 导入 2.选择仓库和项目,选择finish 3.选择project项目导出

  9. Flutter 中的表单

    一.Flutter 常用表单介绍   Flutter 中常见的表单有 TextField 单行文本框,TextField 多行文本框.CheckBox.Radio.Switch  CheckboxLi ...

  10. IIS-7.5 第一次加载慢的 解决办法

    问题焦点 Win2008R2,Win7 下面IIS部署的.NET站点第一次加载比较慢. 解决办法: 1.基本原理: 在第一个请求到达之前加载Web应用程序,从而提高其网站的响应性.通过主动加载和初始化 ...