HDU 5305 Friends

http://acm.hdu.edu.cn/showproblem.php?pid=5305

题意:

n个人m个朋友关系,保证每一个人的朋友关系中在线关系和非在线关系相等,有多少种可能

分析:

1.如果存在某个人的朋友关系为奇数,则为0种方法

2.暴力深搜,将m个关系分为两种情况,为0或为1满足条件继续深搜

代码:

#include<bits/stdc++.h>
using namespace std;
int a[15],num1[15],num2[15];
#define mod 1000000007
int n,m,ans;
struct node
{
    int u,to;
}v[105];
void dfs(int x)
{
    if(x==m)ans++;
    else
    {
        int aa,bb;
        aa=v[x].u;
        bb=v[x].to;
        if(num1[aa]&&num1[bb])
        {
            num1[aa]--;
            num1[bb]--;
            dfs(x+1);
            num1[aa]++;
            num1[bb]++;
        }
        if(num2[aa]&&num2[bb])
        {
            num2[aa]--;
            num2[bb]--;
            dfs(x+1);
            num2[aa]++;
            num2[bb]++;
        }
    }
}
int main()
{
    int t,i,j,x,y;
    scanf("%d",&t);
    while(t--)
    {
        ans=0;
        scanf("%d%d",&n,&m);

        memset(num1,0,sizeof(num1));
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&v[i].u,&v[i].to);
            num1[v[i].u]++;
            num1[v[i].to]++;
        }
        bool flag=0;
        for(i=1;i<=n;i++)
        {
            if(num1[i]%2){
                flag=1;
                break;
            }
            num1[i]=num2[i]=num1[i]/2;
        }
        if(flag)
        {
            printf("0\n");
            continue;
        }
        dfs(0);
        printf("%d\n",ans);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值