标签:
2 3 3 1 2 2 3 3 1 4 4 1 2 2 3 3 4 4 1
0 2
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> using namespace std; const int MAXN = 30; int x[MAXN],y[MAXN], s[MAXN], s1[MAXN], s2[MAXN], g[MAXN][MAXN]; int n, m; int ans; void dfs(int dep) { if(dep > m) { for(int i=1;i<=n;i++) { if(s1[i] != s2[i]) return ; } ans++; return ; } int u = x[dep], v = y[dep]; if(s1[u] < s[u] / 2 && s1[v] < s[v] / 2) { s1[u]++; s1[v]++; dfs(dep + 1); s1[u]--; s1[v]--; } if(s2[u] < s[u] / 2 && s2[v] < s[v] / 2) { s2[u]++; s2[v]++; dfs(dep + 1); s2[u]--; s2[v]--; } } int main() { int T; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); memset(g, 0, sizeof(g)); for(int i=1; i<=m; i++) { scanf("%d%d", &x[i], &y[i]); g[x[i]][y[i]] = g[y[i]][x[i]] = 1; } int flag = 1; for(int i=1; i<=n; i++) { s[i] = s1[i] = s2[i] = 0; for(int j=1; j<=n; j++) { if(i != j && g[i][j] == 1) s[i]++; } if(s[i] & 1) flag = 0; } if(!flag) { printf("0\n"); continue; } ans = 0; dfs(1); printf("%d\n", ans); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 5305 Friends(2015多校第二场 dfs + 剪枝)
标签:
原文地址:http://blog.csdn.net/moguxiaozhe/article/details/47035951