hdu5305 (dfs) + 剪枝

题目大意;给你n,m表示n个人,其中有m对朋友关系并告诉你,求每个人拥有线上和线下朋友下等的个数

思路:首先只要有一个人有奇数条边则结果为0.然后对所有边进行dfs即可。


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <iomanip>

using namespace std;
#define MAXN 100005
#define MOD 1000000007
int graph[9][9];
int dege[9];
int offline[9] , line[9];
int n , m;
int num;

struct node
{
    int x , y;
}arr[100];

void DFS(int k )
{
    if(k == m + 1)
    {
        num ++ ;
        return;
    }
    if(line[arr[k].x] && line[arr[k].y])
    {
        line[arr[k].x] -- ;
        line[arr[k].y] -- ;
        DFS(k + 1);
        line[arr[k].x] ++;
        line[arr[k].y] ++;
    }
    if(offline[arr[k].x] && offline[arr[k].y])
    {
        offline[arr[k].x] -- ;
        offline[arr[k].y] -- ;
        DFS(k + 1);
        offline[arr[k].x] ++;
        offline[arr[k].y] ++;
    }
    return ;
}

int main()
{
    int t;
    cin >> t ;
    while(t--)
    {
        scanf("%d %d" , &n , &m);
        memset(graph , 0 , sizeof(graph));
        memset(dege , 0 , sizeof(dege));
        memset(line , 0 , sizeof(line));
        memset(offline , 0 , sizeof(offline));
        int a , b;
        for(int i = 1 ; i <= m ; i ++)
        {
            scanf("%d %d" , &a , &b);
            arr[i].x = a , arr[i].y = b;
            dege[a]++;
            dege[b]++;
        }
        int flag = 0;
        for(int i = 1 ; i <= n ; i ++)
        {
            if(dege[i] & 1) flag = 1;
            line[i] = offline[i] = dege[i] / 2;
        }
        if(flag)
        {
            printf("0\n");
            continue;
        }
        num = 0;
        DFS(1);
        cout << num << endl;
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值