hdu 3503(有点小技巧的dfs(对结点加东西表示边的某些状态))

Friends

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


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
 

 

Source
 

 

Recommend
wange2014   |   We have carefully selected several similar problems for you:  5309 5308 5307 5306 5304 
题目描述:
将图中的每个点相连的边分配成两种边,求有多少种分配方式.
每条边有两种状态的选择,怎么样枚举边的选择是个问题,采用两个数组c1[]和c2[]分别记录每个点这两种边的数量,初始数量相等,
选0状态,c1[i]数组--,选1状态,c2[i]数组--,这样就可以枚举边的选择.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
#define maxn 30
int n,m;
int ans;
int c1[maxn],c2[maxn];
int deg[maxn];
struct Edge
{
    int from,to;
}edges[101000];

void init()
{
    ans=0;
    memset(c1,0,sizeof(c1));
    memset(c2,0,sizeof(c2));
    memset(deg,0,sizeof(deg));
}

void dfs(int e)
{
    if(e==m+1)   //能够搜到第m+1次,说明每条边都分配了0或者1,减了2*m次,c1和c2数组都为0
    {
        ans++;
        return ;
    }
    int from=edges[e].from,to=edges[e].to;
    if(c1[from] && c1[to]) //这条边分配0
    {
       c1[from]--;  c1[to]--;
       dfs(e+1);
       c1[from]++;  c1[to]++;
    }
    if(c2[from] && c2[to])  //这条边分配1
    {
        c2[from]--; c2[to]--;
        dfs(e+1);
        c2[from]++; c2[to]++;
    }
    return ; //如果这条边既不能分配0,也不能分配1,只能回溯
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&edges[i].from,&edges[i].to);
            deg[edges[i].from]++;
            deg[edges[i].to]++;
        }
      //  for(int i=1;i<=n;i++)
         //   printf("%d ",deg[i]);
       // cout<<endl;
        int flag=0;
        for(int i=1;i<=m;i++)
        {
            int from=edges[i].from,to=edges[i].to;
            if( (deg[from] & 1))
            {
                flag=1; break;
            }
            if( (deg[to] & 1))
            {
                flag=1; break;
            }
            c1[from]= (deg[from] >> 1);  //from的在线朋友数量
            c2[from]= (deg[from] >> 1);   //from的在线朋友数量
            //from这个点的在线朋友和离线朋友都为它度数的一半
            c1[to] = (deg[to] >> 1); //to的离线朋友数量
            c2[to]= (deg[to] >> 1);     //to的离线朋友数量
        }
        if(flag==1)
        {
            printf("0\n");
            continue;
        }
        dfs(1);
        printf("%d\n",ans);
    }
    return 0;
}

 

posted on 2015-07-25 15:28  爱装逼的书呆子  阅读(226)  评论(0编辑  收藏  举报

导航