標籤:
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)