标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1192 Accepted Submission(s): 595
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> using namespace std; #define maxn 30 int n,m; int ans; int c1[maxn],c2[maxn]; int deg[maxn]; struct Edge { int from,to; }edges[101000]; void init() { ans=0; memset(c1,0,sizeof(c1)); memset(c2,0,sizeof(c2)); memset(deg,0,sizeof(deg)); } void dfs(int e) { if(e==m+1) //能够搜到第m+1次,说明每条边都分配了0或者1,减了2*m次,c1和c2数组都为0 { ans++; return ; } int from=edges[e].from,to=edges[e].to; if(c1[from] && c1[to]) //这条边分配0 { c1[from]--; c1[to]--; dfs(e+1); c1[from]++; c1[to]++; } if(c2[from] && c2[to]) //这条边分配1 { c2[from]--; c2[to]--; dfs(e+1); c2[from]++; c2[to]++; } return ; //如果这条边既不能分配0,也不能分配1,只能回溯 } int main() { int t; scanf("%d",&t); while(t--) { init(); scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { scanf("%d%d",&edges[i].from,&edges[i].to); deg[edges[i].from]++; deg[edges[i].to]++; } // for(int i=1;i<=n;i++) // printf("%d ",deg[i]); // cout<<endl; int flag=0; for(int i=1;i<=m;i++) { int from=edges[i].from,to=edges[i].to; if( (deg[from] & 1)) { flag=1; break; } if( (deg[to] & 1)) { flag=1; break; } c1[from]= (deg[from] >> 1); //from的在线朋友数量 c2[from]= (deg[from] >> 1); //from的在线朋友数量 //from这个点的在线朋友和离线朋友都为它度数的一半 c1[to] = (deg[to] >> 1); //to的离线朋友数量 c2[to]= (deg[to] >> 1); //to的离线朋友数量 } if(flag==1) { printf("0\n"); continue; } dfs(1); printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/xianbin7/p/4676004.html