欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路。现给定一个图,问是否存在欧拉回路?
Input
测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是节点数N ( 1 < N < 1000 )和边数M;随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个节点的编号(节点从1到N编号)。当N为0时输入结
束。
Output
每个测试用例的输出占一行,若欧拉回路存在则输出1,否则输出0。
Sample Input
3 3
1 2
1 3
2 3
3 2
1 2
2 3
0
Sample Output
1
0
无向图的欧拉回路:
判断:
1.每个点的度都为偶数
2.图联通(并查集)
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define inf 0x3f3f3f3f
#define Pi 4.0*atan(1.0)
#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max(a,b) a>b?a:b
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const int maxn = 1000+5;
const int maxm = 10000005;
using namespace std;
inline int read(){
int x(0),f(1);
char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}int deg[maxn];
int p[maxn];
void init(int n){
fill(deg,deg+n+1,0);
for(int i=1;i<=n;++i){
p[i]=i;
}
}
bool check(int n){
for(int i=1;i<=n;++i){
if(deg[i]&1){
return false;
}
}
return true;
}
inline int find(int x){
return x==p[x]?x:p[x]=find(p[x]);
}
inline void merge(int x,int y){
x=find(x);
y=find(y);
if(x!=y){
p[y]=x;
}
}
int main(){
int n,m;
int u,v;
while(~scanf("%d%d",&n,&m),n){
init(n);
while(m--){
cin>>u>>v;
merge(u,v);
deg[u]++;
deg[v]++;
}
if(!check(n)){
puts("0");
}else{
int cnt=0;
for(int i=1;i<=n;++i){
if(p[i]==i){
++cnt;
}
}
if(cnt==1){
puts("1");
}else{
puts("0");
}
}
}
return 0;
}