3 3 1 2 1 3 2 3 3 2 1 2 2 3 0
1 0
并查集:对于每次输入的2个点合并,最后若所有的点均在一个集合中则可构成欧拉回路。
实现代码:
//************************************************************************// //*Author : Handsome How *// //************************************************************************// //#pragma comment(linker, "/STA CK:1024000000,1024000000") #pragma warning(disable:4996) #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <complex> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cstring> #include <ctime> #include <cassert> #define fur(i,a,b) for(int i=(a);i<=(b);i++) #define furr(i,a,b) for(int i=(a);i>=(b);i--) using namespace std; typedef long long LL; const int maxn = 1005; int deep[maxn]; int fat[maxn]; int findf(int x) { int t = x; while (t!=fat[t]) t = fat[t]; int c = x,v; while (c!=t) { v = fat[c]; fat[c] = t; c = v; } return t; } void add(int a, int b) { a = findf(a); b = findf(b); if (a == b)return; fat[a] = b; } int main() { //freopen("E:\\data.in", "r", stdin); int n, m; while (scanf("%d", &n) != EOF) { if (n == 0)break; bool ok = true; int node = 0; memset(deep, 0, sizeof(deep)); fur(i, 1, n)fat[i] = i; scanf("%d", &m); int a, b; while (m--) { scanf("%d%d", &a,&b); deep[a]++; deep[b]++; add(a, b); } fur(i, 1, n) { if (deep[i] % 2 != 0) { ok = false; break; } if (fat[i] == i)node++; } if (node != 1)ok = false; if (ok)printf("1\n"); else printf("0\n"); } return 0; }
DFS:首先判断深度,若满足条件则从1开始DFS,然后用一个二维数组记录路径(因为是无向图,所以对线段a-b需要同时记录b-a),接着直接DFS即可。
实现代码:
//************************************************************************// //*Author : Handsome How *// //************************************************************************// //#pragma comment(linker, "/STA CK:1024000000,1024000000") #pragma warning(disable:4996) #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <complex> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cstring> #include <ctime> #include <cassert> #define fur(i,a,b) for(int i=(a);i<=(b);i++) #define furr(i,a,b) for(int i=(a);i>=(b);i--) using namespace std; typedef long long LL; const int maxn = 1111; int dep[maxn]; int has[maxn][maxn]; int n, m; bool dfs(int deep,int head) { if (deep == m + 1)return true; for (int i = 1; i <=n; i++) if (has[head][i]) { has[head][i]--; if (dfs(deep + 1, i))return true; has[head][i]++; } return false; } int main() { //freopen("E:\\data.in", "r", stdin); while (scanf("%d", &n) != EOF) { if (n == 0)break; bool ok = true; memset(dep, 0, sizeof(dep)); memset(has, 0, sizeof(has)); scanf("%d", &m); int a, b; int c = m; while (c--) { scanf("%d%d", &a,&b); dep[a]++; dep[b]++; has[a][b]++; has[b][a]++; } fur(i, 1, n)if (dep[i] % 2 != 0) { ok = false; break; } if (!ok) { printf("0\n"); continue; } if (dfs(1,1))printf("1\n"); else printf("0\n"); } return 0; }