ZOJ 3321 Circle【并查集】
解题思路:给定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【并查集】的更多相关文章
- ZOJ - 3261 逆向并查集
思路:很巧妙的解法.如果按照常规一边读入,一边合并并查集,删边实在没办法做. 首先读入所有的操作,把所有不会被删除的边加入并查集,然后从最后一个操作开始逆向操作,当遇到删边操作,就直接把这条边加入并查 ...
- zoj 3761(并查集+搜索)
题意:在一个平面上,有若干个球,给出球的坐标,每次可以将一个球朝另一个球打过去(只有上下左右),碰到下一个球之后原先的球停下来,然后被撞的球朝这个方向移动,直到有一个球再也撞不到下一个球后,这个球飞出 ...
- zoj 3261 逆向并查集+离线处理
题意:给出一些点,每个点有权值,然后有一些边,相连.无向的.然后有一些操作 链接:点我 query a.表示从a出发的能到达的所有点权值最大的点的编号(相同取编号最小,而且权值要比自己大) desto ...
- 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 ...
- zoj 2334 Monkey King/左偏树+并查集
原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1389 大致题意:N只相互不认识的猴子(每只猴子有一个战斗力值) 两只 ...
- HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)
题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有 ...
- ZOJ 3261 Connections in Galaxy War(逆向并查集)
参考链接: http://www.cppblog.com/yuan1028/archive/2011/02/13/139990.html http://blog.csdn.net/roney_win/ ...
- ZOJ 3261 - Connections in Galaxy War ,并查集删边
In order to strengthen the defense ability, many stars in galaxy allied together and built many bidi ...
- zoj 3659 第37届ACM/ICPC 长春赛区现场赛E题 (并查集)
题意:给出一棵树,找出一个点,求出所有点到这个点的权值和最大,权值为路径上所有边权的最小值. 用神奇的并查集,把路按照权值从大到小排序,然后用类似Kruskal的方法不断的加入边. 对于要加入的一条路 ...
随机推荐
- LIst和map的遍历
1. public static void main(String[] args) { // ArrayList类实现一个可增长的动态数组 List<String> list = new ...
- Codeforces Round #283 (Div. 2) A
解题思路:给出一个递增数列,a1,a2,a3,-----,an.问任意去掉a2到a3之间任意一个数之后, 因为注意到该数列是单调递增的,所以可以先求出原数列相邻两项的差值的最大值max, 得到新的一个 ...
- iOS 处理图片的一些小 Tip
UIImage 缓存是怎么回事? 通过 imageNamed 创建 UIImage 时,系统实际上只是在 Bundle 内查找到文件名,然后把这个文件名放到 UIImage 里返回,并没有进行实际的文 ...
- iOS性能优化专题
http://wereadteam.github.io/2016/05/03/WeRead-Performance/ https://www.cnblogs.com/oc-bowen/p/599999 ...
- ZBrush中Local模式的旋转
刚接触ZBrush®的小伙伴可能对Local(局部)有了简单的了解,但是大多数人对它的认识还是比较模糊的,那么在本文中小编将对local命令做详细说明.此工具可以控制视图的旋转轴心点的位置,默认情况下 ...
- java容器基础
总结一下学过的java容器知识. 一.java容器框架 由于之前学习的java容器类比较混乱,先简单的整理一下java集合框架. 首先,像这种图,网上到处都是,因为这个也算比较准确吧,我也懒得自己画了 ...
- day05-3 初步了解数据类型
目录 数据类型介绍 数字类型 整形(int) 浮点型(float) 字符串 列表 字典 布尔值 数据类型介绍 不同的数据会有不同的数据类型 为了定义不同的数据,我们Python中提供了下述几个数据类型 ...
- Node_进阶_1
第一天 1.1简介 Node.js简介 V8引擎本身就是用于Chrome浏览器的JS解释部分,Ryan Dahl把这个V8搬到了服务器上,用于做服务器的软件. Node.js是一个让Javascrip ...
- HDU 5776 sum( 鸽巢定理简单题 )
链接:传送门 题意:给一个长为 n 的串,问是否有子串的和是 m 的倍数. 思路:典型鸽巢定理的应用,但是这里 n,m 的大小关系是不确定的,如果 n >= m 根据定理可以很简单的判定是一定有 ...
- Consider defining a bean of type 'XX.XX.XX.XX.mapper.XXMapper' in your configuration.
今天构建一个springboot 项目,采用mybatis+mysql 然后就出现了这种错误....浪费我半天时间 Description: Field loginLogMapper in com.g ...