hdu 5305 Friends (dfs)

Friends

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

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

1A

dfs

没啥说的

/*************************************************************************
    > File Name: code/whust/#9/K.cpp
    > Author: 111qqz
    > Email: [email protected]
    > Created Time: 2015年08月05日 星期三 15时02分30秒
 ************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#define y0 abc111qqz
#define y1 hust111qqz
#define yn hez111qqz
#define j1 cute111qqz
#define tm crazy111qqz
#define lr dying111qqz
using namespace std;
#define REP(i, n) for (int i=0;i<int(n);++i)
typedef long long LL;
typedef unsigned long long ULL;
const int inf = 0x7fffffff;
int n ,m,ans;
int d[50];
int on[50],off[50];
int u[50],v[50];

bool ok(int x,int y)
{
    if ( !d[x] && !d[y] && on[x] == off[x] && on[y] == off[y] )
        return true;
    if ( !d[x] && d[y] && on[x] == off[x] )
        return true;
    if ( d[x] && !d[y] && on[y] == off[y] )
        return true;
    if ( d[x] && d[y] )
        return true;
    return false;
}

void dfs ( int i)
{
    if (i==m)
    {
        ans++;
        return;
    }
    int x = u[i];
    int y = v[i];
    d[x]--;
    d[y]--;
    on[x]++;
    on[y]++;
    if (ok(x,y))
        dfs (i+1);
    on[x]--;
    on[y]--;
    off[y]++;
    off[x]++;
    if ( ok( x,y))
        dfs (i+1);
    off[y]--;
    off[x]--;
    d[x]++;
    d[y]++;
}

int main ( )
{
    int T;
    cin>>T;
    while ( T-- )
    {
        ans = 0;
        scanf ("%d %d",&n,&m);
        memset (d,0,sizeof(d));
        memset (on,0,sizeof(on));
        memset (off,0,sizeof(off));
        for ( int i = 0 ; i < m ; i++ )
        {
            scanf ("%d%d",&u[i],&v[i]);
            d[u[i]]++;
            d[v[i]]++;
        }
        dfs (0);
        printf ( "%d\n" , ans );
    }
}
时间: 2024-12-29 01:36:00

hdu 5305 Friends (dfs)的相关文章

hdu 1716 排序2(dfs)

排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5817    Accepted Submission(s): 2229 Problem Description Ray又对数字的列产生了兴趣:现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡片上的数字(

HDU 5952 Counting Cliques(dfs)

Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1855    Accepted Submission(s): 735 Problem Description A clique is a complete graph, in which there is an edge between every pai

HDU 4414 Finding crosses(dfs)

Problem Description The Nazca Lines are a series of ancient geoglyphs located in the Nazca Desert in southern Peru. They were designated as a UNESCO World Heritage Site in 1994. The high, arid plateau stretches more than 80 kilometres (50 mi) between

HDU 6351 Beautiful Now(DFS)多校题解

思路:一开始对k没有理解好,k是指最多k次,不需要达到.这道题dfs暴力就行,我们按照全排列最大最小去找每一位应该和后面哪一位交换.k = 0没判断好WA了2发... 如果k >= len - 1,那么最大最小就是直接sort非前导零的答案.如果k < len - 1,那么我们交换肯定从最大位数交换,比如现在求最大值,那么我们从第一位依次判断,如果该位不是他后面最大的,那么就和后面最大的交换(如果最大的有多个,那么就每个都搜索一下),否则不消耗交换次数判断下一位.最小同理 注意前导零. 代码:

HDU 2553(N皇后)(DFS)

http://acm.hdu.edu.cn/showproblem.php?pid=2553 i表示行,map[i]表示列,然后用DFS遍历回溯 可以参考这篇文章: http://blog.csdn.net/cambridgeacm/article/details/7703739 1 #include <iostream> 2 #include <string> 3 #include <cstring> 4 #include <cstdlib> 5 #inc

hdu Rikka with string (dfs)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; char a[1000+100]; int vis[1000+100]; int n; int cnt; int ok; bool huiwen() { int l=0; int r=strlen(a)-1; while(l<r) { if(a[l]!=a[r])

HDU 5012 骰子旋转(DFS)

http://acm.hdu.edu.cn/showproblem.php?pid=5012 保存骰子的状态,然后用dfs或者bfs搜索 还是再讲一下dfs 我们的目标是找一个与b相同,且转次数最少的状态 dfs就是树状图,要明确每个状态下的分支,以及边界条件 有4种变换,所以每个状态下面有四种分支,又因为骰子转4次以上的状态没有意义,所以边界条件可以是4 每个状态起始时与b判断,如果相同,则更新结果 #include <iostream> #include <string> #i

HDU 2660 Accepted Necklace (DFS)

Accepted Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2474    Accepted Submission(s): 973 Problem Description I have N precious stones, and plan to use K of them to make a necklace f

hdu 1181 变形课 (dfs)简单搜索

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1181 变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 17876    Accepted Submission(s): 6435 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不