判断是不是一个欧拉图
1.是一个连通图
2.只有2个或者没有奇数结点
#include <iostream> #include <cstring> using namespace std; const int Max = 1001; int num[Max]; int f[Max]; int getf(int v) { if(f[v]==v) return v; else {f[v]= getf(f[v]); return f[v];} } int merg(int a, int b) { int t1 = getf(a); int t2 = getf(b); if(t1 != t2) { f[t2] = t1; return 1; } return 0; } int main() { int n; int vn,en; cin >> n; while(n--) { cin >> vn >> en; for(int i = 1; i <= vn;i++) f[i] = i; memset(num,0,sizeof(num)); int count = 0; int a,b; int sign = 0; for(int i = 0; i < en; i++) { cin >> a >> b; merg(a,b); //合并a,b结点 num[a]++; num[b]++; } for(int i =1; i < vn; i++) if(merg(i,i+1)) //不连通,退出循环 { sign = 1; cout << "No" << endl; break; } if(sign) continue; for(int i = 1; i <= vn; i++) if(num[i] % 2 == 1) count ++; if(count == 0 || count == 2) cout << "Yes" << endl; else cout << "No" << endl; } system("pause"); return 0; }
原文:http://www.cnblogs.com/ekinzhang/p/4396529.html