您的位置:首页 > 其它

hdu 5305 Friends(dfs)

2015-09-15 14:36 225 查看
[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

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int ans,n,m;
int v[10],v1[10],v2[10];
struct p
{
int u,v;
};p s[60];
void dfs(int i)
{
if (i==m+1)
{
ans++;
return ;
}
if (v1[s[i].u]&&v1[s[i].v])
{
v1[s[i].u]--;
v1[s[i].v]--;
dfs(i+1);
v1[s[i].u]++;
v1[s[i].v]++;
}
if (v2[s[i].u]&&v2[s[i].v])
{
v2[s[i].u]--;
v2[s[i].v]--;
dfs(i+1);
v2[s[i].u]++;
v2[s[i].v]++;
}
}
int main()
{
int i,j,flag,t;
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&n,&m);
flag=0;
memset(v,0,sizeof(v));
memset(v1,0,sizeof(v1));
memset(v2,0,sizeof(v2));
for (i=1;i<=m;i++)
{
scanf("%d%d",&s[i].u,&s[i].v);
v[s[i].u]++;
v[s[i].v]++;
}
for (i=1;i<=n;i++)
{
if (v[i]%2==1)
{
flag=1;
break;
}
v1[i]=v2[i]=v[i]/2;
}
ans=0;
dfs(1);
printf("%d\n",ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: