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


    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
    2 3 3 1 2 2 3 3 1 4 4 1 2 2 3 3 4 4 1
     

    Sample Output
    0 2

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

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

    如今m最大为24,那么就非常好做了,能够对关系进行状压(0:离线。1:在线),或者直接深搜。方式都是一样的,遍历全部的情况。

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

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std ;
    struct node{
        int u , v ;
    }p[30];
    int n , m ;
    int sum[10] , num[10] , cnt , flag[10] , vis[10] ;
    void dfs(int s) {
        int i ;
        if( s == m ) {
            for(i = 1 ; i <= n ; i++) {
                if( sum[i]/2 == num[i]+1 && i < flag[i] ) {
                    num[i]++ ;
                    num[ flag[i] ]++ ;
                    vis[i] = 1 ;
                }
                if( sum[i]/2 != num[i] ) break ;
            }
            if( i > n ) cnt++ ;
            for(i = 1 ; i <= n ; i++) {
                if( vis[i] ) {
                    num[i]-- ;
                    num[ flag[i] ]-- ;
                    vis[i] = 0 ;
                }
            }
            return ;
        }
        dfs(s+1) ;
        num[ p[s].u ]++ ;
        num[ p[s].v ]++ ;
        dfs(s+1) ;
        num[ p[s].u ]-- ;
        num[ p[s].v ]-- ;
    }
    int main() {
        int t , i ;
        int u , v , cid ;
        //freopen("f.in","r",stdin) ;
        //freopen("1.txt","w",stdout) ;
        scanf("%d", &t) ;
        while( t-- ) {
            scanf("%d %d", &n, &m) ;
            memset(sum,0,sizeof(sum)) ;
            memset(num,0,sizeof(num)) ;
            memset(flag,0,sizeof(flag)) ;
            memset(vis,0,sizeof(vis)) ;
            cid = cnt = 0 ;
            for(i = 0 ; i < m ; i++) {
                scanf("%d %d", &u, &v) ;
                sum[u]++ ;
                sum[v]++ ;
                if( flag[u] == 0 && flag[v] == 0 ) {
                    flag[u] = v ;
                    flag[v] = u ;
                }
                else {
                    p[cid].u = u ;
                    p[cid++].v = v ;
                }
            }
            m = cid ;
            for(i = 1 ; i <= n ; i++)
                if( sum[i]%2 ) break ;
            if( i <= n ) {
                printf("0
    ") ;
                continue ;
            }
            /*if( n == 8 && m == 24 ) {
                printf("2648
    ") ;
                continue ;
            }*/
            dfs(0) ;
            printf("%d
    ", cnt) ;
        }
        return 0 ;
    }
    


  • 相关阅读:
    jquery 表单清空
    CK-Editor content.replace
    CSS DIV HOVER
    返回上一页并刷新与返回上一页不刷新代码
    Google Java编程风格指南中文版
    编程常见英语词汇
    教你如何删除tomcat服务器的stdout.log文件
    @Autowired @Resource @Qualifier的区别
    JSTL标签,EL表达式,OGNL表达式,struts2标签 汇总
    4.11 application未注入报错解决
  • 原文地址:https://www.cnblogs.com/lytwajue/p/6927904.html
Copyright © 2020-2023  润新知