2015 多校联赛 ——HDU5305(搜索)


Friends

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

 Total Submission(s): 163    Accepted Submission(s): 61


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. 
 


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


求:朋友关系可离线可在线,要求每个人的离线朋友数等于在线朋友数,总共有多少种方法

先对入度判断,奇数直接不可能,然后再进行搜索


#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

struct node
{
    int u,v;
} pnode[50];

int in[10];
int c[10],c2[10];
int ans,n,m;
int num,tem;



void dfs(int u)
{
    if(u == m+1)
    {
        for(int i = 1; i <= n; i++)
            if(c[i] != c2[i])
                return;
        ans++;
        return ;
    }
    int a = pnode[u].u;
    int b = pnode[u].v;
    if(c[a] < in[a]/2 && c[b] < in[b]/2)
    {
        c[a] ++;
        c[b] ++;
        dfs(u+1);
        c[a]--;
        c[b]--;
    }
    if(c2[a] < in[a]/2 && c2[b] < in[b]/2)
    {
        c2[a] ++;
        c2[b] ++;
        dfs(u+1);
        c2[a]--;
        c2[b]--;
    }
    return;
}



int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int flag = 0;
        scanf("%d%d",&n,&m);
        memset(in,0,sizeof(in));
        for(int i = 1; i <= m; i++)
        {
            scanf("%d%d",&pnode[i].u,&pnode[i].v);
            in[pnode[i].u]++;
            in[pnode[i].v]++;
        }
        memset(c,0,sizeof(c));
        memset(c2,0,sizeof(c2));
        for(int i = 1; i <= n; i++)
        {
            if(in[i] % 2 == 1)
            {
                flag = 1;
                break;
            }
        }
        if(flag){
            printf("0
");
            continue;
        }
        ans = 0;
        dfs(1);

        printf("%d
",ans);

    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/Przz/p/5409829.html