天天看點

2015多校聯合第二場 5305 friends 深搜

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 (1≤n≤8)  and  m (0≤m≤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≠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
        

Author XJZX  

Source 2015 Multi-University Training Contest 2

唔。。。這次題目沒讀錯,可是不會-_-/// 朋友分為線上好友和線下好友 給定若幹人和朋友關系 問有多少種配置設定方式保證每個人的兩種好友一樣多

據說這個相當于給邊染色@.@ 深搜 omg 五個數組 某邊入度點 in[i]  ;某邊出度點 out[i] ;某點的度  de[i] ;白色邊on[i] 黑色邊 off[i] 開頭剪枝一次就夠了==

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int m,n,ans;
int in[100],out[100],de[100],on[100],off[100];
void dfs(int u)
{
    if(u==m+1)
    {
        for(int i=1;i<=n;i++)
            if(on[i]!=off[i]) return;
        ans++;
        return;
    }
    if(on[in[u]]<de[in[u]]/2&&on[out[u]]<de[out[u]]/2)
    {
        on[in[u]]++;on[out[u]]++;
        dfs(u+1);
        on[in[u]]--;on[out[u]]--;
    }
    if(off[in[u]]<de[in[u]]/2&&off[out[u]]<de[out[u]]/2)
    {
        off[in[u]]++;off[out[u]]++;
        dfs(u+1);
        off[in[u]]--;off[out[u]]--;
    }
}
int main()
{
   // freopen("cin.txt","r",stdin);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(de,0,sizeof(de));
        memset(on,0,sizeof(on));
        memset(off,0,sizeof(off));
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&in[i],&out[i]);
            de[in[i]]++;de[out[i]]++;
        }
        int flag=1;
        for(int i=1;i<=n;i++)
        {
            if(de[i]%2==1) {printf("0\n");flag=0;break;}
        }
        if(!flag) continue;
        ans=0;
        dfs(1);
        printf("%d\n",ans);
    }
    return 0;
}
           

繼續閱讀