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

  1. 3 2
  2. 1 2
  3. 1 3
  4. 4 3
  5. 1 2
  6. 3 2
  7. 4 2
  8. 0 0

Sample Output

  1. Case 1: 0
  2. Case 2: 2
  3.  
  4. 要用邻接表存一下不能走的地方,然后预处理一下,直接用二维的数组存会MLE,还有尽量用C++交,G++容易T
    代码:
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. #include<queue>
  6. #include<stack>
  7. #include<set>
  8. #include<map>
  9. #include<vector>
  10. #include<cmath>
  11. #define Inf 0x3f3f3f3f
  12.  
  13. const int maxn=1e4+;
  14. typedef long long ll;
  15. using namespace std;
  16.  
  17. bool book[maxn];
  18. bool vis[maxn];
  19. vector<int>vec[maxn];
  20. int n,m;
  21. int bfs()
  22. {
  23. queue<int>q;
  24. book[]=true;
  25. q.push();
  26. int ans=;
  27. while(!q.empty())
  28. {
  29. int tmp=q.front();
  30. q.pop();
  31. memset(vis,false,sizeof(vis));
  32. for(int t=;t<vec[tmp].size();t++)
  33. {
  34. vis[vec[tmp][t]]=true;
  35. }
  36. for(int t=;t<=n;t++)
  37. {
  38. if(vis[t]==false&&book[t]==false)
  39. {
  40. ans++;
  41. q.push(t);
  42. book[t]=true;
  43. }
  44. }
  45. }
  46. return ans;
  47. }
  48. int main()
  49. {
  50. int cnt=;
  51. while(scanf("%d%d",&n,&m)!=EOF)
  52. {
  53. if(n==&&m==)
  54. {
  55. break;
  56. }
  57. for(int t=;t<=n;t++)
  58. {
  59. vec[t].clear();
  60. }
  61. memset(book,false,sizeof(book));
  62. int a,b;
  63. for(int t=;t<m;t++)
  64. {
  65. scanf("%d%d",&a,&b);
  66. vec[a].push_back(b);
  67. vec[b].push_back(a);
  68. }
  69. int ans=bfs();
  70. printf("Case %d: %d\n",cnt++,ans);
  71.  
  72. }
  73.  
  74. return ;
  75. }

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. slots属性(省内存,限制属性的定义)

    class Foo: __slots__=['name','age'] #{'name':None,'age':None} # __slots__='name' #{'name':None,'age' ...

  2. Linux 文件IO简单实例

    目录 简述 代码 编译运行 简述 Linux下的所有资源都被抽象为文件,所以对所有资源的访问都是以设备文件的形式访问,设备文件的操作主要包括:打开.关闭.读.写.控制.修改属性等.下面的示例代码主要是 ...

  3. Azure DevOps+Docker+Asp.NET Core 实现CI/CD(一 .简介与创建自己的代理池)

    前言 本文主要是讲解如何使用Azure DevOps+Docker 来实现持续集成Asp.NET Core项目(当然 也可以是任意项目). 打算用三个篇幅来记录完整的全过程 觉得有帮助的朋友~可以左上 ...

  4. DIFF算法浅析(三)在react中的实现

    在虚拟dom中diff的实现. 分别从4个方面: DIFF抽象概念(概述.时间复杂性分析) 在Vue2中的实现(版本2.6.11.必要性.执行方式) 在React中的实现(版本16.13.1,必要性. ...

  5. C#LeetCode刷题之#225-用队列实现栈(Implement Stack using Queues)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4106 访问. 使用队列实现栈的下列操作: push(x) -- ...

  6. CSS动画实例:升空的气球

    1.制作一个气球 设页面中有<div class="balloon"></div>,为. balloon设置样式规则如下: .balloon { heigh ...

  7. 前端路由、后端路由——想要学好vue-router 或者 node.js 必须得明白的两个概念

    前端路由和后端路由的概念讲解 引言 正文 一.路由的概念 二.后端路由 三.前端路由 四.其他知识 结束语 引言 无论你是正在学习vue 还是在学习node, 你一定会碰到前端路由和后端路由这两个概念 ...

  8. JQ选择器总结

    jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法 $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个 ...

  9. Javascript模块化编程(二):AMD规范 (转)

    原文作者: 阮一峰 日期: 2012年10月30日 这个系列的第一部分介绍了Javascript模块的基本写法,今天介绍如何规范地使用模块. (接上文) 七.模块的规范 先想一想,为什么模块很重要? ...

  10. Qt 实现 异形 窗体&按钮

    //关键部分代码如下//设置异形窗体 //setWindowOpacity(0.5);//设置窗体透明度 0完全透明,1完全不透明 this->setWindowFlag(Qt::Framele ...