HDU 5305 Friends(dfs+剪枝)

Friends

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


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

题意:给你n个人(n<=8),m个关系(m<=n*(n-1)/2),每个m包含x,y表示x和y是好朋友。任意一个人可以和他一个朋友保持在线关系或者离线关系。求使所有人都能和他朋友有一半的在线关系和一半的离线关系的方法总数。

思路:dfs+剪枝,也就是暴力枚举所有情况+剪枝。刚开始我用状压怎么剪也TLE,无奈改成dfs。需要注意的是:

1、只要有一个人的关系是奇数种(度为奇数),则方法总数一定为0

2、直接dfs会TLE。注意到在合法情况下,当每个人只剩下一个关系未dfs枚举时,其实这个关系已经确定了。因此在这个地方剪枝可以减少大量的时间。

代码:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=50;
const ll mo=1e9+7;
int n,m,k,x,y,f;
int a[maxn],sum[maxn],you[maxn];
int c[maxn],pos[maxn],l[maxn],r[maxn];
int ans,ct,cnt,tmp,flag;
int vis[10];
bool jud()
{
    for(int i=1;i<=n;i++)
    {if(sum[i]+1==pos[i]&&i<a[i])
    {
        sum[i]++;
        sum[a[i]]++;
        vis[i]=1;
    }
    if(sum[i]!=pos[i]) return 0;
    }
    return 1;
}
void dfs(int i)
{
    if(i==m){
    if(jud())ans++;
    for(int i=1;i<=n;i++)if(vis[i])
    {
        sum[i]--;sum[a[i]]--;
        vis[i]=0;
    }
    return;}
    sum[l[i]]++;sum[r[i]]++;
    dfs(i+1);
    sum[l[i]]--;sum[r[i]]--;
    dfs(i+1);
}
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    {
        while(T--){
            scanf("%d%d",&n,&m);
            f=0;
            memset(c,0,sizeof(c));
            memset(vis,0,sizeof(vis));
            memset(a,0,sizeof(a));
            ans=0;f=0;
            for(int i=0;i<m;i++)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                c[x]++;
                c[y]++;
                if(!a[x]&&!a[y])
                {
                    a[x]=y;
                    a[y]=x;
                }
                else {l[f]=x;r[f++]=y;}
            }
            m=f;
            int flag=1;
            for(int i=1;i<=n;i++)
            {
                if(c[i]&1) flag=0;
                pos[i]=c[i]/2;
            }
            if(!flag) puts("0");
            else
            {
               memset(sum,0,sizeof(sum));
                ans=0;
                dfs(0);
                printf("%d\n",ans);
            }
    }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lsd20164388/article/details/81053458