【PAT】A1013. Battle Over Cities(DFS)

PAT甲级 1013 Battle Over Cities It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the c 阅读详情

【PAT】A1013. Battle Over Cities(DFS)

@(PAT)

链接:https://www.patest.cn/contests/pat-a-practise/10013

思路:
1. 题目是要我们找到一块原来的图,去掉一个点以及与之相连的道路后,还需要添加几条通路能够使新的图再次连同,很容易想到使用DFS。
2. 要注意每次调用一次DFS函数,进行一次深度查找的时候,就是找到了图的连通的一部分,最后统计图有N个连通的部分,则让整个图都连通就只需要添加N- 1条通路。
3. 注意使用DFS的递归形式的话,记录用的变量要放在main函数外面。

My AC code:

#include <iostream>
#include <vector>

using namespace std;

int n; 
vector<vector<int> > mat_lost;
vector<bool> visited;

vector<vector<int> > matInit(int size) {
    vector<vector<int> > mat;
    vector<int> tempv;
    for (int i= 0; i< size; i++) {
        tempv.push_back(0);
    }
    for (int i= 0; i< size; i++) {
        mat.push_back(tempv);
    }
    return mat;
}

void DFS(int start) {
    visited[start]= true;
    for (int i= 0; i< n; i++) {
        if (!visited[i]&& mat_lost[start][i]== 1) {
            DFS(i);
        }
    }
}

int main() {
    int m, k;
    scanf("%d%d%d", &n, &m, &k);
    vector<vector<int> > mat= matInit(n);
    for (int i= 0; i< m; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        mat[a- 1][b- 1]= 1;
        mat[b- 1][a- 1]= 1;
    }

    for (int i= 0; i< n; i++) {
        visited.push_back(false);
    }

    for (int c= 0; c< k; c++) {
        int lost;
        scanf("%d", &lost);
        lost= lost- 1;
        mat_lost= matInit(n);
        for (int i= 0; i< n; i++) {
            for (int j= 0; j< n; j++) {
                if (i!= lost&& j!= lost) {
                    mat_lost[i][j]= mat[i][j];
                }
            }
        }
        for (int i= 0; i< n; i++) {
            visited[i]= false;
        }
        visited[lost]= true;
        int num= 0; 
        for (int i= 0; i< n; i++) {
            if (!visited[i]) {
                DFS(i);
                num++;
            }
        }
        printf("%d\n", num- 1);
    }
}

因为使用了vector表示二维数组,所以可能有点繁琐。

1013 Battle Over Cities (25)(25 分) 1 题目 2 解题思路 3 AC代码 1 题目   It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are ... 阅读详情

相关推荐

基于SECS协议开发的简明教程(1)

SEMI SECS MES E4 E5 E30 E37 HSMS SDK

L251210269的专栏 7629

【图的DFS遍历】PAT A1013 Battle Over Cities (25分)

文章目录题目描述知识点我的实现码前思考代码实现码后反思 题目描述 知识点 图的遍历 我的实现 码前思考 就是求图中的连通块的数量 代码实现 //建议使用邻接表进行访问 #include <iostream> #include <vector> #include <algorithm> using namespace std; const int maxn = 1010; //共享数据结构的初始化 bool vis[maxn]; vector<int>.

yc_cy1999的博客 205

目标检测 RT-DETR(2023)详细解读

在使用了这个技巧后,显然训练过程中,类别的标签不再是此前的0和1离散值,而是0~1的连续值。关于 Query Selection(查询向量选择),大家应该并不陌生,这个方法可谓在DETR领域大杀四方,如DAB-DETR对查询向量进行重构理解,将其解释为Anchor,DN-DETR通过查询降噪来应对匈牙利匹配的二义性所导致的训练时间长的问题,DINO提出从Encoder中选择Top-k特征进行学习等一系列方法,这都无疑向我们证明,查询向量很重要,选择好的Query能够让我们事半功倍。在RT-DETR中,

qq_51352130的博客 5869

pat 甲级 图 A1013 Battle Over Cities (25分) dfs

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 by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other hig

weixin_42358356的博客 117

ColorOS13系统账户锁与密码锁破解全指南:从恢复模式到激活绕过

本文针对ColorOS13系统用户,详细解析了忘记锁屏密码(数字/图案锁)和遭遇OPPO账户锁(激活锁)的合法解决方案。核心是通过官方恢复模式进行格式化操作清除锁屏密码,并强调账户锁需通过密码找回或官方售后等正规途径解除。文章旨在帮助设备合法所有者恢复使用权,并提供了安全操作指南与数据备份建议。

weixin_28304023的博客 965

BIDS数据存储格式介绍与dcm2bids安装与使用

BIDS格式&dcm2bids安装与使用

machinelearning_er的博客 7317

pat甲级A1013 Battle Over Cities (25分) 并查集+dfs

思路 题目要求去掉一个顶点后,需要添加多少条边使得剩下的所有顶点再次连通。可转化去掉一个点后,图中有多少个连通块。添加边数为连通块数减一。连通块可用并查集表示。 将去掉顶点的状态设为已遍历,这样在dfs中,每个顶点遍历不到它,也不能通过它到达其他顶点,相当于去掉这个顶点。 #include <bits/stdc++.h> using namespace std; const in...

qq_43732110的博客 155

EEG | CHB-MIT癫痫数据集CNN分类实战(一)

CHB-MIT癫痫数据集CNN分类实战

Always learn from the best. 1万+

