多校 hdu 5305

Friends

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1429    Accepted Submission(s): 725

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 (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.

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

Source

2015 Multi-University Training Contest 2

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
typedef pair<int , int > P;
#define maxn 10

int num[maxn], g1[maxn], g2[maxn], n, m, T;
P e[30];
int sum;

void dfs(int t)
{
    if(t == m + 1)
    {
        sum++;
        return ;
    }
    int u = e[t].first;
    int v = e[t].second;
    if(g1[u] && g1[v])//  online
    {
        g1[u]--, g1[v]--;
        dfs(t + 1);
        g1[u]++, g1[v]++;
    }

    if(g2[u] && g2[v])// offline
    {
        g2[u]--, g2[v]--;
        dfs(t + 1);
        g2[u]++, g2[v]++;
    }
    return ;
}

int main()
{
    scanf("%d", &T);
    while(T--)
    {
        memset(num, 0, sizeof num);
        memset(g1, 0, sizeof g1);
        memset(g2, 0, sizeof g2);
        int flag = 1;
        scanf("%d%d", &n, &m);
        int u, v;
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d", &u, &v);
            e[i] = {u, v};
            num[u]++;
            num[v]++;
        }
        for(int i=1; i<=n; i++)
        {
            if(num[i] & 1)
            {
                flag = 0;
                break;
            }
            num[i] >>= 1;
            g1[i] = g2[i] = num[i];
        }
        sum = 0;
        dfs(1);
        if(flag) printf("%d\n", sum);
        else printf("0\n");

    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-07 18:55:57

多校 hdu 5305的相关文章

多校-HDU 5351 MZL&#39;s Border 数学

f[1] = 'b', f[2] = 'a', f[i] = f[i - 1] + f[i - 2] 斐波那契数列的字符串,给你n和m,前m位中,最长的前缀等于后缀的长度是多少.1≤n≤1000, 1≤m≤length(f[n]) 规律题,虽然我不知道为什么. 1 import java.io.*; 2 import java.util.*; 3 import java.math.*; 4 public class Main{ 5 //Scanner cin = Scanner(System.i

hdu 5305 Friends(2015多校第二场第6题)记忆化搜索

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5305 题意:给你n个人,m条关系,关系可以是online也可以是offline,让你求在保证所有人online关系的朋友和offline关系的朋友相等的情况下,这样的情况有多少种. 思路:因为online关系和offline关系的人数相等,而且m最多才28,所以只要枚举每个人的一半的关系是否符合要求即可,而且根据题意m是奇数或者有一个人的总关系为奇数那么就没有符合要求的情况,这样可以排除很多情况.

HDU 5305 Friends (搜索+剪枝) 2015多校联合第二场

開始对点搜索,直接写乱了.想了想对边搜索,尽管复杂度高.剪枝一下水过去了. 代码: #include<cstdio> #include<iostream> #include<cstring> #include<vector> using namespace std; struct Edge{ int a,b; }G[35]; int n,m,deg[10],on[10],off[10]; int res; void init(){ memset(deg,0,

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

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, everyo

[多校2015.02.1006 高斯消元] hdu 5305 Friends

题意: 给你n个人m条关系 每条关系包括a,b 代表a和b能够是线上朋友也能够是线下朋友 然后保证每一个人的线上朋友数和线下朋友数相等 问你有多少种组成方法 思路: 官方题解是爆搜+剪枝,然而并不会写. . 比赛的时候想到用高斯消元来剪枝 最后枚举自由元 由于关系的话到了最后肯定有些关系是确定的. 这样一定会消掉一些部分 最后G++AC C++TLE. . 代码: #include"cstdlib" #include"cstdio" #include"cs

HDU 5305 Friends(2015多校第二场 dfs + 剪枝)

Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 552    Accepted Submission(s): 253 Problem Description There are  people and  pairs of friends. For every pair of friends, they can choos

【HDU 5305】Friends 多校第二场(双向DFS)

依据题意的话最多32条边,直接暴力的话 2 ^ 32肯定超时了.我们能够分两次搜索时间复杂度降低为 2 * 2  ^ 16 唯一须要注意的就是对眼下状态的哈希处理. 我採用的是 十进制表示法 跑的还是比較快的,可能是用STL函数的原因添加了一些常数复杂度. #include<map> #include<vector> #include<cstdio> #include<cstring> #include<algorithm> using name

hdu 5305 Friends 【暴搜】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5305 题意:给一个无向图 , 每条边可以是online边也可以是offline边,问 有多少种方法使得每个节点的online边和offline边一样多 解法:暴搜.记录每个点连接的边数,奇数的直接不可能,偶数的分成两个数组,c1[i]表示i的在线朋友数,c2[i]表示i的离线朋友数,然后一条边一条边搜就行了. 代码: #include <stdio.h> #include <ctime>

HDU 5305 Friends (深搜)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5305 题面: Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1149    Accepted Submission(s): 569 Problem Description There are n people and m