1013 Battle Over Cities (25 point(s))

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 ... 阅读详情

1013 Battle Over Cities (25 point(s))

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

思考:

题意:城市和高速公路构成带权无向图,现在去掉一些城市,其删点子图需要多少条道路使得相互连通。

知识点:并查集,连通分支数

注意点:

(1)findRoot(x)函数;

(2)由于有多个提问,因此需要存储所有高速公路端点(玄学TLE);

(3)注意需要对于删点子图的处理,即遇到该城市(点)或者包含该城市的高速公路(边)都要跳过;

(4)最终需要新修建连通分支数-1条道路。

#include<iostream>
#include<vector>
#include<string>
using namespace std;
const int MAX = 1e3+7;
int Tree[MAX];
int findRoot(int x){
	if(Tree[x]==-1) return x;
	else{
		int temp = findRoot(Tree[x]);
		Tree[x] = temp;
		return temp;
	}
}
struct highway{
	int a,b;
}; 
int main(void){
	int N,M,K;//cities,highways,to be checked
	cin>>N>>M>>K;
	vector<highway> p;
	highway hw;
	for(int i=0;i<M;i++){
		cin>>hw.a>>hw.b;
		p.push_back(hw);
	}
	int t;// what we concern?
	for(int k=0;k<K;k++){
		cin>>t;
		for(int i=1;i<=N;i++) Tree[i]=-1;
		for(int i=0;i<M;i++){
			int c1 = p[i].a;
			int c2 = p[i].b;
			if(c1==t||c2==t) continue;
			c1 = findRoot(c1);
			c2 = findRoot(c2);
			if(c1!=c2){
				Tree[c1]=c2;
			} 
		}
		int branch=0;
		for(int i=1;i<=N;i++){
			if(i==t) continue;
			if(Tree[i]==-1) branch++;
		}
		cout<<branch-1<<endl;
	} 
	return 0;
}

 

阿里开源最强数字人工具 EchoMimicV2,本地部署(一) EchoMimicV2是阿里推出的半身人体AI数字人项目,基于参考图片、音频剪辑和手部姿势序列生成高质量动画视频,确保音频内容与半身动作的一致性。现在本地部署,安装体验一下。 阅读详情

相关推荐

NRF52832串口通信避坑指南:从SDK配置到硬件流控(RTS/CTS)实战

本文详细介绍了NRF52832串口通信的实战指南,从SDK配置到硬件流控(RTS/CTS)的完整实现。通过分析UARTE模块特性、解决SDK版本差异问题、正确配置硬件引脚以及实现稳定通信的代码示例,帮助开发者避开常见陷阱,确保高效可靠的串口通信。特别强调了硬件流控在高速通信中的重要性及其实践方法。

weixin_33739646的博客 283

1013 Battle Over Cities (25 point(s))

题解 可以用并查集,本题解采用dfs. #include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;cstring&gt; #include&lt;algorithm&gt; using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 1010; int e...

徐伯莱 257

安装Anaconda和Labelme的教程

1 登录Anaconda官网,选择Download https://www.anaconda.com/distribution/ 2 选择你想下载的Python版本,选择你的操作系统和位数,然后点击Download按钮就开始下载了 3 开始安装 下面两个框框都勾选 点击next,等待安装成功 打开Anaconda Navigator 点击红色圈圈的按钮 在cmd中输入 (1)创建虚拟环境 conda create -n labelme python=3.6 (2)激活虚拟环境 conda activa

qq_42178122的博客 3812

PAT 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...

weixin_30399871的博客 188

【Pytorch教程】:卷积神经网络 CNN

Pytorch教程目录 Torch and Numpy 变量 (Variable) 激励函数 关系拟合(回归) 区分类型 (分类) 快速搭建法 批训练 加速神经网络训练 Optimizer优化器 卷积神经网络 CNN 目录Pytorch教程目录什么是卷积神经网络 CNN卷积 和 神经网络池化(pooling)流行的 CNN 结构CNN 卷积神经网络MNIST手写数据CNN模型训练可视化训练全部代码 什么是卷积神经网络 CNN 卷积 和 神经网络 卷积神经网络是如何运作的呢, 举一个识别图片的例子, 我们知

nanke_4869的博客 8097

知识见闻 - Salesforce公司发展史

不过,通过将自己开发的产品与公司收购的其它云产品进行结合,Salesforce可以为用户提供更多的产品种类,Salesforce可以继续强化自己的平台和产品套件属性,这一平台和套件的发展规模已经远远超出了一个单一的CRM的规模。他们围绕一个相对简单的产品向用户兜售云的概念,然后,他们开始开发其它产品,并寻找培育一个不断增长的生态系统的方法,最理想的情况是能驱动Salesforce的营收增长。现在,Salesforce通过打造更多的用例并改进自己产品的方式来直接应对竞争对手的竞争,从而捍卫自己现在的地位。

guoqx的专栏 1454

1013 Battle Over Cities (25 分)

A1013 考点:连通集的个数 #include&lt;cstdio&gt; #include&lt;vector&gt; #include&lt;cstring&gt; using namespace std; const int maxn=1010; const int INF=1000000000; int N, M, K; int d[maxn][maxn], visit[maxn]...

学习为主,偶尔扯淡 564

四十二、Fluent欧拉模型流化床模拟:从基础设置到颗粒动力学解析

