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 调用 JS 的几种方式
1. 前言 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类人,我给大 ...
- OpenCV常用图像拼接方法(一) :直接拼接
OpenCV常用图像拼接方法将分为四部分与大家分享,这里是第一种方法,欢迎关注后续. OpenCV常用图像拼接方法(一) :直接拼接,俗称硬拼,就是简单的将两张图片合并成一张大图. 方法比较简单,这里 ...
- java Struts 多种表单写法
1.html:form(struts标签) 缺点:必须指定一个有效的action属性. 优点:可以使用struts token机制. 调用方法通过submit的name属性. <table al ...
- 2020-04-23:假设一个订单的编号规则是AAAAOrder2020-0000001,AAAAOrder2020-0000002....后面的数字是自增长,如果订单号码达到AAAAOrder2020-1000000(100万),数据库中应该有100万条数据,此时我随机删除2条数据(物理删除,且不考虑日志和备份),请问怎么找到删掉的数据的编号?给出解题思路即可,答案需要在1秒内运行得到。
福哥答案2020-04-23: 分批查询:分成500次count(),每次count()肯定小于等于2000条数据,经过测试,一次count()在.1ms左右,500次就是500ms.二分法(时间微超 ...
- JavaScript 回调函数的简单示例
回调函数 回调函数也被称为高阶函数 所谓高阶函数,就是说值 函数作为参数被传递或者返回值输出 操作函数的函数称为 高阶函数 把一段可执行的代码(一个函数)作为参数传递给其他的代码(另一个函数),并在需 ...
- Vue Vue-loader / VueLoaderPlugin / Webpack
在不用VueCli创建项目的时候,手写引入vue的时候,配置webpack的时候发现了这个问题 webpack vue-loader was used without the correspondin ...
- Elasticsearch+SpringBoot报NoNodeAvailableException解决方案
Elasticsearch整合SpringBoot 首先大家在整合的时候一定要注意版本兼容问题,此问题尤为重要 Elasticsearch简称Es 在使用SpringBoot整合Elasticsear ...
- Kubernetes 的层级命名空间介绍
原文链接:https://fuckcloudnative.io/posts/introducing-hierarchical-namespaces/ 在单个 Kubernetes 集群上安全托管大量用 ...
- Jmeter 常用函数(19)- 详解 __BeanShell
如果你想查看更多 Jmeter 常用函数可以在这篇文章找找哦 https://www.cnblogs.com/poloyy/p/13291704.htm 作用 执行 BeanShell 脚本,并返回结 ...
- js中的寄生组合继承
function inheritProperty(subType, superType) { function F(){} F.prototype = superType.prototype; sup ...