hdu 5305 Friends (dfs)

来源:互联网 发布:淘宝商城-情侣装 编辑:程序博客网 时间:2024/05/19 09:13

题目地址

http://acm.hdu.edu.cn/showproblem.php?pid=5305

Friends

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


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
23 31 22 33 14 41 22 33 44 1
 

Sample Output
02
 

思路

一开始没有想到是用DFS,模拟了半天全WA了。。不过在n=1时我清楚为什么输出为2就A了。。

AC代码

#include<iostream>#include<algorithm>#include<stdio.h>#include<cstring>using namespace std;int sum[10],on[10],off[10],ans,m;struct edge{    int u,v;};edge p[100000];void dfs(int now){    if(now==m+1){        ans++;        return;    }    int u=p[now].u;    int v=p[now].v;    if(on[u]&&on[v])    {        on[u]--;        on[v]--;        dfs(now+1);        on[u]++;        on[v]++;    }    if(off[u]&&off[v])    {        off[u]--;        off[v]--;        dfs(now+1);        off[u]++;        off[v]++;    }    return;}int main(){    int tot,f;    int n;    scanf("%d",&tot);    while(tot--){        ans=0;        memset(sum,0,sizeof(sum));        memset(on,0,sizeof(on));        memset(off,0,sizeof(off));        scanf("%d%d",&n,&m);        for(int i=1;i<=m;i++){            scanf("%d%d",&p[i].u,&p[i].v);            sum[p[i].u]++;            sum[p[i].v]++;        }        bool f=false;        for(int i=1;i<=n;i++){            on[i]=sum[i]/2;            off[i]=sum[i]/2;            if(sum[i]&1)            {                f=true;                break;            }        }        if(f){            printf("0\n");            continue;        }        dfs(1);        printf("%d\n",ans);    }    return 0;}



0 0
原创粉丝点击