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");
	}
}

  

时间: 2024-10-13 01:06:45

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

ZOJ 3321 Circle(并查集啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3731 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 th

ZOJ 2833-Friendship(并查集+优化)

Friendship Time Limit: 3 Seconds      Memory Limit: 32768 KB A friend is like a flower, a rose to be exact, Or maybe like a brand new gate that never comes unlatched. A friend is like an owl, both beautiful and wise. Or perhaps a friend is like a gho

ZOJ 3321 Circle

 I - Circle Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3321 Description 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 c

ZOJ Problem Set - 3321 并查集

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3321 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.

zoj 3659 Conquer a New Region 并查集+贪心

点击打开链接题目链接 Conquer a New Region Time Limit: 5 Seconds      Memory Limit: 32768 KB The wheel of the history rolling forward, our king conquered a new region in a distant continent. There are N towns (numbered from 1 to N) in this region connected by s

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

题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有首字母指向尾字母的有向边,每个字母看成一个点,题中要求等效于判断图中是否存在一条路径经过每一条一次且仅一次,就是有向欧拉通路.统计个顶点的出入度,如果每个点的出入度都相同,那就是欧拉回路,如果有两个奇数度,那就是欧拉通路,除此之外,都不能满足要求.还有别忘了判断是否连通,此时用到并查集,图中所有的边

Welcome Party ZOJ - 4109 (思维+并查集)

题目链接: Welcome Party  ZOJ - 4109 题目大意:给你T组测试样例,然后n个人,m个关系,每一个关系包括两个人,这两个人为好朋友,然后问你怎么安排顺序,使得整个队伍的友情损失度最小(当一个人放置时,如果他的前面中没有他的朋友,那么整个队伍的朋友损失度就会加1) 具体思路:首先用并查集求出每一个联通块,然后用一个超级汇点连向这些连通块的根,然后优先队列+bfs求出字典序最小的正解就可以了. AC代码: 1 #include<bits/stdc++.h> 2 using n

zoj 3659 并查集

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4882 现在在牡丹江,明天regional现场赛,不出一个月就要退役了,求保佑 今天热身赛做题很紧张,老是打错字,所以晚上写写代码练练手 脑子还是不好使,没想到可以并查集 思路:题目中的操作导致的一个结果是,一条边会成为一个集合的w,----  如果能想到这里可能就能想到并查集吧 WA了因为如果father[x]==x并不能表示x中只有一个数啊... #include <cst

ZOJ 3811 Untrusted Patrol【并查集】

题目大意:给一个无向图,有些点有装监视器记录第一次到达该点的位置,问是否存在一条路径使得监视器以给定的顺序响起,并且经过所有点 思路:牡丹江网络赛的题,当时想了种并查集的做法,通神写完程序WA了几发,此时JYB用BFS秒了,索性最后还是调出来了,今天自己写了下,感觉唯一的坑点就是需要遍历完所有的点 //zoj3811 #include <stdio.h> #include <string.h> #include <algorithm> #include <queu