解题思路:给定n个点,m条边,判断是否构成一个环

注意到构成一个环,所有点的度数为2,即一个点只有两条边与之相连,再有就是判断合并之后这n个点是否在同一个连通块

Circle


Time Limit: 1 Second      Memory Limit: 32768 KB

Your task is so easy. I will give you an undirected graph, and you just need to tell me whether the graph is just a circle. A cycle is three or more nodes V1, V2, V3, ... Vk, such that there are edges between V1 and V2, V2 and V3, ... Vk and V1, with no other extra edges. The graph will not contain self-loop. Furthermore, there is at most one edge between two nodes.

Input

There are multiple cases (no more than 10).

The first line contains two integers n and m, which indicate the number of nodes and the number of edges (1 < n < 10, 1 <= m < 20).

Following are m lines, each contains two integers x and y (1 <= x, y <= n, x != y), which means there is an edge between node x and node y.

There is a blank line between cases.

Output

If the graph is just a circle, output "YES", otherwise output "NO".

Sample Input

3 3
1 2
2 3
1 3 4 4
1 2
2 3
3 1
1 4

Sample Output

YES
NO
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int degree[10010],pre[10010]; int find(int root){ return root == pre[root] ? root : pre[root] = find(pre[root]); }
void unionroot(int x,int y)
{
int root1=find(x);
int root2=find(y);
if(root1!=root2)
pre[root1]=root2;
} int main()
{
int m,n,u,v,i,j;
while(scanf("%d %d",&n,&m)!=EOF)
{
int flag=1;
memset(degree,0,sizeof(degree));
for(i=1;i<=10010;i++)
pre[i]=i;
for(i=1;i<=m;i++)
{
scanf("%d %d",&u,&v);
degree[u]++;
degree[v]++;
unionroot(u,v);
} for(i=1;i<=n;i++)
{
if(degree[i]!=2)
{
flag=0;
break;
}
if(find(i)!=find(n))
{
flag=0;
break;
}
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}
}

  

ZOJ 3321 Circle【并查集】的更多相关文章

  1. ZOJ - 3261 逆向并查集

    思路:很巧妙的解法.如果按照常规一边读入,一边合并并查集,删边实在没办法做. 首先读入所有的操作,把所有不会被删除的边加入并查集,然后从最后一个操作开始逆向操作,当遇到删边操作,就直接把这条边加入并查 ...

  2. zoj 3761(并查集+搜索)

    题意:在一个平面上,有若干个球,给出球的坐标,每次可以将一个球朝另一个球打过去(只有上下左右),碰到下一个球之后原先的球停下来,然后被撞的球朝这个方向移动,直到有一个球再也撞不到下一个球后,这个球飞出 ...

  3. zoj 3261 逆向并查集+离线处理

    题意:给出一些点,每个点有权值,然后有一些边,相连.无向的.然后有一些操作 链接:点我 query a.表示从a出发的能到达的所有点权值最大的点的编号(相同取编号最小,而且权值要比自己大) desto ...

  4. hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)

    Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...

  5. zoj 2334 Monkey King/左偏树+并查集

    原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1389 大致题意:N只相互不认识的猴子(每只猴子有一个战斗力值) 两只 ...

  6. HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)

    题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有 ...

  7. ZOJ 3261 Connections in Galaxy War(逆向并查集)

    参考链接: http://www.cppblog.com/yuan1028/archive/2011/02/13/139990.html http://blog.csdn.net/roney_win/ ...

  8. ZOJ 3261 - Connections in Galaxy War ,并查集删边

    In order to strengthen the defense ability, many stars in galaxy allied together and built many bidi ...

  9. zoj 3659 第37届ACM/ICPC 长春赛区现场赛E题 (并查集)

    题意:给出一棵树,找出一个点,求出所有点到这个点的权值和最大,权值为路径上所有边权的最小值. 用神奇的并查集,把路按照权值从大到小排序,然后用类似Kruskal的方法不断的加入边. 对于要加入的一条路 ...

随机推荐

  1. 笔记--js实现异步

    <script type="text/javascript"> var xhr=false; function createXhr() { var xhobj = fa ...

  2. Activity全屏沉浸状态

    public class MainActivity extends AppCompatActivity { private static final String TAG = "MainAc ...

  3. Android 的永久登陆 与注销登陆

    一.永久登陆 sharedprefrence 存储 userID  以及 password private String FILE = "saveUserNamePwd";//用于 ...

  4. 史上最低,低到尘埃,CDR邀你一起嗨购618

    盼呀盼,望穿秋水~盼呀盼,何时降价~ 6.4开始,CDR X6全民狂欢618放价活动全面开启 力度之大,范围之广,时间之久,价格之低,都是前所未有的 不负众望,这个618,CDR真的做到一降到底,没有 ...

  5. luoguP5055 【模板】可持久化文艺平衡树 可持久化非旋转treap

    好题. Code: #include<bits/stdc++.h> using namespace std; #define setIO(s) freopen(s".in&quo ...

  6. 算法23-------岛屿的最大面积 LeetCode 695

    一.题目: 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被水包围着. 找到给定 ...

  7. HDU 1028 Ignatius and the Princess III(母函数整数拆分)

    链接:传送门 题意:一个数n有多少种拆分方法 思路:典型母函数在整数拆分上的应用 /********************************************************** ...

  8. js中的变量提升和函数提升

    从上周开始,我所在的学习小组正式开始了angular的学习,angular是全面支持es6的,所以语法上和以前的angular有了很大的不同,比如变量声明时就抛弃了var,而选择了let和const: ...

  9. 2015 Multi-University Training Contest 3 hdu 5324 Boring Class

    Boring Class Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  10. CSS学习(五)

    导航栏 熟练使用导航栏,对于任何网站都非常重要. 使用CSS你可以转换成好看的导航栏而不是枯燥的HTML菜单. 导航栏=链接列表 作为标准的HTML基础一个导航栏是必须的.在我们的例子中我们将建立一个 ...