zoj Circle 并查集+出入度检测
来源:互联网 发布:中国教育干部网络app 编辑:程序博客网 时间:2024/05/29 02:53
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 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"
我们可以知道,若n个点构成一个圆,只有一条回路,自己与自己不能构成回路,所以如果能够成一个圆,那每个节点的出入度都是二并且,查找每个点,其f[x]都是n。。。
附上代码 备忘
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int f[20];
int cnt[20],cir[20];
int n,m,a,b;
int find(int x)
{
if(f[x]!=x)
f[x]=find(f[x]);
return f[x];
}
void make(int a,int b)
{
int f1=find(a);
int f2=find(b);
if(f2!=f1)
{
cnt[f1]+=cnt[f2];
f[f2]=f1;
}
}
int main()
{
while(cin>>n>>m)
{
int flag=0;
memset(cir,0,sizeof(cir));
for(int i=1;i<=n;i++)
{
f[i]=i;
cnt[i]=1;
}
for(int i=0;i<m;i++)
{
cin>>a>>b;
if(a==b)
{
flag=1;
break;
}
cir[a]++;
cir[b]++;
make(a,b);
}
if(flag)
{
printf("NO\n");
continue;
}
for(int i=1;i<=n;i++)
{
int t=find(i);
if(cnt[t]!=n||cir[i]!=2)
{
flag=1;
break;
}
}
if(flag)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}
- zoj Circle 并查集+出入度检测
- ZOJ 3321 Circle(并查集)
- zoj 3321 Circle【并查集】
- ZOJ 3321 Circle(并查集啊)
- zoj 3659 并查集
- zoj 3641 并查集
- zoj 3789 并查集
- zoj Friendship 并查集
- zoj 3659 并查集
- zoj 3641 并查集
- Zoj 3811并查集
- ZOJ 3789 并查集
- zoj 2833 Friendship(并查集)
- zoj 1789The Suspects 并查集
- zoj Information Sharing(并查集)
- ACM zoj 1789(并查集实现)
- zoj 1203 kruskal+并查集
- 逆向并查集(ZOJ 3261)
- 一些笔记
- jdbc
- 深度探索linux中对内核头文件合法化检查的问题
- Android CI 资料整理
- 手机管理应用研究
- zoj Circle 并查集+出入度检测
- 根据生日判断星座
- git 使用方法
- 原码 反码 补码详解
- Oracle常用命令
- matlab 蒙特卡洛
- 平衡二叉树(AVL树)
- How to Set Up Chinese Characters on Windows 7
- Android SDK上手指南:用户界面设计