HDU 5305 Friends(dfs)
来源:互联网 发布:ubuntu mentohust下载 编辑:程序博客网 时间:2024/06/05 04:07
Friends
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 297 Accepted Submission(s): 127
Problem Description
There are n people and m pairs of friends. For every pair of friends, they can choose to become online friends (communicating using online applications) or offline friends (mostly using face-to-face communication). However, everyone in these n people wants to have the same number of online and offline friends (i.e. If one person has x onine friends, he or she must have x offline friends too, but different people can have different number of online or offline friends). Please determine how many ways there are to satisfy their requirements.
Input
The first line of the input is a single integer T (T=100) , indicating the number of testcases.
For each testcase, the first line contains two integersn (1≤n≤8) and m (0≤m≤n(n−1)2) , indicating the number of people and the number of pairs of friends, respectively. Each of the next m lines contains two numbers x and y , which mean x and y are friends. It is guaranteed that x≠y and every friend relationship will appear at most once.
For each testcase, the first line contains two integers
Output
For each testcase, print one number indicating the answer.
Sample Input
23 31 22 33 14 41 22 33 44 1
Sample Output
02
Source
/*题意:n个人,m个关系,每个人和别人的关喜有线上和线下,求每个人线上和线下的关系一样多的方案数思路: dfs先贴别人代码,跑的快,我的跑的慢*/#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<stack>#include<vector>#include<set>#include<map>#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define eps 1e-8using namespace std;typedef __int64 LL;#define N 100int x[N],y[N],in[N],on[N],off[N];int n,m;int ans;bool judge(){ if(m&1) return false; for(int i=1;i<=n;i++) if(in[i]&1) return false; return true;}void dfs(int pos){ if(pos==m) { ans++; return ; } int u=x[pos],v=y[pos]; if(on[u]<in[u]/2&&on[v]<in[v]/2) //这个边为online 边 { on[u]++; on[v]++; dfs(pos+1); on[u]--; on[v]--; } if(off[u]<in[u]/2&&off[v]<in[v]/2) //这个边为off边 { off[u]++; off[v]++; dfs(pos+1); off[u]--; off[v]--; }}int main(){ int i,j,t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(in,0,sizeof(in)); memset(on,0,sizeof(on)); memset(off,0,sizeof(off)); for(i=0;i<m;i++) { scanf("%d%d",&x[i],&y[i]); in[x[i]]++; in[y[i]]++; } ans=0; if(!judge()) { printf("0\n"); continue; } dfs(0); printf("%d\n",ans); } return 0;}
/*我的代码*/#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<stack>#include<vector>#include<set>#include<map>#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define bug printf("hihi\n")#define eps 1e-8typedef __int64 ll;using namespace std;#define INF 0x3f3f3f3f#define N 1<<8__int64 ans;int on[N],down[N];int n,m,f[N],in[N];inline int get(int x){ int s=0; while(x) { s++; x&=(x-1); } return s;}inline bool judge(int pos,int cur){ int i; for(i=0;i<n;i++) if(on[i]&(1<<pos)&&!(cur&(1<<i))) return false; for(i=0;i<n;i++) if(down[i]&(1<<pos)&&(cur&(1<<i))) return false; return true;}void dfs(int pos){ if(pos==n) { ans++; return ; } int i,len=1<<n; for(i=0;i<len;i++) { if((f[pos]&i)!=i) continue; int tt=get(i); if(tt!=in[pos]/2) continue; if(!judge(pos,i)) continue; on[pos]=i; down[pos]=f[pos]^i; dfs(pos+1); on[pos]=0; down[pos]=0; }}int main(){ int i,j,t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); int u,v; memset(f,0,sizeof(f)); memset(on,0,sizeof(on)); memset(down,0,sizeof(down)); memset(in,0,sizeof(in)); bool flag=false; if(m&1) flag=true; while(m--) { scanf("%d%d",&u,&v); u--;v--; f[u]|=1<<v; f[v]|=1<<u; in[u]++; in[v]++; } for(i=0;i<n;i++) { if(in[i]&1) flag=true; } if(flag) { printf("0\n"); continue; } ans=0; int len=1<<n; for(i=0;i<len;i++) { if((f[0]&i)!=i) continue; int tt=get(i); if(tt!=in[0]/2) continue; on[0]=i; down[0]=f[0]^i; dfs(1); on[0]=0; down[0]=0; } printf("%I64d\n",ans); } return 0;}
1 0
- HDU 5305 Friends(dfs)
- HDU 5305 Friends(dfs)
- hdu 5305 Friends(dfs)
- HDU 5305 Friends (DFS)
- HDU 5305 Friends (DFS)
- hdu 5305 Friends (dfs)
- HDU 5305 Friends DFS
- hdu 5305 Friends dfs
- HDU 5305 Friends(DFS)
- HDU 5305 Friends(dfs)
- HDU 5305 Friends(简单DFS)
- hdu(5305)——Friends(dfs)
- *HDU 5305 - Friends (DFS + 剪枝)
- HDU 5305 Friends DFS+剪枝
- HDU 5305 Friends(DFS + 剪枝)
- 【HDU 5305】Friends 多校第二场(双向DFS)
- HDU 5301 Friends(DFS + 枚举)
- HDU 5305 Friends(2015多校第二场 dfs + 剪枝)
- 微信公众平台开发时需要判断微信浏览器访问
- 2015.7.23 简陋的学生通讯录管理系统
- [基本实验] 利用Burp和FireBug绕过上传漏洞的前端防护
- 大话西游
- 融云即时聊天,IM
- HDU 5305 Friends(dfs)
- 2015多校第二场 1004( Delicious Apples )
- 每天一个linux命令(9):touch 命令
- Android 五大存储方式详解
- Java的运行原理
- animateWithDuration动画效果
- Frequently used algorithms in Leetcode
- 犀牛——第6章对象6.2属性的查询和设置
- 每天一个小知识点11(jQuerMobile总结二)