hdu5305(2015多校2)--Friends(状压,深搜)

来源:互联网 发布:露华浓淘宝是真的吗 编辑:程序博客网 时间:2024/06/05 03:52

Friends

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


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
23 31 22 33 14 41 22 33 44 1
 

Sample Output
02

题目大意:给出n个人,m个朋友关系,每个关系可以为在线或离线,问如果要让每个人的在线朋友数和离线朋友数相同,那么关系组成有多少种情况。(n<=8,m<=n*(n-1)/2)

由题目可以推出如果m=25,26,27,28,那么n一定为8,那么总会有一个人的朋友数位奇数,关系组成情况数为0

现在m最大为24,那么就很好做了,可以对关系进行状压(0:离线,1:在线),或者直接深搜,方式都是一样的,遍历所有的情况。

注意:m为24时数目还是比较大的,需要剪枝一下,可以在每个人的关系中选出一条,这一条边是不用枚举的,因为可以由已经推出的情况来决定这条边是不是在线,这样至少会减少1条边,每减少一条边,那么搜索的复杂度就会减少一半。

[cpp] view plain copy
  1. #include <cstdio>  
  2. #include <cstring>  
  3. #include <algorithm>  
  4. using namespace std ;  
  5. struct node{  
  6.     int u , v ;  
  7. }p[30];  
  8. int n , m ;  
  9. int sum[10] , num[10] , cnt , flag[10] , vis[10] ;  
  10. void dfs(int s) {  
  11.     int i ;  
  12.     if( s == m ) {  
  13.         for(i = 1 ; i <= n ; i++) {  
  14.             if( sum[i]/2 == num[i]+1 && i < flag[i] ) {  
  15.                 num[i]++ ;  
  16.                 num[ flag[i] ]++ ;  
  17.                 vis[i] = 1 ;  
  18.             }  
  19.             if( sum[i]/2 != num[i] ) break ;  
  20.         }  
  21.         if( i > n ) cnt++ ;  
  22.         for(i = 1 ; i <= n ; i++) {  
  23.             if( vis[i] ) {  
  24.                 num[i]-- ;  
  25.                 num[ flag[i] ]-- ;  
  26.                 vis[i] = 0 ;  
  27.             }  
  28.         }  
  29.         return ;  
  30.     }  
  31.     dfs(s+1) ;  
  32.     num[ p[s].u ]++ ;  
  33.     num[ p[s].v ]++ ;  
  34.     dfs(s+1) ;  
  35.     num[ p[s].u ]-- ;  
  36.     num[ p[s].v ]-- ;  
  37. }  
  38. int main() {  
  39.     int t , i ;  
  40.     int u , v , cid ;  
  41.     //freopen("f.in","r",stdin) ;  
  42.     //freopen("1.txt","w",stdout) ;  
  43.     scanf("%d", &t) ;  
  44.     while( t-- ) {  
  45.         scanf("%d %d", &n, &m) ;  
  46.         memset(sum,0,sizeof(sum)) ;  
  47.         memset(num,0,sizeof(num)) ;  
  48.         memset(flag,0,sizeof(flag)) ;  
  49.         memset(vis,0,sizeof(vis)) ;  
  50.         cid = cnt = 0 ;  
  51.         for(i = 0 ; i < m ; i++) {  
  52.             scanf("%d %d", &u, &v) ;  
  53.             sum[u]++ ;  
  54.             sum[v]++ ;  
  55.             if( flag[u] == 0 && flag[v] == 0 ) {  
  56.                 flag[u] = v ;  
  57.                 flag[v] = u ;  
  58.             }  
  59.             else {  
  60.                 p[cid].u = u ;  
  61.                 p[cid++].v = v ;  
  62.             }  
  63.         }  
  64.         m = cid ;  
  65.         for(i = 1 ; i <= n ; i++)  
  66.             if( sum[i]%2 ) break ;  
  67.         if( i <= n ) {  
  68.             printf("0\n") ;  
  69.             continue ;  
  70.         }  
  71.         /*if( n == 8 && m == 24 ) { 
  72.             printf("2648\n") ; 
  73.             continue ; 
  74.         }*/  
  75.         dfs(0) ;  
  76.         printf("%d\n", cnt) ;  
  77.     }  
  78.     return 0 ;  
  79. }  

原创粉丝点击