欧拉回路
Problem Description
欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路。现给定一个图,问是否存在欧拉回路?
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
解题思路:
无向图存在欧拉回路的充要条件:
- 该图所有顶点度数都为偶数。
- 且该图是连通图。
有向图存在欧拉回路的充要条件:
- 所有顶点的入度等于出度。
- 且该图是连通图。
然后就dfs就行了。
如果dfs一趟,能不重复的遍历所有边,且上面两个条件成立。就存在欧拉回路。
AC代码:
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) print f("%d\n", n)
#define pc(n) print f("%c", n)
#define pdd(n,m) print f("%d %d", n, m)
#define pld(n) print f("%int d\n", n)
#define pldd(n,m) print f("%int d %int d\n", n, m)
#define sld(n) scanf("%int d",&n)
#define sldd(n,m) scanf("%int d%int d",&n,&m)
#define slddd(n,m,k) scanf("%int d%int d%int d",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sc(n) scanf("%c",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define aint (x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
#define pii map<int ,int >
#define mk make_pair
#define rtl rt<<1
#define rtr rt<<1|1
#define Max(x,y) (x)>(y)?(x):(y)
//#define int long long
typedef pair<int ,int > PII;
typedef long long ll;
typedef unsigned long long uint ;
typedef long double ld;
const int MOD = 1e9 + 7;
const int mod = (1ll<<63);
const double eps = 1e-9;
const int INF = 0x3f3f3f3f3f3f3f3f;
//const int inf = 0x3f3f3f3f;
inline int read(){int ret = 0, sgn = 1;char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-')sgn = -1;ch = getchar();}
while (ch >= '0' && ch <= '9'){ret = ret*10 + ch - '0';ch = getchar();}
return ret*sgn;}
inline void Out(int a){if(a>9) Out(a/10);putchar(a%10+'0');}
int qmul(int a,int b,int mod){int res=0;while(b){if(b&1)res=(res+a)%mod;a=(a+a)%mod;b>>=1;}return res;}
int qpow(int m,int k,int mod){int res=1%mod,t=m%mod;while(k){if(k&1)res=qmul(res,t,mod);t=qmul(t,t,mod);k>>=1;}return res;}
int gcd(int a,int b){if(b > a) swap(a,b); return b==0?a : gcd(b,a%b);}
int lcm(int a,int b){return a/gcd(a,b)*b;}
int inv(int x,int mod){return qpow(x,mod-2,mod)%mod;}
//const int N = 3e3+15;
int t = 1,cas = 1;
int n,m;
const int N = 1e4+7;
vector<int> G[N];
int vis[1005][1005];
int in[1005];
int now[1005];
int cnt;
int tot;
void dfs(int pos)
{
int nn = G[pos].size();
for(int i = now[pos] ; i < nn ; i ++){
now[pos] = i+1;
int to = G[pos][i];
if(!vis[pos][to]){
vis[pos][to] = 1;
vis[to][pos] = 1;
cnt ++;
dfs(to);
}
}
}
signed main()
{
while(~scanf("%d",&n) && n){scanf("%d",&m);
for(int i = 0 ; i < n ; i ++){in[i] = 0;now[i] = 0;G[i].clear();}
cnt = tot = 0;
for(int i = 0 ; i < m ; i ++){
int x,y;scanf("%d",&x);scanf("%d",&y);;x--,y--;
G[x].pb(y);G[y].pb(x);
in[x] ++;in[y]++; tot++;
vis[x][y] = vis[y][x] = 0;
}
int flag = 1;
for(int i = 0 ; i < n ; i ++){
if(in[i] < 2 || in[i]%2){
flag = 0;
break;
}
}
if(flag){
dfs(0);
//cout<<cnt<<" "<<tot<<endl;
if(cnt == tot)
cout<<1<<endl;
else
cout<<0<<endl;
}
else{
cout<<0<<endl;
}
}
}