USTC campus network is a huge network. There is a bi-directional link between every pair of computers in the network. One of the computers is the BBS server, which is so popular that thousands of people log on it every day. Recently some links of the network are damaged by the rainstorm. The network administrator is going to check which computers are still connected with the BBS server directly or indirectly.

You are to help the administrator to report the number of computers still connecting with the BBS server (not including itself).

InputThe input consists of multiple test cases. Each test case starts with a line containing two integers N and M (1 ≤ N ≤ 10,000, 0 ≤ M ≤ 1,000,000), which are the number of computers and the number of damaged links in USTC campus network, respectively. The computers are numbered from 1 to N and computer 1 is the BBS server. 
Each of the following M lines contains two integers A and B(1 ≤ A ≤ N, 1 ≤ B ≤ N, A ≠ B), which means the link between computer A and B is damaged. A link will appear at most once.

The last test case is followed by a line containing two zeros.OutputFor each test case, print a line containing the test case number( beginning with 1) followed by the number of computers still connecting with the BBS server.Sample Input

3 2
1 2
1 3
4 3
1 2
3 2
4 2
0 0

Sample Output

Case 1: 0
Case 2: 2 要用邻接表存一下不能走的地方,然后预处理一下,直接用二维的数组存会MLE,还有尽量用C++交,G++容易T
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#define Inf 0x3f3f3f3f const int maxn=1e4+;
typedef long long ll;
using namespace std; bool book[maxn];
bool vis[maxn];
vector<int>vec[maxn];
int n,m;
int bfs()
{
queue<int>q;
book[]=true;
q.push();
int ans=;
while(!q.empty())
{
int tmp=q.front();
q.pop();
memset(vis,false,sizeof(vis));
for(int t=;t<vec[tmp].size();t++)
{
vis[vec[tmp][t]]=true;
}
for(int t=;t<=n;t++)
{
if(vis[t]==false&&book[t]==false)
{
ans++;
q.push(t);
book[t]=true;
}
}
}
return ans;
}
int main()
{
int cnt=;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==)
{
break;
}
for(int t=;t<=n;t++)
{
vec[t].clear();
}
memset(book,false,sizeof(book));
int a,b;
for(int t=;t<m;t++)
{
scanf("%d%d",&a,&b);
vec[a].push_back(b);
vec[b].push_back(a);
}
int ans=bfs();
printf("Case %d: %d\n",cnt++,ans); } return ;
}

I - 乓 (BFS+邻接表)的更多相关文章

  1. bfs 邻接表(需要优化 可能会RE *【模板】)

    //---基于邻接表的bfs #include <stdio.h> #include <string.h> #include <iostream> #include ...

  2. bfs 邻接表

    #include<stdio.h> #include<stdlib.h> #include<string.h> struct node { int date; st ...

  3. ACM/ICPC 之 数据结构-邻接表+BFS(TSH OJ-无线广播Broadcast)

    这道题中若能够构成互不干扰的区域,其构成的图其实就是汉密尔顿路(Hamilton road),因此如果能够观察出来可以直接转化为汉密尔顿路的存在性证明,即便不能观察,我相信ACMer也能转化为BFS问 ...

  4. 数据结构 《2》----基于邻接表表示的图的实现 DFS(递归和非递归), BFS

    图通常有两种表示方法: 邻接矩阵 和 邻接表 对于稀疏的图,邻接表表示能够极大地节省空间. 以下是图的数据结构的主要部分: struct Vertex{ ElementType element; // ...

  5. 数据结构学习笔记05图 (邻接矩阵 邻接表-->BFS DFS、最短路径)

    数据结构之图 图(Graph) 包含 一组顶点:通常用V (Vertex) 表示顶点集合 一组边:通常用E (Edge) 表示边的集合 边是顶点对:(v, w) ∈E ,其中v, w ∈ V 有向边& ...

  6. 邻接表实现Dijkstra算法以及DFS与BFS算法

    //============================================================================ // Name : ListDijkstr ...

  7. 数据结构(11) -- 邻接表存储图的DFS和BFS

    /////////////////////////////////////////////////////////////// //图的邻接表表示法以及DFS和BFS //////////////// ...

  8. 用邻接表实现DFS和BFS

    #include <stdio.h> #include <stdlib.h> #define MAXVERTEX 10 typedef char VertexType; //顶 ...

  9. 无向图的 DFS 和 BFS实现 (以邻接表存储的图)

    #include <iostream> #include <queue> using namespace std; #define MaxVertexNum 10 typede ...

随机推荐

  1. 8、Builder 建造者模式 组装复杂的实例 创造型模式

    1.什么是Builder模式 定义: 将一个复杂对象的构建与表示相分离,使得同样的构建过程可以创建不同的表示.大白话就是,你不需要知道这个类的内部是什么样的,只用把想使用的参数传进去就可以了,达到了解 ...

  2. 1、Java 开发环境配置

    Java 开发环境配置 在本章节中我们将为大家介绍如何搭建Java开发环境. Windows 上安装开发环境 Linux 上安装开发环境 安装 Eclipse 运行 Java window系统安装ja ...

  3. EasyExcel的基本使用方法

    在Java语言领域,说到Excel处理工具,大家首先想到的可能是阿帕奇的poi,poi在处理数据量不大的excel文件上确实非常强大,但是随着后来excel从03(一个excel文件中最多有65536 ...

  4. Nginx WebUI管理

    简介 NginxWebUI是一款方便实用的nginx 网页配置工具,可以使用 WebUI 配置 Nginx 的各项功能,包括端口转发,反向代理,ssl 证书配置,负载均衡等,最终生成「nginx.co ...

  5. Springboot快速入门篇,图文并茂

    Springboot快速入门篇,图文并茂 文章已托管到GitHub,大家可以去GitHub查看阅读,欢迎老板们前来Star!搜索关注微信公众号 [码出Offer] 领取各种学习资料! image-20 ...

  6. 制作 macOS Sierra U盘USB启动安装盘方法教程 (亲测)

    备注:相关镜像到apple官网下载 https://discussionschinese.apple.com/thread/250596904 进去点击"请使用这个 App Store 链接 ...

  7. C#LeetCode刷题之#717-1比特与2比特字符( 1-bit and 2-bit Characters)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3740 访问. 有两种特殊字符.第一种字符可以用一比特0来表示.第 ...

  8. Linux系统安装Nginx(Centos7)

    Nginx是一款轻量级的网页服务器.反向代理服务器.它最常的用途是提供反向代理服务,还可以做负载均衡.相较于Apache.lighttpd具有占有内存少,稳定性高等优势.服务端很多场景都需要使用,这篇 ...

  9. 5招详解linux之openEuler /centos7防火墙基本使用指南

    防火墙是一种防火墙管理解决方案,可用于许多 Linux 发行版,它充当 Linux 内核提供的 iptables 数据包筛选系统的前端.在本指南中,将介绍如何为服务器设置防火墙,并向你展示使用管理工具 ...

  10. Java中访问控制修饰符的详解和示例——Java学习

    Java中的四个访问控制修饰符 简述 在Java中共有四个: public -- 对外部完全可见 protected -- 对本包和所有子类可见 默认(不需要修饰符)-- 对本包可见 private ...