dfs判断图的连通块数量~

#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
const int maxn=1e6+;
vector<int> g[maxn];
int visit[maxn],N,M,x,y,flag;
void init () {
fill (visit,visit+maxn,);
for (int i=;i<maxn;i++) g[i].clear();
}
void dfs (int v) {
visit[v]=true;
if (g[v].size()!=) flag=;
for (int i=;i<g[v].size();i++)
if (!visit[g[v][i]]) dfs (g[v][i]);
}
int dfsTrave () {
int block=;
for (int i=;i<=N;i++)
if (!visit[i]) {
flag=;
dfs (i);
if (!flag) block++;
}
return block;
}
int main () {
while (~scanf ("%d %d",&N,&M)) {
init ();
for (int i=;i<M;i++) {
scanf ("%d %d",&x,&y);
g[x].push_back(y);
g[y].push_back(x);
}
printf ("%d\n",dfsTrave());
}
return ;
}

Codeforce 977E Cyclic Components的更多相关文章

  1. CF 977E Cyclic Components

    E. Cyclic Components time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. Cyclic Components CodeForces - 977E(DFS)

    Cyclic Components CodeForces - 977E You are given an undirected graph consisting of nn vertices and  ...

  3. 【codeforces div3】【E. Cyclic Components】

    E. Cyclic Components time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  4. Codeforce Div-3 E.Cyclic Components

    You are given an undirected graph consisting of nn vertices and mm edges. Your task is to find the n ...

  5. Codeforces 977E:Cyclic Components(并查集)

    题意 给出nnn个顶点和mmm条边,求这个图中环的个数 思路 利用并查集的性质,环上的顶点都在同一个集合中 在输入的时候记录下来每个顶点的度数,查找两个点相连,且度数均为222的点,如果这两个点的父节 ...

  6. Cyclic Components CodeForces - 977E(找简单环)

    题意: 就是找出所有环的个数, 但这个环中的每个点都必须只在一个环中 解析: 在找环的过程中 判断度数是否为2就行...emm... #include <bits/stdc++.h> us ...

  7. S - Cyclic Components (并查集的理解)

    Description You are given an undirected graph consisting of nn vertices and mm edges. Your task is t ...

  8. E. Cyclic Components (DFS)(Codeforces Round #479 (Div. 3))

    #include <bits/stdc++.h> using namespace std; *1e5+; vector<int>p[maxn]; vector<int&g ...

  9. Codeforces Round #479 (Div. 3) E. Cyclic Components (思维,DFS)

    题意:给你\(n\)个顶点和\(m\)条边,问它们有多少个单环(无杂环),例如图中第二个就是一个杂环. 题解:不难发现,如果某几个点能够构成单环,那么每个点一定只能连两条边.所以我们先构建邻接表,然后 ...

随机推荐

  1. 题解【UVA10054】The Necklace

    题目描述 输入格式 输出格式 题意简述 有一种由彩色珠子连接而成的项链.每个珠子的两半由不同颜色组成.如图所示,相邻两个珠子在接触的地方颜色相同.现在有一些零碎的珠子,需要确认它们是否可以复原成完整的 ...

  2. 同步异步IO,阻塞非阻塞

    同步异步 同步IO操作:导致请求进程阻塞,知道IO操作完成. 异步IO操作:不导致进程阻塞. 在处理(网络) IO 的时候,阻塞和非阻塞都是同步IO, 阻塞,就是调用我(函数),我(函数)没有接收完数 ...

  3. OSS链接出现 connection pool shutdown错误修改

    在类中创建了OSSClient对象 ,方法共用此实例对象,在前端很短的时间内连续提交,造成异常错误. 解决方法时将OSSClient对象在方法中创建

  4. python 中模块的版本号

    查看所使用的模块的版本号,以numpy为例 import numpy numpy.__version__ 查看help(numpy)时,信息太多,不想看了,如何退出,按q,即可.

  5. 【转载】C/C++编译过程分析

    转自:http://www.360doc.com/content/14/0109/16/835125_343879650.shtml C/C++编译过程 C/C++编译过程主要分为4个过程 1) 编译 ...

  6. android .9背景图作为TextView背景时文字无法居中问题

    问题产生原因: .9图黑色边框绘制伸缩区域有问题,重叠的最大区域是TextView文字所能显示的区域 如下图所示,横向和纵向最大重叠部分就是文字可显示部分,这个图作为背景后文字整体偏下,无法上下居中对 ...

  7. C++11 新特性学习

    在Linux下编译C++11 #include<typeinfo> int main() { auto a=; cout<<typeid(a).name()<<en ...

  8. pgspider http fdw http 相关的几个配置参数

    http 请求时间配置 session 级别的 set http.timeout_msec = 10000; SELECT http_set_curlopt('CURLOPT_TIMEOUT', '1 ...

  9. Educational Codeforces Round 78 (Rated for Div. 2)D(并查集+SET)

    连边的点用并查集检查是否有环,如果他们的fa是同一个点说明绕了一圈绕回去了.n个点一共能连n-1条边,如果小于n-1条边说明存在多个联通块. #define HAVE_STRUCT_TIMESPEC ...

  10. Go_bufio包

    bufio 是通过缓冲来提高效率. io操作本身的效率并不低,低的是频繁的访问本地磁盘的文件.所以bufio就提供了缓冲区(分配一块内存),读和写都先在缓冲区中,最后再读写文件,来降低访问本地磁盘的次 ...