2015暑假多校联合---Friends(dfs枚举)
2016-08-27 20:53
363 查看
原题链接
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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 integers n (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.
[align=left]Output[/align]
For each testcase, print one number indicating the answer.
[align=left]Sample Input[/align]
2
3 3
1 2
2 3
3 1
4 4
1 2
2 3
3 4
4 1
[align=left]Sample Output[/align]
0
2
[align=left]Author[/align]
XJZX
[align=left]Source[/align]
2015 Multi-University Training Contest 2
[align=left]Recommend[/align]
wange2014
题意:输入n,m,n表示有n个人,m表示m对朋友关系,现在要使每个人的朋友划分为在线朋友和离线朋友,且在线朋友和离线朋友数量相等(一对朋友之间只能是在线朋友或者离线朋友),求方案数;
思路:用dfs深搜枚举每一条边(即每一对朋友关系),若能深搜进行完最后一条边,即当前边cnt==m+1 则ans++;
代码如下:
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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 integers n (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.
[align=left]Output[/align]
For each testcase, print one number indicating the answer.
[align=left]Sample Input[/align]
2
3 3
1 2
2 3
3 1
4 4
1 2
2 3
3 4
4 1
[align=left]Sample Output[/align]
0
2
[align=left]Author[/align]
XJZX
[align=left]Source[/align]
2015 Multi-University Training Contest 2
[align=left]Recommend[/align]
wange2014
题意:输入n,m,n表示有n个人,m表示m对朋友关系,现在要使每个人的朋友划分为在线朋友和离线朋友,且在线朋友和离线朋友数量相等(一对朋友之间只能是在线朋友或者离线朋友),求方案数;
思路:用dfs深搜枚举每一条边(即每一对朋友关系),若能深搜进行完最后一条边,即当前边cnt==m+1 则ans++;
代码如下:
#include <iostream> #include <algorithm> #include <queue> #include <vector> #include <cstdio> #include <cstring> using namespace std; int n,m,cnt,ans; int c1[10],c2[10],d[10]; struct Node { int u,v; }node[200]; void dfs(int i) { if(i-1==m) { ans++; return ; } if(c1[node[i].u]&&c1[node[i].v]) { c1[node[i].u]--; c1[node[i].v]--; dfs(i+1); c1[node[i].u]++; c1[node[i].v]++; } if(c2[node[i].u]&&c2[node[i].v]) { c2[node[i].u]--; c2[node[i].v]--; dfs(i+1); c2[node[i].u]++; c2[node[i].v]++; } } int main() { int T; cin>>T; while(T--) { cnt=0; ans=0; scanf("%d%d",&n,&m); memset(node,0,sizeof(node)); memset(c1,0,sizeof(c1)); memset(c2,0,sizeof(c2)); memset(d,0,sizeof(d)); for(int i=1;i<=m;i++) { int u,v; scanf("%d%d",&u,&v); node[++cnt].u=u; node[cnt].v=v; d[u]++; d[v]++; } int flag=0; for(int i=1;i<=n;i++) { c1[i]=c2[i]=d[i]/2; if(d[i]&1) { flag=1; break; } } if(flag) { puts("0"); continue; } dfs(1); printf("%d\n",ans); } return 0; }
相关文章推荐
- 2015多校联合训练赛 hdu 5305 Friends 2015 Multi-University Training Contest 2 枚举+剪枝
- hdu 5358 First One 2015多校联合训练赛#6 枚举
- HDU 5371 (2015多校联合训练赛第七场1003)Hotaru's problem(manacher+二分/枚举)
- 2015暑假多校联合---CRB and His Birthday(01背包)
- HDU 5305 Friends (搜索+剪枝) 2015多校联合第二场
- 2015多校联合第二场 5305 friends 深搜
- hdu 5348 MZL's endless loop 2015多校联合训练赛#5 找环+dfs
- 2015暑假多校联合---Zero Escape(变化的01背包)
- hdu 5409 CRB and Graph 2015多校联合训练赛#10 dfs
- HDU 5305 Friends(2015多校联合)
- hdu 5386 Cover 2015多校联合训练赛#8 枚举
- hdu 5400 Arithmetic Sequence 2015多校联合训练赛#9 枚举
- [DFS] 多校联合第二场 F题 Friends
- 2015暑假多校联合---Mahjong tree(树上DP 、深搜)
- HDU 5371 (2015多校联合训练赛第七场1003)Hotaru's problem(manacher+二分/枚举)
- HDU 5305 Friends (搜索+剪枝) 2015多校联合第二场
- HDU 5305 Friends(2015多校第二场 dfs + 剪枝)
- hdu 5416 CRB and Tree 2015多校联合训练赛#10 枚举
- hdu 5358 First One 2015多校联合训练赛#6 枚举
- HDU 5319 Painter (2015 Multi-University Training Contest 3 2015多校联合)