HDU 5305 Friends(简单DFS)

Friends

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

Total Submission(s): 878    Accepted Submission(s): 422

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

Source

2015 Multi-University Training Contest 2

    题意:n个人,分别有各自的朋友,有的关系好,有的关系不好,如今要求

每一个人的朋友中关系好的与关系不好的数量要同样,问多少种方案?

    思路:由于须要关系好的与关系不好的数量要同样。所以朋友的数量为奇数

的能够直接输出0,(由于奇数没有办法让两者同样),推断完奇数后进行搜索,先找出朋友中关系好的,剩下的自己主动归为关系不好的之中,假设出现两个人之间有一个人好朋友与坏朋友已经满了的,直接return。一个省时间的地方。

比赛的时候让队友做的,当时没有看出来是搜索。啊啊啊啊啊

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

int n,m;
int num[10];
int pa[10],pb[10];
int sum;

struct node
{
    int x;
    int y;
}q[100100];

void DFS(int p)
{
    //printf("p = %d\n",p);
   if(p == m)
   {
       sum++;
       return ;
   }
   int x = q[p].x;
   int y = q[p].y;
   if(pa[x] && pa[y])
   {
       pa[x]--;
       pa[y]--;
       DFS(p+1);
       pa[x]++;
       pa[y]++;
   }
   if(pb[x] && pb[y])
   {
       pb[x]--;
       pb[y]--;
       DFS(p+1);
       pb[x]++;
       pb[y]++;
   }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        sum = 0;
        memset(num,0,sizeof(num));
        scanf("%d%d",&n,&m);
        int x,y;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            q[i].x = x;
            q[i].y = y;
            num[x]++;
            num[y]++;
        }
        int flag = 0;
        for(int i=1;i<=n;i++)
        {
            pa[i] = num[i]/2;
            pb[i] = num[i]/2;
            if(num[i]%2 == 1)
            {
                flag = 1;
                break;
            }
        }
        if(flag)
        {
            printf("0\n");
            continue;
        }
        DFS(0);
        printf("%d\n",sum);
    }
    return 0;
}
时间: 2024-08-04 09:42:17

HDU 5305 Friends(简单DFS)的相关文章

hdu - 1704 Rank(简单dfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1704 遇到标记过的就dfs,把隐含的标记,最后计数需要注意. 1 #include <cstdio> 2 #include <cstring> 3 int n; 4 int vis[501][501]; 5 void dfs(int x,int y) 6 { 7 for(int i=1;i<=n;i++) 8 if(vis[y][i]) 9 { 10 vis[x][i]=1; 11 dfs

HDU 5305 Friends(dfs)

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

hdu 1016 Prime Ring Problem (简单DFS)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25700    Accepted Submission(s): 11453 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

hdu 4739Zhuge Liang&#39;s Mines(简单dfs,需要注意重点)

Zhuge Liang's Mines Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1166    Accepted Submission(s): 505 Problem Description In the ancient three kingdom period, Zhuge Liang was the most famous

hdu 1520Anniversary party(简单树形dp)

Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4310    Accepted Submission(s): 1976 Problem Description There is going to be a party to celebrate the 80-th Anniversary of the

Counting Cliques HDU - 5952 单向边dfs

题目:题目链接 思路:这道题vj上Time limit:4000 ms,HDU上Time Limit: 8000/4000 MS (Java/Others),且不考虑oj测评机比现场赛慢很多,但10月5号的计蒜客重现赛只给了1000ms确实有点过分吧,好久没有做这种简单dfs做到自闭了,,,题目并不难,注意剪枝就好了,建图时建标号小的点指向标号大的点的单向边,这样按标号从小到大搜一遍就好了,完全图的任意两个点都要有边,按点的标号搜到第n-s+1个点,因为后面所有的点加起来都组不成点数为s的完全子

hdu 1501 Zipper (dfs+记忆化搜索)

Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6491    Accepted Submission(s): 2341 Problem Description Given three strings, you are to determine whether the third string can be formed

HDU 2119 Matrix 简单二分匹配

行做x集,列做y集,1就给该行该列连一条边,输出最大匹配边即可 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<set> using namespace std; #define N 105 int lef[N], pn;//lef[v]表示Y集的点v 当前连接的点 , pn为x点集的

Red and Black(简单dfs)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12519    Accepted Submission(s): 7753 Problem Description There is a rectangular room, covered with square tiles. Each tile is color

hdu 1518 Square (dfs搜索可参考poj1011)

Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8589    Accepted Submission(s): 2784 Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end