HDU 5305 Friends(DFS)

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 \le n \le 8)$ and $m\ (0 \le m \le \frac{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 \neq 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

题意:给出n个人,m个朋友关系,每个关系可以为在线或离线,问如果要让每个人的在线朋友数和离线朋友数相同,那么关系组成有多少种情况。(n<=8,m<=n*(n-1)/2)

如果一个人的朋友数(转化为点的度)为奇数,肯定不能让在线朋友和离线朋友数相同。这种情况剔除之后,把每个人的朋友数分成相同的两拨,然后就是走关系图的每一条边,走一遍是一种情况。

#include
#include
using namespace std;
int n,m,ans;
int c1[100],c2[100];
struct node
{
    int x,y;
} e[100];
void dfs(int cur)
{
    if(cur==m+1)
    {
        ans++;
        return;
    }
    int v=e[cur].x;
    int u=e[cur].y;
    if(c1[v]&&c1[u])
    {
        c1[v]--;
        c1[u]--;
        dfs(cur+1);
        c1[u]++;
        c1[v]++;

    }
    if(c2[v]&&c2[u])
    {
        c2[v]--;
        c2[u]--;
        dfs(cur+1);
        c2[u]++;
        c2[v]++;
    }
    return;
}
int main()
{
    int t,i,du[100],flag;
    cin>>t;
    while(t--)
    {
        ans=0;
        flag=0;
        memset(du,0,sizeof(du));
        cin>>n>>m;
        for(i=1; i<=m; i++)
        {
            cin>>e[i].x>>e[i].y;
            du[e[i].x]++;
            du[e[i].y]++;
        }
        for(i=1; i<=n; i++)
        {
            if(du[i]&1)
            {
                cout<<0<


你可能感兴趣的:(搜索)