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 ...
随机推荐
- FLV文件格式分析(附源码)
FLV文件主要由两部分组成:Header和Body. 1. Header header部分记录了flv的类型.版本等信息,是flv的开头,一般都差不多,占9bytes.具体格式如下: 文件类型 3 b ...
- linux中systemctl详细理解及常用命令
linux中systemctl详细理解及常用命令 https://blog.csdn.net/skh2015java/article/details/94012643 一.systemctl理解 Li ...
- 如何预测股票分析--长短期记忆网络(LSTM)
在上一篇中,我们回顾了先知的方法,但是在这个案例中表现也不是特别突出,今天介绍的是著名的l s t m算法,在时间序列中解决了传统r n n算法梯度消失问题的的它这一次还会有令人杰出的表现吗? 长短期 ...
- 操作系统 - epoll中的ET和LT
参考 https://www.quora.com/What-is-meant-by-edge-triggering-and-level-triggering http://man7.org/linux ...
- JSP中四大作用域详解
四大作用域 为了在页面.请求.和用户之间传递和共享数据,JSP提供了四个不同的作用域:page(页面作用域).request(请求作用域).session(会话作用域).application(应用程 ...
- Linux Centos7文件系统
上期教大家创建分区,刚分区完成后没有文件系统,分区不能使用.本期教大家创建文件系统,(文件系统:操作系统通过文件系统管理文件及数据,创建文件系统的过程俗称格式化.)没有文件系统的设备称之为裸(raw) ...
- React 实现input输入框的防抖和节流
1.为什么使用防抖和节流对于频繁触发的事件 比如keydown keyup事件 当频繁点击时候 会多次触发事件 页面出现卡顿 影响性能 2.函数防抖(debounce):间隔时间内只执行一次 函数 ...
- Go递归
1. 递归介绍 package main import ( "fmt" ) func test(n int) { if n > 2 { n-- test(n) } fmt.P ...
- BufferedInputStream 介绍
BufferedInputStream 介绍 BufferedInputStream 是缓冲输入流.它继承于FilterInputStream.BufferedInputStream 的作用是为另一个 ...
- Spring IoC 源码分析 (基于注解) 之 包扫描
在上篇文章Spring IoC 源码分析 (基于注解) 一我们分析到,我们通过AnnotationConfigApplicationContext类传入一个包路径启动Spring之后,会首先初始化包扫 ...