A10131013 Battle Over Cities (25分)
一、技术总结
- 这一题是考查图的知识,题目的意思要理解清楚,就是考查统计图中连通块的数量,也就是没有一个结点后。
- 怎么删除该结点,并且统计连通块的数量成为问题解决的关键,这里可以当访问到结点时,直接返回,或则跳过,这种操作就是相当于删除了该结点。
memset(inq, false, sizeof(inq));这个是初始化标记数组,可以用于反复的遍历数组。- 还有这次出现了一个比较简单的问题,就是在写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分)的更多相关文章
- 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 ...
- 1013 Battle Over Cities (25分) DFS | 并查集
1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways ...
- 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 ...
- 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 ...
- 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 ...
- 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)
题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
- pat1013. Battle Over Cities (25)
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- 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 ...
随机推荐
- CentOS安装docker,及其基本操作
CentOS安装docker,及其基本操作 一.安装docker Docker要求运行在Centos 7上,要求系统为64位,系统内核版本3.10以上 1.uname -an 查看当前系统版本 2.y ...
- AAC huffman decoding
在AAC编码器内部,使用huffman coding用于进一步减少scalefactor和量化频谱系数的冗余. 从individual_channel_stream层提取码流进行huffman解码,码 ...
- HBase记录
本次记录是用于:SparkStreaming对接Kafka.HBase记录 一.基本概念 1.HBase以表的形式存储数据.表有行和列族组成.列族划分为若干个列.其结构如下 2.Row Key:行键 ...
- Jarvis OJ - DD-Hello -Writeup
Jarvis OJ - DD-Hello -Writeup 转载请注明出处http://www.cnblogs.com/WangAoBo/p/7239216.html 题目: 分析: 第一次做这道题时 ...
- python-第三方库的理解及某个函数的源代码
第三方库,是一个总称,里面有各个模块,而具体使用的函数是模块里的. 库包含多个模块, 每个模块里包含多个函数. import AAAA 就是引用AAAA这个库,这个库里的模块函数都可以用,只是 ...
- docker互联机制实现便捷互访
何为容器互联 & 为何需要容器互联 容器的互联是一种让多个容器中应用进行快速交互的方式,它会在源和接收容器之间创建连接关系,接收容器可以通过容器名快速访问到源容器,而不用指定具体的 ip 地址 ...
- C语言随笔5:函数、函数指针
函数 C语言中函数参数传递时,将实参的值拷贝到函数参数的存储区中.这种传递参数的方式称为按值传递. 函数不会访问实参本身,访问的是函数存储在栈区的副本,不会改变实参.函数凋用结束,函数在栈区的内容释放 ...
- 如何在项目中新建.gitignore文件
1. 在需要创建 .gitignore 文件的文件夹, 右键选择 Git Bash 进入命令行,进入项目所在目录. 2. 输入 touch .gitignore 在文件夹就生成了一个“.gitigno ...
- 利用tensorboard将数据可视化
注:代码是网上下载的,但是找不到原始出处了,侵权则删 先写出visual类: class TF_visualizer(object): def __init__(self, dimension, ve ...
- go key-value缓存go-cache实现
Cache类型 Cache封装了一个cache类型,cache类型的参数解析: 1.defaultExpiration time.Duration 每个键值的默认过期时间. 2.items map[s ...