zoukankan      html  css  js  c++  java
  • hdu 5305 Friends (dfs)

    Friends

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


    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
     
    Author
    XJZX
     
     
    1A
    dfs
    没啥说的
    /*************************************************************************
        > File Name: code/whust/#9/K.cpp
        > Author: 111qqz
        > Email: rkz2013@126.com 
        > Created Time: 2015年08月05日 星期三 15时02分30秒
     ************************************************************************/
    
    #include<iostream>
    #include<iomanip>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<string>
    #include<map>
    #include<set>
    #include<queue>
    #include<vector>
    #include<stack>
    #define y0 abc111qqz
    #define y1 hust111qqz
    #define yn hez111qqz
    #define j1 cute111qqz
    #define tm crazy111qqz
    #define lr dying111qqz
    using namespace std;
    #define REP(i, n) for (int i=0;i<int(n);++i)  
    typedef long long LL;
    typedef unsigned long long ULL;
    const int inf = 0x7fffffff;
    int n ,m,ans;
    int d[50];
    int on[50],off[50];
    int u[50],v[50];
    
    bool ok(int x,int y)
    {
        if ( !d[x] && !d[y] && on[x] == off[x] && on[y] == off[y] )
            return true;
        if ( !d[x] && d[y] && on[x] == off[x] )
            return true;
        if ( d[x] && !d[y] && on[y] == off[y] )
            return true;
        if ( d[x] && d[y] )
            return true;
        return false;
    }
    
    void dfs ( int i)
    {
        if (i==m)
        {
            ans++;
            return;
        }
        int x = u[i];
        int y = v[i];
        d[x]--;
        d[y]--;
        on[x]++;
        on[y]++;
        if (ok(x,y))
            dfs (i+1);
        on[x]--;
        on[y]--;
        off[y]++;
        off[x]++;
        if ( ok( x,y))
            dfs (i+1);
        off[y]--;
        off[x]--;
        d[x]++;
        d[y]++;
    }
    
    int main ( )
    {
        int T;
        cin>>T;
        while ( T-- )
        {
            ans = 0;
            scanf ("%d %d",&n,&m);
            memset (d,0,sizeof(d));
            memset (on,0,sizeof(on));
            memset (off,0,sizeof(off));
            for ( int i = 0 ; i < m ; i++ )
            {
                scanf ("%d%d",&u[i],&v[i]);
                d[u[i]]++;
                d[v[i]]++;
            }
            dfs (0);
            printf ( "%d
    " , ans );
        }
    }
  • 相关阅读:
    升级Nginx1.14.1以上版本
    MaxScale中间件部署数据库读写分离
    php文件锁解决少量并发问题
    使用mysql悲观锁解决并发问题
    配置和查看composer镜像
    PHP常用的 五种设计模式及应用场景
    全球免费公共 DNS 解析服务器 IP 地址列表推荐 (解决无法上网/加速/防劫持)
    九种跨域方式实现原理
    Hadoop中RPC协议小例子报错java.lang.reflect.UndeclaredThrowableException解决方法
    DataNode启动不成功——java.net.BindException: Port in use: localhost:0 Caused by: java.net.BindException: Cannot assign requested address解决办法
  • 原文地址:https://www.cnblogs.com/111qqz/p/4706178.html
Copyright © 2011-2022 走看看