【PAT A1013】Battle over cities(图的遍历DFS)

【代码】【PAT A1013】Battle over cities(图的遍历DFS)

Honeyxuu的博客 115

最新版Node.js下载安装及环境配置教程(非常详细)

选择对应你系统的Node.js版本。

DN金猿的博客 12万+

FlashBootloader01-基于UDS的Flash Bootloader【架构篇】

文章目录1 Flash Bootloader的依赖项2 Flash Bootloader中使用的UDS的主要功能3 Flash Bootloader更新固件3.1 初始化MCU3.2 刷写流程 1 Flash Bootloader的依赖项 通常Flash Bootloader由以下四方面组成: UDS协议 上位机「编写Bootloader Sequence」 通信驱动(如CAN ) Flash Driver 2 Flash Bootloader中使用的UDS的主要功能 1. 10 02服务 客户端

3591

PAT A1013 Battle Over Cities (25分)【图的DFS+连通分量】

文章目录题目描述知识点实现码前思考代码实现码后反思 题目描述 知识点 图的遍历 实现 码前思考 图的dfs,求连通分量哒~ 记得每次都要初始化vis数组全体为false哈 代码实现 //图的DFS,计数连通分量 //数组下标从1开始 #include <cstdio> #include <algorithm> using namespace std; const int maxn = 1e3+10; int n; int m; int k; int blocked; b.

yc_cy1999的博客 256

C++经典面试题100例及答案

答:把一些具有共性的对象归类后形成一个集合,也就是所谓的类。答:这样可以提高编译效率,因为分开的话只需要编译一次生成对应的.obj文件后,再次应用该类的地方,这个类就不会被再次编译,从而大大的提高了编译效率。答:这种函数会自动为内联函数,这种函数在函数调用的地方在编译阶段都会进行代码替换。答:常对象是指在任何场合都不能对其成员的值进行修改的对象。答:所谓的回调函数,就是预先在系统的对函数进行注册,让系统知道这个函数的存在,以后,当某个事件发生时,再调用这个函数对事件进行响应。

usstmiracle的博客 3万+

A1013 Battle Over Cities (25 分)PAT甲级真题(C++)题目详解【图论+DFS】

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the c

qq_47677800的博客 1947

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

PAT (Advanced Level) Practice A1013 Battle Over Cities (25 分)(甲级)(C++)(图、DFS)

原题链接:A1013 Battle Over Cities #include&lt;algorithm&gt; #include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;cstring&gt; #include &lt;ctime&gt; #include &lt;cmath&gt; #include &lt;vector&gt; #i

izhxxx的博客 193

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

PAT-A 1013 Battle Over Cities (25) DFS+求图的连通块数目(两种实现)

https://pintia.cn/problem-sets/994805342720868352/problems/994805500414115840 1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highways in a war. If a c...

荷叶田田 435

PAT 甲级 1013 Battle Over Cities

PAT 甲级 1013 Battle Over Cities It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other hig

Jasonare的博客 414

pat顶级1001 Battle Over Cities - Hard Version (35 point(s))

欢迎访问我的pat顶级题解目录哦https://blog.csdn.net/richenyunqi/article/details/86751676 题目描述 算法笔记 本题是pat甲级1013. Battle Over Cities (25)的升级版,我在博客pat甲级1013. Battle Over Cities (25)题解中阐述了解决甲级1013题的两种方法:并查集和DFS搜索。本题中...

日沉云起的博客 1941

PAT-ADVANCED1013——Battle Over Cities

我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED 原题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805500414115840 题目描述: 题目翻译: 1013 城市之间的战争 在战争中,所有的城市都通过高速公路连接在一起,这一...

清风阁 1822

PAT A1013 Battle Over Cities

PAT A1013 Battle Over Cities Sample Input: 3 2 3 1 2 1 3 1 2 3 Sample Output: 1 0 0 分析: 题意即:找去掉某一结点及其所有边后,剩下的图的连通块数 思路 1: 每次查询先重置标记数组flag[],将要查询结点"注释掉",标记为true,这样其他节点就到不了它(相当于删除这个节点及其所有边) 每D...

qq_42347617的博客 208

PAT 1013 Battle Over Cities

分数 25作者 CHEN, Yue单位 浙江大学。

m0_73801775的博客 795

[PAT A1013]Battle Over Cities

[PAT A1013]Battle Over Cities 题目描述 It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are close...

欢迎来到我的编程小屋 747

【PAT】1013. Battle Over Cities (25)

题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1013 题目描述: It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways

此去经年 3848

PAT 甲级 1013(Battle Over Cities )

PAT 甲级 1013题目要求翻译代码思路 题目要求 翻译 在战争中,所有的城市都通过高速公路连接在一起,这一点是至关重要的。如果一个城市被敌人占领了,那么所有连接这个城市的高速公路都会被封闭。我们必须马上知道为了使得余下的城市保持连接状态,我们是否需要修建其他的高速公路。给你一张城市地图,上面标识出了所有余下的高速公路,你需要快速说出需要修建的高速公路的数量。 举个例子,如果我们有3座城市,2条高速公路分别连接city1-city2、city1-city3。如果city1被敌人占领了,我们就需要修建一条

qq_42865772的博客 275
上一篇: 【系统分析与设计】Lesson 2 Homework
下一篇: 【数据结构】C++实现整理_图的BFS与DFS(递归非递归)
timsooo
博客等级 码龄11年 16粉丝 61原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值