HDU 5305(Friends-暴搜)

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(n1)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;
}




posted on 2017-05-27 14:04  yjbjingcha  阅读(161)  评论(0编辑  收藏  举报

导航