PAT甲级1013 Battle Over Cities------图的遍历

2024-02-14 15:08

本文主要是介绍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 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。求连通分量就涉及到图的遍历,感觉DFS写起来跟简单一些。

代码如下:

#include <iostream>
#include<algorithm>
using namespace std;
int edge[1000][1000]={0},flag[1000],n;
void DFS(int i)
{flag[i]=1;for(int j=1;j<n+1;j++){if(edge[i][j]&&!flag[j]) DFS(j);}
}
int main()
{int m,k,a,b;scanf("%d%d%d",&n,&m,&k);for(int i=0;i<m;i++){scanf("%d%d",&a,&b);edge[a][b]=edge[b][a]=1;}while(k--){fill(flag,flag+1000,0);int count=0;scanf("%d",&a);flag[a]=1;for(int i=1;i<n+1;i++)if(!flag[i]) {count++;DFS(i);}printf("%d\n",count-1);}return 0;
}

来总结一下:
1)这个题刚开始我还构造了另外一个数组,用来存储每一次检查时(就是去掉了一个边时)的邻接矩阵,后来发现,只要将该点加入集合(flag),就可以不会再访问到和该点有关的值。

2)本来最后一个测时点一直超时,后来将所有的cin和cout都换成scanf和printf后就AC了。

这篇关于PAT甲级1013 Battle Over Cities------图的遍历的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/708833

相关文章

leetcode105 从前序与中序遍历序列构造二叉树

根据一棵树的前序遍历与中序遍历构造二叉树。 注意: 你可以假设树中没有重复的元素。 例如,给出 前序遍历 preorder = [3,9,20,15,7]中序遍历 inorder = [9,3,15,20,7] 返回如下的二叉树: 3/ \9 20/ \15 7   class Solution {public TreeNode buildTree(int[] pr

PHP实现二叉树遍历(非递归方式,栈模拟实现)

二叉树定义是这样的:一棵非空的二叉树由根结点及左、右子树这三个基本部分组成,根据节点的访问位置不同有三种遍历方式: ① NLR:前序遍历(PreorderTraversal亦称(先序遍历)) ——访问结点的操作发生在遍历其左右子树之前。 ② LNR:中序遍历(InorderTraversal) ——访问结点的操作发生在遍历其左右子树之中(间)。 ③ LRN:后序遍历(PostorderT

安卓玩机工具------小米工具箱扩展工具 小米机型功能拓展

小米工具箱扩展版                     小米工具箱扩展版 iO_Box_Mi_Ext是由@晨钟酱开发的一款适用于小米(MIUI)、多亲(2、2Pro)、多看(多看电纸书)的多功能工具箱。该工具所有功能均可以免root实现,使用前,请打开开发者选项中的“USB调试”  功能特点 【小米工具箱】 1:冻结MIUI全家桶,隐藏状态栏图标,修改下拉通知栏图块数量;冻结

react笔记 8-17 属性绑定 class绑定 引入图片 循环遍历

1、绑定属性 constructor(){super()this.state={name:"张三",title:'我是一个title'}}render() {return (<div><div>aaaaaaa{this.state.name}<div title={this.state.title}>我是一个title</div></div></div>)} 绑定属性直接使用花括号{}   注

hashmap的存值,各种遍历方法

package com.jefflee;import java.util.HashMap;import java.util.Iterator;import java.util.Map;public class HashmapTest {// 遍历Hashmap的四种方法public static void main(String[] args) {//hashmap可以存一个null,把

Knight Moves -uva 简单的BFS遍历

昨天刚学了BFS的遍历,在uva上找了个题敲了出来,感觉还不错,最近敲代码挺有手感的,希望这种状态保持下去 #include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#define MAX_SIZE 10 + 5#define LEN 100 + 10using namespace std;in

笔试强训,[NOIP2002普及组]过河卒牛客.游游的水果大礼包牛客.买卖股票的最好时机(二)二叉树非递归前序遍历

目录 [NOIP2002普及组]过河卒 牛客.游游的水果大礼包 牛客.买卖股票的最好时机(二) 二叉树非递归前序遍历 [NOIP2002普及组]过河卒 题里面给的提示很有用,那个马的关系,后面就注意,dp需要作为long的类型。 import java.util.Scanner;// 注意类名必须为 Main, 不要有任何 package xxx 信息publ

二叉树的层次遍历(10道)

(写给未来遗忘的自己) 102.二叉数的层序遍历(从上到下) 题目: 代码: class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> result; queue<TreeNode*> node; if (root == nullptr) {

关于双指针遍历

今晚跟一个朋友突然问我,你懂双指针遍历吗?并叫我敲出代码。当时自己愣住了,但是还是写出来了,第一个版本是: #include <iostream> using namespace std; int main(int argc, char** argv, char** arge) { cout<<"输出参数个数:"<<argc<<endl; cout<<"输出字符串数组:"<<endl; f

C语言手撕实战代码_二叉树_构造二叉树_层序遍历二叉树_二叉树深度的超详细代码实现

二叉树习题1.通过前序序列和中序序列构造二叉树2.通过层次遍历序列和中序序列创建一棵二叉树3.求一棵二叉树的结点个数4.求一棵二叉树的叶子节点数5.求一棵二叉树中度为1的节点个数6.求二叉树的高度7.求一棵二叉树中值为x的节点作为根节点的子树深度8.判断两棵树是否相似,相似返回1,不相似返回09.设计算法利用叶子结点中的空指针域将所有叶子结点链接成一个带头结的双链表10.假设一个仅包含二元运算的算