题目链接

https://www.patest.cn/contests/gplt/L2-013

思路

可以通过图的连通块个数来判断

假如 一座城市的失去 改变了其他城市之间的连通性

那么 这座城市本来所在的连通块 就会被分裂成为 两个以上的连通块

加上 这座城市 被分裂出来 又多了 一个连通块

所以 在每次失去的时候 我们深搜 判断一下 连通块个数

如果

失去后的连通块个数 > 原来连通块个数 + 1 那么 就要发出红色警报了

要记得 每次更新一下 连通块 个数

每次都要更新

保证每次判断 都是在上一次失去的基础上 判断的

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll; const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-6; const int INF = 0x3f3f3f3f;
const int maxn = 5e2 + 5;
const int MOD = 1e9 + 7; int G[maxn][maxn];
int v[maxn]; int n, m; void dfs(int x)
{
v[x] = 1;
for (int i = 0; i < n; i++)
{
if (v[i] == 0 && G[x][i] == 1)
dfs(i);
}
} void init()
{
CLR(G);
CLR(v);
} int main()
{
scanf("%d%d", &n, &m);
int x, y;
for (int i = 0; i < m; i++)
{
scanf("%d%d", &x, &y);
G[x][y] = G[y][x] = 1;
}
int fin = 0;
for (int i = 0; i < n; i++)
{
if (v[i] == 0)
{
dfs(i);
fin++;
}
}
int k;
scanf("%d", &k);
for (int i = 0; i < k; i++)
{
scanf("%d", &x);
for (int j = 0; j < n; j++)
G[x][j] = G[j][x] = 0;
int temp = 0;
CLR(v);
for (int j = 0; j < n; j++)
{
if (v[j] == 0)
{
dfs(j);
temp++;
}
}
if (temp > fin + 1)
printf("Red Alert: City %d is lost!\n", x);
else
printf("City %d is lost.\n", x);
fin = temp;
if (i == n - 1)
printf("Game Over.\n");
}
}

PAT 天梯赛 L2-013. 红色警报 【BFS】的更多相关文章

  1. PAT天梯赛练习题 L2-013 红色警报(并查集+逆序加边)

    L2-013. 红色警报 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 战争中保持各个城市间的连通性非常重要.本题要求你编写一 ...

  2. 天梯赛决赛 L2-1.红色警报 并查集

    L2-013. 红色警报 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 战争中保持各个城市间的连通性非常重要.本题要求你编写一 ...

  3. PAT 天梯赛 L3-008. 喊山 【BFS】

    题目链接 https://www.patest.cn/contests/gplt/L3-008 思路 因为 每个山头 最多有两个 能听到它的 临近山头 那么 我们就可以 给每个 山头 都 分配 最多两 ...

  4. PTA天梯赛L2

    L2-001 紧急救援 题意:就是给你一张n<500的图:让你求最短路径,最短路条数,以及路径: 做法,先用dijkstra求最短路,然后dfs找最短路条数,以及点权的最大值: 一般dfs不就可 ...

  5. 团体程序设计天梯赛(CCCC) L3014 周游世界 BFS证明

    团体程序设计天梯赛代码.体现代码技巧,比赛技巧.  https://github.com/congmingyige/cccc_code

  6. PAT天梯赛 L1-049 天梯赛座位分配

    题目链接:点击打开链接 天梯赛每年有大量参赛队员,要保证同一所学校的所有队员都不能相邻,分配座位就成为一件比较麻烦的事情.为此我们制定如下策略:假设某赛场有 N 所学校参赛,第 i 所学校有 M[i] ...

  7. PAT天梯赛L3-007 天梯地图

    题目链接:点击打开链接 本题要求你实现一个天梯赛专属在线地图,队员输入自己学校所在地和赛场地点后,该地图应该推荐两条路线:一条是最快到达路线:一条是最短距离的路线.题目保证对任意的查询请求,地图上都至 ...

  8. PAT天梯赛练习题——L3-007. 天梯地图(多边权SPFA)

    L3-007. 天梯地图 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题要求你实现一个天梯赛专属在线地图,队员输入自己学校 ...

  9. PAT 天梯赛 L2-016. 愿天下有情人都是失散多年的兄妹 【BFS】

    题目链接 https://www.patest.cn/contests/gplt/L2-016 思路 用BFS 每层 遍历当代 并且查找当代是否有重复 有重复就跳出 然后 POP 并且将他们的下一代 ...

随机推荐

  1. python笔记3:注释命名风格

    6.注释: 行注释采用  # 开头,多行注释使用三个单引号 (''') 或三个双引号 ("' '"),注释不需要对齐 三引号让程序员从引号和特殊字符串的泥潭里面解脱出来,自始至终保 ...

  2. BZOJ——1623: [Usaco2008 Open]Cow Cars 奶牛飞车

    http://www.lydsy.com/JudgeOnline/problem.php?id=1623 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 6 ...

  3. Careercup | Chapter 5

    5.1 You are given two 32-bit numbers, N andM, and two bit positions, i and j. Write a method to inse ...

  4. linux time

    uint32_t midtime; static struct timeval startstart,midmid; while (ros::ok()) { gettimeofday(&sta ...

  5. c实现的trim函数

    功能:去掉字符串首尾的空格,换行符等空白. 代码: #include <string.h> #include <stdio.h> #include <ctype.h> ...

  6. 2017 [六省联考] T6 寿司餐厅

    4873: [Shoi2017]寿司餐厅 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 450  Solved: 316[Submit][Status ...

  7. scp 时出现permission denied

    如果scp到 /usr/local/目录下,则无权限,可更改到用户目录下

  8. GDB调试动态链接库

    http://cyukang.com/2012/06/25/gdb-with-libso.html http://cyukang.com/2011/05/06/valgrind.html

  9. favico是针对网页图标内容更改

    favico.js源码 (function () { var Favico = function (opt) { "use strict"; opt = opt ? opt : { ...

  10. 第一个Hello,OS World操作系统

    来自:清泛网 - http://www.tsingfun.com/html/2015/dev_0804/hello_os_word_my_first_os.html 首先阐述下程序运行的基本原理:计算 ...