您的位置:首页 > 其它

hdu5305 (dfs) + 剪枝

2015-08-03 16:03 260 查看
题目大意;给你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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: