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. UOJ 34: 多项式乘法(FFT模板题)

    关于FFT 这个博客的讲解超级棒 http://blog.miskcoo.com/2015/04/polynomial-multiplication-and-fast-fourier-transfor ...

  2. 关于BaiduPSC-Go的一些bug的更正

    首先说下操作步骤 下载是在GutHub,这个不赘述,网上很多资料 下载之后配置环境变量,在path的后面加上一个分号,然后加上你下载的目录,目录名最好为英文 然后通过命令行CMD工具,输入BaiduP ...

  3. android WebView缩放时卡顿问题

    问题描述: WebView 在双指缩放页面时会卡顿 解决过程:有试过打开硬件加速android:hardwareAccelerated="true" 还是没用,纠结了一天,最后屏蔽 ...

  4. (matlab)自定义图像(matlab)

    clc;clear all;A=[0 230 255 60 30 100];A=uint8(A);imshow(A,'InitialMagnification','fit') 如图: clc;clea ...

  5. 多对多三种创建方式、forms组件、cookies与session

    多对多三种创建方式.forms组件.cookies与session 一.多对多三种创建方式 1.全自动 # 优势:不需要你手动创建第三张表 # 不足:由于第三张表不是你手动创建的,也就意味着第三张表字 ...

  6. 左偏树(p4431)

    难得不是左偏树,而是思维: 这道题在做得时候,有两个性质 1.如果a是一个不下降序列,那么b[i]==a[i]时取得最优解. 2.如果a是一个严格递减序列,则取a序列的中位数x,令b[1]=b[2]= ...

  7. Codeforces Round #624 (Div. 3) A. Add Odd or Subtract Even(水题)

    You are given two positive integers aa and bb . In one move, you can change aa in the following way: ...

  8. OO完结篇-第四单元小结

    OO第四单元小结 一.作业架构分析. 1.第一次作业 本次作业需要完成UML类图查询. 难点在于初次接触UML,需要对UML进行一定程度的学习和理解. 思路主要是根据每个传进来的element获取其t ...

  9. Lining Up

    AtCoder - 2271-Lining Up                                                                             ...

  10. rabbitmq - 简单认识

    1. 概述 与 rabbitmq 做交互 amqp 最著名的实现 与 jms 最明显的区别 消息 不是去找 queue 而是去找 exchange 2. rabbitmq 基本组件 sender 发送 ...