I - 乓 (BFS+邻接表)
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+邻接表)的更多相关文章
- bfs 邻接表(需要优化 可能会RE *【模板】)
//---基于邻接表的bfs #include <stdio.h> #include <string.h> #include <iostream> #include ...
- bfs 邻接表
#include<stdio.h> #include<stdlib.h> #include<string.h> struct node { int date; st ...
- ACM/ICPC 之 数据结构-邻接表+BFS(TSH OJ-无线广播Broadcast)
这道题中若能够构成互不干扰的区域,其构成的图其实就是汉密尔顿路(Hamilton road),因此如果能够观察出来可以直接转化为汉密尔顿路的存在性证明,即便不能观察,我相信ACMer也能转化为BFS问 ...
- 数据结构 《2》----基于邻接表表示的图的实现 DFS(递归和非递归), BFS
图通常有两种表示方法: 邻接矩阵 和 邻接表 对于稀疏的图,邻接表表示能够极大地节省空间. 以下是图的数据结构的主要部分: struct Vertex{ ElementType element; // ...
- 数据结构学习笔记05图 (邻接矩阵 邻接表-->BFS DFS、最短路径)
数据结构之图 图(Graph) 包含 一组顶点:通常用V (Vertex) 表示顶点集合 一组边:通常用E (Edge) 表示边的集合 边是顶点对:(v, w) ∈E ,其中v, w ∈ V 有向边& ...
- 邻接表实现Dijkstra算法以及DFS与BFS算法
//============================================================================ // Name : ListDijkstr ...
- 数据结构(11) -- 邻接表存储图的DFS和BFS
/////////////////////////////////////////////////////////////// //图的邻接表表示法以及DFS和BFS //////////////// ...
- 用邻接表实现DFS和BFS
#include <stdio.h> #include <stdlib.h> #define MAXVERTEX 10 typedef char VertexType; //顶 ...
- 无向图的 DFS 和 BFS实现 (以邻接表存储的图)
#include <iostream> #include <queue> using namespace std; #define MaxVertexNum 10 typede ...
随机推荐
- 【编写程序中经常犯的一些错误】 Python | 面向对象(一)
[编写程序中经常犯的一些错误]Python | 面向对象(一) 在学习Python的面向对象这一部分时,经常出现以下错误: 这是错误范例,请仔细甄别: class Person: def __int_ ...
- RxJS 中的观察者和迭代器模式
目录 前言 观察者模式 迭代器模式 RxJS 中两种模式的结合和实现 小结 参考 1. 前言 RxJS 是一个库,它通过使用observable(可观察对象)序列来编写异步和基于事件的程序.其结合了观 ...
- 开启CAN通信学习(二)——基于Kvaser的CAN通信案例
1 案例硬件介绍 Kvaser是瑞典的一家专门提供CAN和LIN总线分析仪及数据记录仪的公司,在CAN产品开发领域已经有近30年的经验,本案例选择的CAN通信硬件型号是Kvaser Leaf Ligh ...
- 3、Java 对象和类
1.理解Java中的类和对象 对象: 作为学习计算机专业的很有意思,跟朋友开玩笑说,我有很多对象,没有就new一个.对象可以说是类的实例,通过类的构造方法得到的一个对象实例.它拥有此对象应有的行为与方 ...
- WordPress固定链接后404的解决方法
一般Wordpress自带的链接是一大串数字加符号,不美观也不明确,一般要设置成固定链接,而设置成功后大部分情况访问文章,子链接都会出现404界面,我找了网上的资料,才知道需要配置伪静态链接, 一般的 ...
- MySQL回表查询
一.MySQL索引类型 1.普通索引:最基本的索引,没有任何限制 2.唯一索引(unique index):索引列的值必须唯一,但是允许为空 3.主键索引:特殊的唯一索引,但是不允许为空,一般在建表的 ...
- JavaScript String.prototype 原型
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...
- C#设计模式之7-桥接模式
桥接模式(Bridge Pattern) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/401 访问. 桥接模式属于结构 ...
- Windows下,配置VS Code的Java开发环境
Windows下,配置VS Code的Java开发环境 前言 最近痴迷于VS Code的开发环境配置,原因就在于它的轻巧和免费,还能当一个非常棒的文本编辑器.如果之前你配置过VS Code并且失败了, ...
- DAPP开发初探——永存的留言
转载地址 https://blog.csdn.net/qq_33764491/article/details/80570266 前言 最近DAPP的开发貌似很火,学习了区块链的一些知识之后,相信有很多 ...