解题思路:给定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. sql导出到excel

    1.先查询数据ID,别名  导出到excel 2.增加标准名称,标识错误数据,导入sqlServer select distinct [StandardName] from [GMSOA].[dbo] ...

  2. 几个月来使用mobx代替redux的一些总结

    遇到的一些小坑 React组件内部想要调用store里的action方法,得如下图,否则不会调用(这个现在看来好像不对,待重新检验) 而不能如下图 组件中调用改变store的action后,状态变化并 ...

  3. MyBatis数据持久化(十)与Spring4整合

    前面几节介绍了mybatis的基本使用方法,本节主要介绍如何使用mybatis与主流的IoC容器Spring进行整合. 我们首先需要获取Spring框架的jar文件,在写本文时spring的最新Rel ...

  4. vuex存储和本地存储的区别

    1.实质的区别 vuex存的是状态,存储在内存,localstorage是浏览器提供的接口,让你存的是文件,以文件的形式存储在本地 2.应用场景 vuex用于组件之间的传值,localstorage则 ...

  5. node——将数据写入data.json防覆盖

    前续 1.读取data.json文件内容 2.判断是否是第一次读取data.json(第一次读取则data.json是没有的) 3.将data.json数据转为数组list 4.将写入数据加入数组li ...

  6. SVN提交代码时报405 Method Not Allowed

    原因: 1.删除了某个文件夹,然后又创建了一个同名文件夹 2.之前执行过Add操作,但没上传代码,在电脑上提交了同路径的代码,再次上传时会报错 解决方法: 1. 删除出现错误的文件夹 2. SVN U ...

  7. jsonp实现原理

    jquery 封装在 ajax方法 里面的jsonp jsonp跨域的原理       1:使用script 标签发送请求,这个标签支持跨域访问       2:在script 标签里面给服务器端传递 ...

  8. thinkphp 同一字段不同查询条件实现

    搞定了 另外区间查询还可以支持更多的条件,只要是针对一个字段的条件都可以写到一起,例如:$map['name'] = array(array('like','%a%'), array('like',' ...

  9. 【codeforces 734F】Anton and School

    [题目链接]:http://codeforces.com/problemset/problem/734/F [题意] 给你两个数组b和c; 然后让你找出一个非负数组a满足题中所给关系; [题解] 有个 ...

  10. Xshell 安装 Xftp

    一.下载 Xftp 链接:https://pan.baidu.com/s/1dGeL2gD 密码:as9x 二.安装 Xftp 无脑下一步 三.点击 Xshell 上的新建文件传输 四.弹出 xftp ...