码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5305(Friends-暴搜)

时间:2015-08-17 15:37:42      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

Friends

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1717    Accepted Submission(s): 854


Problem Description
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.
 

Input
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 (1n8)技术分享 and m (0mn(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 xy技术分享 and every friend relationship will appear at most once.
 

Output
For each testcase, print one number indicating the answer.
 

Sample Input
2 3 3 1 2 2 3 3 1 4 4 1 2 2 3 3 4 4 1
 

Sample Output
0 2
 

Author
XJZX
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:  5395 5394 5393 5392 5391 
 

暴搜




#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (100+10)
#define MAXM (100+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int n,m;
int e[MAXM][2];
int degree[MAXN],totdeg[MAXN];
ll ans;
bool check(int x,int y) 
{
	return  ( ( totdeg[x]||( !degree[x] ))   &&  ( totdeg[y]||( !degree[y] ))  );

}
void dfs(int p)
{
	if (p==m)
	{
		For(i,n)
			if (i!=e[p][0]&&i!=e[p][1]&°ree[i]) return ;
		if (degree[e[p][0]]==degree[e[p][1]]&&abs(degree[e[p][0]])==1) {
			ans++;
		} 
		return ;	
	}
//	if (p==m+1) 
//	{
//		ans++;
//		return;
//	}
	int x=e[p][0],y=e[p][1];
	totdeg[x]--;totdeg[y]--;
	degree[x]++;degree[y]++;
	if (check(x,y)) dfs(p+1);
	degree[x]-=2;degree[y]-=2;
	if (check(x,y)) dfs(p+1);
	degree[x]++;degree[y]++;
	totdeg[x]++;totdeg[y]++;
}
int main()
{
//	freopen("F.in","r",stdin);
	
	int T;cin>>T;
	while(T--) {
		ans=0; MEM(degree) MEM(totdeg)  
		cin>>n>>m;
		For(i,m) scanf("%d%d",&e[i][0],&e[i][1]),totdeg[e[i][0]]++,totdeg[e[i][1]]++;
		
		bool flag=0;
		For(i,n) if (totdeg[i] & 1) {
			flag=1;puts("0");break;
		}
		if (flag) continue;
		
		if (m) dfs(1); else ans=1;
		printf("%lld\n",ans);
	}
	
	return 0;
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 5305(Friends-暴搜)

标签:

原文地址:http://blog.csdn.net/nike0good/article/details/47724905

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!