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 (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
 
 
1A
dfs
没啥说的
/*************************************************************************
    > File Name: code/whust/#9/K.cpp
    > Author: 111qqz
    > Email: rkz2013@126.com 
    > 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
" , ans );
    }
}
原文地址:https://www.cnblogs.com/111qqz/p/4706178.html