2015 多校賽 第二場 1006 (hdu 5305)

來源:互聯網
上載者:User

標籤:

Problem DescriptionThere 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. 
 

 

InputThe 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. 
 

 

OutputFor each testcase, print one number indicating the answer. 

 

Sample Input23 31 22 33 14 41 22 33 44 1 

 

Sample Output02

 

題意:給出一幅無向圖。依次對邊染白色或黑色,使得每個點所關聯的白邊和黑邊數目相同。問有多少種染色方法。

 

思路:

搜尋題。強行暴力搜必定逾時。枚舉點來搜尋也不好寫。因此枚舉邊。

記錄每個點的度數,若有點的度數為奇數,直接輸出0。否則,將所有點的度數除以2,得到每個點關聯的白邊數目和黑邊數目,此為剪枝。

代碼如下:

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int t,n,m,a[30],b[30],d[10];int B[30],W[30],ans;void dfs(int k){    if(k==m){        ans++;return;    }    int u=a[k],v=b[k];    if(B[u]<d[u]&&B[v]<d[v]){        B[u]++,B[v]++;        dfs(k+1);        B[u]--,B[v]--;    }    if(W[u]<d[u]&&W[v]<d[v]){        W[u]++,W[v]++;        dfs(k+1);        W[u]--,W[v]--;    }}int main(){    scanf("%d",&t);    while(t--){        bool flag=true;        scanf("%d%d",&n,&m);        memset(d,0,sizeof(d));        for(int i=0;i<m;i++){            scanf("%d%d",&a[i],&b[i]);            d[a[i]]++;d[b[i]]++;        }        for(int i=1;i<=n;i++){            if(d[i]&1)                flag=false;            d[i]/=2;        }        if(!flag){            puts("0");            continue;        }        memset(W,0,sizeof(W));        memset(B,0,sizeof(B));        ans=0;        dfs(0);        printf("%d\n",ans);    }    return 0;}
View Code

 

2015 多校賽 第二場 1006 (hdu 5305)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.