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>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>

using namespace std;

struct
{
    int u;
    int v;
}e[100];

int c1[100], c2[100];
int num[100];
int ans;
int n, m;

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

int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        memset(c1, 0, sizeof(c1));
        memset(c2, 0, sizeof(c2));
        memset(num, 0, sizeof(num));

        scanf("%d%d",&n,&m);
        for (int i = 1; i <= m;i++)
        {
            scanf("%d%d",&e[i].u,&e[i].v);
            num[e[i].u]++;
            num[e[i].v]++;
        }
        int ok = 1;
        for (int i = 1; i <= n; i++)
        {
            c1[i] = c2[i] = num[i] / 2;
            if (num[i] & 1)
            {
                ok = 0;
                break;
            }
        }
        if (!ok)
        {
            printf("0\n");
            continue;
        }
        ans = 0;
        dfs(1);
        printf("%d\n",ans);
    }
    return 0;
}

参考自:http://blog.csdn.net/Tc_To_Top/article/details/47027079

版权声明:转载请注明出处。

时间: 2025-01-31 00:47:34

hdu 5305 Friends 【暴搜】的相关文章

HDU 1045 DFS暴搜

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6229    Accepted Submission(s): 3506 Problem Description Suppose that we have a square city with straight streets. A map of a city is a s

HDU 5012 bfs暴搜

Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 243    Accepted Submission(s): 135 Problem Description There are 2 special dices on the table. On each face of the dice, a distinct number wa

HDU 5339 Untitled(暴搜)

Untitled Problem Description There is an integer $a$ and $n$ integers $b_1, \ldots, b_n$. After selecting some numbers from $b_1, \ldots, b_n$ in any order, say $c_1, \ldots, c_r$, we want to make sure that $a \ mod \ c_1 \ mod \ c_2 \ mod \ldots \ m

HDU 5305 Friends(深搜)

题意:t组数据,每组一个n和m表示点数和边数,接下来m条边,每条边两种状态,求每个点邻接边的两种状态数目相同的排列方式有几种 分析:从第一个顶点开始往下深搜每条边,每条边两种状态,注意回朔. 代码: #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int maxn = 10; int n,m,ans; int e

[HDU 5135] Little Zu Chongzhi&#39;s Triangles (dfs暴搜)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5135 题目大意:给你n条边,选出若干条边,组成若干个三角形,使得面积和最大.输出最大的面积和. 先将边从小到大排序,这样前面的两条边加起来如果不大于第三条边就可以跳出,这是一个存在性条件. dfs(int idx,int now,int cnt,int nowmax)代表我当前处理的是第idx条边,已经加入边集的有cnt条边,当前的边的长度和为now,组成的最大面积和为nowmax. 暴力枚举每个三

HDU 4284 Travel Folyd预处理+dfs暴搜

题意:给你一些N个点,M条边,走每条边要花费金钱,然后给出其中必须访问的点,在这些点可以打工,但是需要先拿到证书,只可以打一次,也可以选择不打工之直接经过它.一个人从1号点出发,给出初始金钱,问你能不能访问所以的点,并且获得所以证书. 题解:目标是那些一定要访问的点,怎么到达的我们不关心,但是我们关系花费最少的路径,而且到达那个点后是一定要打工的,如果只是经过,那么在求花费最少的路径的时候已经考虑过了. 因此先用Folyd求出各个点直接的最短路径,由于N很小,又只要求出一个解,所以直接dfs暴搜

hdu 4400 离散化+二分+BFS(暴搜剪枝还超时的时候可以借鉴一下)

Mines Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1110    Accepted Submission(s): 280 Problem Description Terrorists put some mines in a crowded square recently. The police evacuate all peo

hdu 4848 Wow! Such Conquering! (暴搜+强剪枝)

Wow! Such Conquering! Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description There are n Doge Planets in the Doge Space. The conqueror of Doge Space

hdu 5348 MZL&#39;s endless loop 暴搜

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 MZL's endless loop Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1426    Accepted Submission(s): 319 Special Judge Problem Description As w