hdu5305 (dfs) + 剪枝
2015-08-03 16:03
260 查看
题目大意;给你n,m表示n个人,其中有m对朋友关系并告诉你,求每个人拥有线上和线下朋友下等的个数
思路:首先只要有一个人有奇数条边则结果为0.然后对所有边进行dfs即可。
思路:首先只要有一个人有奇数条边则结果为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; }
相关文章推荐
- 周记——20150803
- vs2015与Irrlicht鬼火引擎
- javassist库小实例
- Android Studio debug调试
- CAN2.0A 和CAN2.0B
- Jsp中四种传递参数的方法
- QT编程中编译的时候出现c_jkdacx0.obj : error LNK2001: 无法解析的外部符号 "class QTextCodec * GlobalCodec" ?
- java正则表达式(字符串)
- 升级win10后如何清除最近打开文件记录?
- Android使用HTML和JavaScript制作页面
- codeVS 3914昆虫繁殖
- Algorithms—66.Plus One
- SD卡上读写文件
- Leetcode-199(Java) Binary Tree Right Side View
- 如何统计在一篇文章中某个单词出现了几次,以及第一次出现的位置
- ant+ivy,集成到eclipse中
- 用CorePlot实现类似Air Quality的柱状图滚动效果(2/2)
- JSP-EL表达式
- MFC画图(画线、画矩形、画刷画笔的使用)
- C语言结构体中加typedef的区别(文章转移)