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 ...
随机推荐
- Go_MySQL查询插入删除
什么是预处理? 普通SQL语句执行过程: 客户端对SQL语句进行占位符替换得到完整的SQL语句. 客户端发送完整SQL语句到MySQL服务端 MySQL服务端执行完整的SQL语句并将结果返回给客户端. ...
- Mysql 函数定义及批量数据脚本
零.说在前面 在定义函数之前 需要先将 log_bin_trust_function_creators 值设为开启,原因如下 在主从复制的两台Mysql服务器中,slaver会从master复制数据, ...
- Oracle常用函数记录
Oracle函数 --schema:hcf --不带任何参数 http://www.cnblogs.com/wuyisky/archive/2010/05/11/oracle_function.htm ...
- flutter web 配置环境及运行(windows)
此下 操作 都是基于 windows 一, 将镜像添加到 用户环境变量中 由于在国内访问Flutter有时可能会受到限制,Flutter官方为中国开发者搭建了临时镜像,大家可以将如下环境变量加入到用 ...
- MySql 怎么存取 Emoji
01.前言 Emoji 在我们生活中真的是越来越常见了,几乎每次发消息的时候不带个 Emoji,总觉得少了点什么,似乎干巴巴的文字已经无法承载我们丰富的感情了.对于我们开发者来说,如何将 Emoji ...
- 全网最详细!搭建Hexo+Coding/Gitee,实现一键生成,同步部署
全网最全小白搭建Hexo+Gitee/Coding/Github 全网最全小白搭建Hexo+Gitee/Coding/Github 本站内容已全部转移到https://www.myyuns.ltd,具 ...
- Java+Selenium+Testng自动化测试学习(二)
Java+Selenium+TestNG自动化测试框架整合 1.简化代码 封装一个定位元素的类,类型为ElementLocation package com.test; import org.open ...
- Sql Server跨服务器操作数据
var serversSql = "select count(*) count from sys.servers WHERE name='ITSV'"; var result = ...
- Codeforces 1315A Dead Pixel (水题)
Screen resolution of Polycarp's monitor is a×ba×b pixels. Unfortunately, there is one dead pixel at ...
- Java出现NoSuchElementException异常
参考网址:https://blog.csdn.net/xiao_ma_csdn/article/details/78906650 出现这个异常是线程访问越界,这个时候就要检查下到底是哪里越界. 原因是 ...