hdu 5305 Friends (dfs)
来源:互联网 发布:淘宝商城-情侣装 编辑:程序博客网 时间:2024/05/19 09:13
题目地址
http://acm.hdu.edu.cn/showproblem.php?pid=5305
Friends
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1626 Accepted Submission(s): 817
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
一开始没有想到是用DFS,模拟了半天全WA了。。不过在n=1时我清楚为什么输出为2就A了。。
AC代码
#include<iostream>#include<algorithm>#include<stdio.h>#include<cstring>using namespace std;int sum[10],on[10],off[10],ans,m;struct edge{ int u,v;};edge p[100000];void dfs(int now){ if(now==m+1){ ans++; return; } int u=p[now].u; int v=p[now].v; if(on[u]&&on[v]) { on[u]--; on[v]--; dfs(now+1); on[u]++; on[v]++; } if(off[u]&&off[v]) { off[u]--; off[v]--; dfs(now+1); off[u]++; off[v]++; } return;}int main(){ int tot,f; int n; scanf("%d",&tot); while(tot--){ ans=0; memset(sum,0,sizeof(sum)); memset(on,0,sizeof(on)); memset(off,0,sizeof(off)); scanf("%d%d",&n,&m); for(int i=1;i<=m;i++){ scanf("%d%d",&p[i].u,&p[i].v); sum[p[i].u]++; sum[p[i].v]++; } bool f=false; for(int i=1;i<=n;i++){ on[i]=sum[i]/2; off[i]=sum[i]/2; if(sum[i]&1) { f=true; break; } } if(f){ printf("0\n"); continue; } dfs(1); printf("%d\n",ans); } return 0;}
0 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(2015多校第二场 dfs + 剪枝)
- 【HDU 5305】Friends 多校第二场(双向DFS)
- HDU 5301 Friends(DFS + 枚举)
- 内核ko中打印应用层数据
- SpringMVC表单标签(4)
- elciple中如何设置字体大小
- BZOJ1002
- 关于a标签title中有空格,空格后不显示问题
- hdu 5305 Friends (dfs)
- Javascript 私有变量
- chrome cookie管理
- hdoj 2122 Ice_cream’s world III【最小生成树】
- C# 解析简单的Xml文件
- HDU 5349 MZL's simple problem(平衡树 树堆)
- 日经社説 20150813 人民元の切り下げに映る中国経済の苦境
- 面向过程和面向对象及面向对象的三大特征
- UVA - 10004 - Bicoloring(染色问题)