2015暑假多校联合---Friends(dfs枚举)

原题链接

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

Recommend

wange2014

题意:输入n,m,n表示有n个人,m表示m对朋友关系,现在要使每个人的朋友划分为在线朋友和离线朋友,且在线朋友和离线朋友数量相等(一对朋友之间只能是在线朋友或者离线朋友),求方案数;

思路:用dfs深搜枚举每一条边(即每一对朋友关系),若能深搜进行完最后一条边,即当前边cnt==m+1  则ans++;

代码如下:

#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;
int n,m,cnt,ans;
int c1[10],c2[10],d[10];
struct Node
{
    int u,v;
}node[200];

void dfs(int i)
{
    if(i-1==m)
    {
        ans++;
        return ;
    }
    if(c1[node[i].u]&&c1[node[i].v])
    {
        c1[node[i].u]--;
        c1[node[i].v]--;
        dfs(i+1);
        c1[node[i].u]++;
        c1[node[i].v]++;
    }
    if(c2[node[i].u]&&c2[node[i].v])
    {
        c2[node[i].u]--;
        c2[node[i].v]--;
        dfs(i+1);
        c2[node[i].u]++;
        c2[node[i].v]++;
    }
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cnt=0;
        ans=0;
        scanf("%d%d",&n,&m);
        memset(node,0,sizeof(node));
        memset(c1,0,sizeof(c1));
        memset(c2,0,sizeof(c2));
        memset(d,0,sizeof(d));
        for(int i=1;i<=m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            node[++cnt].u=u;
            node[cnt].v=v;
            d[u]++;
            d[v]++;
        }
        int flag=0;
        for(int i=1;i<=n;i++)
        {
            c1[i]=c2[i]=d[i]/2;
            if(d[i]&1)
            {
                flag=1;
                break;
            }
        }
        if(flag)
        {
            puts("0");
            continue;
        }
        dfs(1);
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-09-29 23:53:02

2015暑假多校联合---Friends(dfs枚举)的相关文章

2015暑假多校联合---Mahjong tree(树上DP 、深搜)

题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artist. Today he is playing mahjong alone. He suddenly feels that the tree in the yard doesn't look good. So he wants to decorate the tree.(The tree has n

2015暑假多校联合---CRB and His Birthday(01背包)

题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthday. His mom decided to buy many presents for her lovely son.She went to the nearest shop with M Won(currency unit).At the shop, there are N kinds of pr

2015暑假多校联合---Expression(区间DP)

题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5396 Problem Description Teacher Mai has n numbers a1,a2,?,anand n−1 operators("+", "-" or "*")op1,op2,?,opn−1, which are arranged in the form a1 op1 a2 op2 a3 ? an. He wan

2016暑假多校联合---Windows 10

2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on the top of a mountain. Recently, our old monk found the operating system of his computer was updating to windows 10 automatically and he even can't j

2016暑假多校联合---Substring(后缀数组)

2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a string, he has to calculate the total number of its distinct substrings. But ?? thinks that is too easy, he wants to make this problem more interesti

2016暑假多校联合---Rikka with Sequence (线段树)

2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta has an array A with n numbers. Then he make

2016暑假多校联合---Another Meaning

2016暑假多校联合---Another Meaning Problem Description As is known to all, in many cases, a word has two meanings. Such as “hehe”, which not only means “hehe”, but also means “excuse me”. Today, ?? is chating with MeiZi online, MeiZi sends a sentence A to

2015年多校联合训练第一场OO’s Sequence(hdu5288)

题意:给定一个长度为n的序列,规定f(l,r)是对于l,r范围内的某个数字a[i],都不能找到一个对应的j使得a[i]%a[j]=0,那么l,r内有多少个i,f(l,r)就是几.问所有f(l,r)的总和是多少. 公式中给出的区间,也就是所有存在的区间. 思路:直接枚举每一个数字,对于这个数字,如果这个数字是合法的i,那么向左能扩展的最大长度是多少,向右能扩展的最大长度是多少,那么i为合法的情况就是左长度*右长度(包含i且i是合法的区间总数). 统计左长度可以判断a[i]的约数是否在前面出现过-因

2016暑假多校联合---Joint Stacks (STL)

HDU  5818 Problem Description A stack is a data structure in which all insertions and deletions of entries are made at one end, called the "top" of the stack. The last entry which is inserted is the first one that will be removed. In another wor