本文详细解析了Fluent欧拉模型在流化床模拟中的应用,从基础概念到颗粒动力学设置全面覆盖。重点介绍了欧拉模型的颗粒温度概念、多相流配置技巧以及关键参数优化策略,帮助工程师准确模拟气固两相流动现象。通过实用案例演示了网格检查、求解器配置和边界条件设置的最佳实践。

weixin_27303545的博客 445

PTA(A)1013 Battle Over Cities (25point(s))

思路: 并查集建边,找孤立点,总数-2. 代码: #include <bits/stdc++.h> using namespace std; #define M 1000005 #define N 1005 int f[N]; int a[M], b[M], k; int n, m; void init() { for (int i = 1; i <= n; ++i) .

Sqssq的博客 276

相机光学(三十八)——VCM(Voice Coil Motor)音圈马达

VCM(Voice Coil Motor)音圈马达全称Voice Coil Montor,电子学里面的音圈电机,是马达的一种。因为原理和扬声器类似,所以叫音圈电机,具有高频响、高精度的特点。其主要原理是在一个永久磁场内,通过改变马达内线圈的直流电流大小,来控制弹簧片的拉伸位置,从而带动上下运动。手机摄像头广泛的使用VCM实现自动对焦功能,通过VCM可以调节镜头的位置,呈现清晰的图像。弹片式结构滚珠式结构摩擦式结构Open loop开马达Close loop闭环马达。

图像学习之旅 7224

实战:爬取某小程序的订单数据(解析小程序 API 参数)

本文以美团外卖小程序为例,详细分析了电商类小程序订单数据接口的技术实现与采集方法。首先通过抓包定位核心接口,解析其请求参数;重点破解了token验证机制与sign签名加密算法,通过逆向分析还原参数生成逻辑;最后基于Python实现了订单数据的自动化爬取与存储。文章强调数据采集需遵守法律合规性,提供应对反爬机制的策略,并指出了必须在获得合法授权的前提下使用这些技术。该研究为数据采集工程师提供了实用的技术参考,但必须严格遵循"最小必要"原则,确保不违反平台规则和用户隐私保护法规。

2503_91057718的博客 2317

PAT(甲级) 1013 Battle Over Cities (25point(s))

题目 题目链接 思路 这道题大意是给你一个连通图,删去某一点后,求有几个连通分量; 用邻接矩阵法存储图,因为删去城市有K种可能,所以要循环K次; 每次循环时都要依次dfs检查每个城市,看这个城市与哪个城市相连,共循环N ^ 2次 知识点 二维vector的初始化方法; 在Java中用户不能调用构造方法,总是用new通过系统调用,所以不会出现类型()直接初始化的方法; 但c++允许直接调用构造方法,...

hjyp的博客 252

第一章 电力电子仿真软件 PLECS简介

PLECS是瑞士Plexim公司开发的电力电子仿真软件,具有快速仿真、丰富元件库和热分析功能。软件提供单机版和嵌入MATLAB的版本,支持多平台安装且流程简单。首次运行需获取授权文件,提供60分钟演示模式。界面包含模型创建、帮助文档和元件库浏览器等功能区域,操作便捷。该软件是专业高效的电力电子系统仿真工具。

502

1013 Battle Over Cities (25 point(s))

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

佳迪斯的博客 186

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

1013 Battle Over Cities (25 point(s)) - C语言 PAT 甲级

图的遍历 * 实际上本题是求,图中删除一个节点后,剩余图的连通分量是多少; * 添加的最少路线,就是连通分量数 - 1;

有三只土豆 308

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

1013 Battle Over Cities (25)(25 point(s))

problem 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 i...

weixin_30599769的博客 109

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

PAT (Advanced Level) Practice 1013 Battle Over Cities (25 point(s))

题目:PAT A 1013 题目大意 给出N个城市,M条边连通的图。有K次询问,每次询问,去掉一个点,则连通全部点需要几条边 解题思路 连通分量的个数。对每个点,去掉以后,查看还有多少个联通分量,答案为连通分量个数-1 #include<bits/stdc++.h> using namespace std; const int maxn=1e3+5; int n,m,k; vector<int>mp[maxn]; bool vis[maxn]; void dfs(int s){

helloful的博客 130

1013. Battle Over Cities (25) 连通图

1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It is vitally important to have all the

a716121的专栏 655

1013 Battle Over Cities (DFS+连同分量)

1013 Battle Over Cities (25 point(s)) 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 clo...

alex1997222的博客 220

[PAT]1013. Battle Over Cities (25)@Java

1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It is vitally important to have all the

zjkC050818的博客 653

1013 Battle Over Cities (25)

告诉你有N座城市,然后有K条路,问你如果一座城掉了,要把其他所有的城连起来,要修几条路。 就简单的深搜就可以搞定了。数据比较少,多的话会超时,打个表就行了。#include <cstdio> #include <vector> #include <string> #include <cstring> #include <cstdlib> #include <iostream> #include <

AvaloNero的博客 417

1013. Battle Over Cities (25)-PAT甲级真题(图的遍历,统计强连通分量的个数,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

柳婼 の blog 3028

1013 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...

Imagirl1的博客 358

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

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

日沉云起的博客 1941

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 m...

旺崽的博客 2809
上一篇: 1012 The Best Rank (25 point(s))
下一篇: 1015 Reversible Primes (20 point(s))
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值