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. 关于海思SDK在Ubuntu下安装错误问题

    在Ubuntu下安装海思sdk时,直接运行安装脚本,会有如下提示: ./sdk.unpack  ./sdk.unpack: 2: ./sdk.unpack: source: not found ./s ...

  2. 精讲RestTemplate第3篇-GET请求使用方法详解

    本文是精讲RestTemplate第3篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 精讲RestTemplate第2篇-多种底层H ...

  3. Java高级篇XML和正则表达式

    常见的XML解析技术: 1.DOM(基于XML树结构,比较耗资源,适用于多次访问XML): 2.SAX(基于事件,消耗资源小,适用于数量较大的XML): 3.JDOM(比DOM更快,JDOM仅使用具体 ...

  4. Docker 快速搭建 MySQL8 开发环境

    使用 Docker 快速搭建一个 MySQL8 开发环境 步骤 获取镜像 docker pull mysql:8 启动容器,密码 123456,映射 3306 端口 docker run --name ...

  5. java_流程控制语句、权限修饰符

    判断语句 if语句第一种格式: if if(关系表达式){ 语句体; } if语句第二种格式: if…else if(关系表达式) { 语句体1; } else { 语句体2; } if语句第三种格式 ...

  6. 2020-06-16:Redis hgetall时间复杂度?

    福哥答案2020-06-16: 时间复杂度是O(N).时间复杂度:O(N) where N is the size of the hash.

  7. C#LeetCode刷题-链表

    链表篇 # 题名 刷题 通过率 难度 2 两数相加   29.0% 中等 19 删除链表的倒数第N个节点   29.4% 中等 21 合并两个有序链表 C#LeetCode刷题之#21-合并两个有序链 ...

  8. JavaScript 手写 setTimeout 及 同步调用和异步调用

    demo let timeout = (sec, num) => { const now = new Date().getTime() // 获取进入方法时的时间 let flag = true ...

  9. DRF基础操作流程

    Django Rest_Framework 核心思想: 缩减编写api接口的代码 -->DRF Django REST framework是一个建立在Django基础之上的Web 应用开发框架, ...

  10. ubuntu 绝望事件

    @ubuntu.com hi!大家好,早上发生了很有意思的事情 显示器分辨率(x2) 系统 2560x1440 Ubuntu 20.04.1 LTS 上面的表格是现在的环境 开机进入锁屏页面,正常显示 ...