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 ...
随机推荐
- 03-Thread类中的常用方法
Thread类中的常用的方法: * 1. start():启动当前线程:调用当前线程的run() * 2. run(): 通常需要重写Thread类中的此方法,将创建的线程要执行的操作声明在此方法中 ...
- 解读生成对抗网络(GAN) 之U-GAN-IT
Unsupervised Generative Attentionnal Networks with Adapter Layer-In(U-GAN-IT) 从字面我们可以理解为无监督生成对抗网络和适配 ...
- tomcat启动失败的解决办法
初次安装tomcat启动失败的解决办法: 1.CATALINA_HOME C:\Program Files\apache-tomcat-8.5.242.path %CATALINA_HOME% ...
- 2020-05-31:假如Redis里面有1亿个key,其中有10w个key是以某个固定的已知的前缀开头的,如何将它们全部找出来?
福哥答案2020-05-31: 使用keys指令可以扫出指定模式的key列表.对方接着追问:如果这个redis正在给线上的业务提供服务,那使用keys指令会有什么问题?这个时候你要回答redis关键的 ...
- 2020-04-06:insert语句在mysql里经历了什么?
1.会话状态转换为update 2.激活事物状态由 not_active 变为 active 3.查找定位数据 4.进行乐观插入 记录insert的undo记录记录undo的redo log 入red ...
- C#LeetCode刷题之#859-亲密字符串(Buddy Strings)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3973 访问. 给定两个由小写字母构成的字符串 A 和 B ,只要 ...
- CODING DevOps 代码质量实战系列最后一课,周四发车
随着 ToB(企业服务)的兴起和 ToC(消费互联网)产品进入成熟期,线上故障带来的损失越来越大,代码质量越来越重要,而「质量内建」正是 DevOps 核心理念之一. <DevOps 代码质量实 ...
- mycat数据库集群系列之数据库多实例安装
mycat数据库集群系列之数据库多实例安装 最近在梳理数据库集群的相关操作,现在花点时间整理一下关于mysql数据库集群的操作总结,恰好你又在看这一块,供一份参考.本次系列终结大概包括以下内容:多数据 ...
- Android studio 在项目里配置签名 + cmd命令安装apk在测试机
一.在项目里配置签名 搜索百度里有很多可视化操作在项目里配置签名,但是对于已经有签名的旧项目来说,用语句是最方便的. 方法: 第一步:把签名文件放到项目中,和build.gradle的同一级目录下.当 ...
- API、Win32 SDK、Win32项目、MFC、Windows窗体应用程序的区别
[原]API.Win32 SDK.Win32项目.MFC.Windows窗体应用程序的区别 首先来看一下每一个术语的定义: API:Application Programming Interface. ...