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 (1≤n≤8)
and m (0≤m≤n(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 x≠y
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

2015 Multi-University Training Contest 2

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;
}

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

时间: 2025-01-18 05:34:12

HDU 5305(Friends-暴搜)的相关文章

HDU 1045 DFS暴搜

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6229    Accepted Submission(s): 3506 Problem Description Suppose that we have a square city with straight streets. A map of a city is a s

HDU 5012 bfs暴搜

Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 243    Accepted Submission(s): 135 Problem Description There are 2 special dices on the table. On each face of the dice, a distinct number wa

HDU 5339 Untitled(暴搜)

Untitled Problem Description There is an integer $a$ and $n$ integers $b_1, \ldots, b_n$. After selecting some numbers from $b_1, \ldots, b_n$ in any order, say $c_1, \ldots, c_r$, we want to make sure that $a \ mod \ c_1 \ mod \ c_2 \ mod \ldots \ m

HDU 5305 Friends(深搜)

题意:t组数据,每组一个n和m表示点数和边数,接下来m条边,每条边两种状态,求每个点邻接边的两种状态数目相同的排列方式有几种 分析:从第一个顶点开始往下深搜每条边,每条边两种状态,注意回朔. 代码: #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int maxn = 10; int n,m,ans; int e

hdu 5305 Friends 【暴搜】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5305 题意:给一个无向图 , 每条边可以是online边也可以是offline边,问 有多少种方法使得每个节点的online边和offline边一样多 解法:暴搜.记录每个点连接的边数,奇数的直接不可能,偶数的分成两个数组,c1[i]表示i的在线朋友数,c2[i]表示i的离线朋友数,然后一条边一条边搜就行了. 代码: #include <stdio.h> #include <ctime>

[HDU 5135] Little Zu Chongzhi&#39;s Triangles (dfs暴搜)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5135 题目大意:给你n条边,选出若干条边,组成若干个三角形,使得面积和最大.输出最大的面积和. 先将边从小到大排序,这样前面的两条边加起来如果不大于第三条边就可以跳出,这是一个存在性条件. dfs(int idx,int now,int cnt,int nowmax)代表我当前处理的是第idx条边,已经加入边集的有cnt条边,当前的边的长度和为now,组成的最大面积和为nowmax. 暴力枚举每个三

HDU 4284 Travel Folyd预处理+dfs暴搜

题意:给你一些N个点,M条边,走每条边要花费金钱,然后给出其中必须访问的点,在这些点可以打工,但是需要先拿到证书,只可以打一次,也可以选择不打工之直接经过它.一个人从1号点出发,给出初始金钱,问你能不能访问所以的点,并且获得所以证书. 题解:目标是那些一定要访问的点,怎么到达的我们不关心,但是我们关系花费最少的路径,而且到达那个点后是一定要打工的,如果只是经过,那么在求花费最少的路径的时候已经考虑过了. 因此先用Folyd求出各个点直接的最短路径,由于N很小,又只要求出一个解,所以直接dfs暴搜

hdu 4400 离散化+二分+BFS(暴搜剪枝还超时的时候可以借鉴一下)

Mines Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1110    Accepted Submission(s): 280 Problem Description Terrorists put some mines in a crowded square recently. The police evacuate all peo

hdu 4848 Wow! Such Conquering! (暴搜+强剪枝)

Wow! Such Conquering! Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description There are n Doge Planets in the Doge Space. The conqueror of Doge Space

hdu 5348 MZL&#39;s endless loop 暴搜

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 MZL's endless loop Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1426    Accepted Submission(s): 319 Special Judge Problem Description As w