PAT 1013 Battle Over Cities

news/2024/11/19 18:41:03/

个人学习记录,代码难免不尽人意。

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 cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.

For example, if we have 3 cities and 2 highways connecting city 1-city 2
and city 1-city 3 . Then if city 1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city 2-city 3.

Input Specification:
Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.

Output Specification:
For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.

Sample Input:
3 2 3
1 2
1 3
1 2 3
Sample Output:
1
0
0

#include <cstdio>
#include<set>
#include<string>
#include<algorithm>
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
const int maxn=1010;
const int INF=1000000000;
vector<int> G[maxn];
bool visit[maxn]={false};
void dfs(int n,int node){visit[n]=true;for(int i=0;i<G[n].size();i++){if(G[n][i]!=node&&!visit[G[n][i]]){dfs(G[n][i],node);}}return;
}
int main(){int n,m,k;scanf("%d%d%d",&n,&m,&k);for(int i=1;i<=m;i++){int a,b;scanf("%d%d",&a,&b);G[a].push_back(b);G[b].push_back(a);}for(int i=1;i<=k;i++){fill(visit+1,visit+1+n,false);int node;scanf("%d",&node);int count=0;for(int j=1;j<=n;j++){if(!visit[j]&&j!=node){dfs(j,node);count++;}}printf("%d\n",count-1);}
}

本题不难,重点在于知道如何遍历无向图并求出去掉某个节点的连通块个数。


http://www.ppmy.cn/news/1039199.html

相关文章

golang github.com/spf13/cast 库识别不了 自定义数据类型

以下代码运行不会是10&#xff0c;而是返回 0 package mainimport ("fmt""github.com/spf13/cast" )type UserNum int32func main() {var uNum UserNumuNum 10uNumint64 : cast.ToInt64(uNum)uNumint64E, err : cast.ToInt64E(uNum)fmt.Println(uNumin…

两个案例熟悉String的基本操作

1、第一个案例 Java语言规范要求完全相同的字符串字面量&#xff0c;应该包含同样的Unicode字符序列&#xff08;包含同一份码点序列的常量&#xff09;&#xff0c;并且必须是指向同一个String类实例。 package string; public class StringTest4 {public static void main(St…

【计算机网络篇】UDP协议

✅作者简介&#xff1a;大家好&#xff0c;我是小杨 &#x1f4c3;个人主页&#xff1a;「小杨」的csdn博客 &#x1f433;希望大家多多支持&#x1f970;一起进步呀&#xff01; UDP协议 1&#xff0c;UDP 简介 UDP&#xff08;User Datagram Protocol&#xff09;是一种无连…

探秘分布式大数据:融合专业洞见,燃起趣味火花,启迪玄幻思维

文章目录 一 数据导论二 大数据的诞生三 大数据概论3.1 大数据的5V特征3.2 大数据的工作核心 四 大数据软件生态4.1 数据存储软件4.2 数据计算软件4.3 数据传输软件 五 Apache Hadoop概述5.1 Apache Hadoop框架5.2 Hadoop的功能5.3 Hadoop的发展5.4 Hadoop发行版本 一 数据导论…

【SpringBoot笔记38】SpringBoot基于jakarta.mail依赖实现发送邮件的功能(邮件发送)

这篇文章,主要介绍SpringBoot基于jakarta.mail依赖实现发送邮件的功能(邮件发送)。 目录 一、邮件发送功能 1.1、引入依赖 1.2、邮件发送配置类 (1)添加配置信息 <

matlab使用教程(17)—多项式的定义和运算

1.创建并计算多项式 此示例说明如何在 MATLAB 中将多项式表示为向量以及根据相关点计算多项式。 1.1 表示多项式 MATLAB 将多项式表示为行向量&#xff0c;其中包含按降幂排序的系数。例如&#xff0c;三元素向量 p [p2 p1 p0]; 表示多项式 创建一个向量以表示二次多项式…

Unity如何控制声音大小(包括静音功能)

一&#xff1a;UGUI制作 1. 首先在【层级】下面创建UI里面的Slider组件。设置好它对应的宽度和高度。 2.调整Slider滑动条的填充颜色。一般声音颜色我黄色&#xff0c;所以我们也调成黄色。 我们尝试滑动Slider里面的value。 a.滑动前。 b.滑动一半。 c.滑动完。 从以上滑动va…

Blazor前后端框架Known-V1.2.12

V1.2.12 Known是基于C#和Blazor开发的前后端分离快速开发框架&#xff0c;开箱即用&#xff0c;跨平台&#xff0c;一处代码&#xff0c;多处运行。 Gitee&#xff1a; https://gitee.com/known/KnownGithub&#xff1a;https://github.com/known/Known 概述 基于C#和Blazo…