您的位置:首页 > 其它

2015 多校赛 第二场 1006 (hdu 5305)

2015-08-04 20:37 267 查看
[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each testcase, print one number indicating the answer.

[align=left]Sample Input[/align]

2

3 3

1 2

2 3
3 1
4 4

1 2
2 3

3 4
4 1

[align=left]Sample Output[/align]

0
2

题意:给出一幅无向图。依次对边染白色或黑色,使得每个点所关联的白边和黑边数目相同。问有多少种染色方法。

思路:

搜索题。强行暴力搜必定超时。枚举点来搜索也不好写。因此枚举边。

记录每个点的度数,若有点的度数为奇数,直接输出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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: