zoukankan      html  css  js  c++  java
  • hdu5305 Friends(dfs,多校题)

    Friends

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 2945    Accepted Submission(s): 1413


    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 (1n8) and m (0mn(n1)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 xy 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

    有n个人和m对朋友关系,他们中间有关系好的,也有关系不好的。要使他们每个人的好朋友和一般朋友数量相等(比如一个人有2个好朋友,那么他就应该也有2个一般朋友),有多少种方案。

    输入的时候就可以保存每个人的朋友总数,如果有一个人的朋友总数是奇数,那么就不可能达到题目要求的条件,直接输出0;

    不然的话,就另外用2个数组保存每个人的好朋友数量和一般朋友数量,都是朋友总数的一半。

    接下来就是dfs,先找好朋友关系,如果两个人中有一个人的好朋友关系用完了,那就只能看这两个人的坏朋友关系,如果其中有一个人的坏朋友关系也用完了,那就return;像第一个案例就是没到p==m,就return了。不然就继续找下去;能到p==m就表示所有组都己经找完了,而且也肯定是一半好朋友,一半坏朋友,不然的话中间就return了,不会再往下找(看了好久才明白emmmm)

    http://www.voidcn.com/article/p-eifhpyyl-bhn.html

     1 #include<bits/stdc++.h> 
     2 using namespace std;
     3 int n,m;
     4 int num[10];
     5 int pa[10],pb[10];
     6 int sum;
     7 struct node
     8 {
     9     int x;
    10     int y;
    11 }q[50];
    12 
    13 void DFS(int p)
    14 {
    15     //printf("p = %d
    ",p);
    16    if(p == m)
    17    {
    18        sum++;
    19        return ;
    20    }
    21    int x = q[p].x;
    22    int y = q[p].y;
    23    if(pa[x] && pa[y])//x和y的好朋友次数都还有 
    24    {
    25        pa[x]--;
    26        pa[y]--;
    27       // cout<<"好朋友"<<x<<" "<<y<<endl;
    28        DFS(p+1);
    29        pa[x]++;
    30        pa[y]++;
    31    }
    32    if(pb[x] && pb[y])//x和y的坏朋友次数都还有
    33    {
    34        pb[x]--;
    35        pb[y]--;
    36     //   cout<<"坏朋友"<<x<<" "<<y<<endl;
    37        DFS(p+1);
    38        pb[x]++;
    39        pb[y]++;
    40    }
    41 }
    42 
    43 int main()
    44 {
    45     int T;
    46     scanf("%d",&T);
    47     while(T--)
    48     {
    49         sum = 0;
    50         memset(num,0,sizeof(num));
    51         memset(pa,0,sizeof(pa));
    52         memset(pb,0,sizeof(pb));
    53         scanf("%d%d",&n,&m);
    54         int x,y;
    55         for(int i=0;i<m;i++)
    56         {
    57             scanf("%d%d",&x,&y);
    58             q[i].x = x;
    59             q[i].y = y;
    60             num[x]++;//x的朋友数量加1 
    61             num[y]++;
    62         }
    63         int flag = 0;
    64         for(int i=1;i<=n;i++)
    65         {
    66             pa[i] = num[i]/2;//i的好朋友是i的朋友总数的一半 
    67             pb[i] = num[i]/2;//一般朋友 
    68             if(num[i]%2 == 1)//因为要每个人的朋友好和一般朋友相等,所以如果出现奇数肯定不行 
    69             {
    70                 flag = 1;
    71                 break;
    72             }
    73         }
    74        // cout<<endl; 
    75         if(flag)
    76         {
    77             printf("0
    ");
    78             continue;
    79         }
    80         DFS(0);
    81         printf("%d
    ",sum);
    82     }
    83     return 0;
    84 }
  • 相关阅读:
    SkyWalking链路追踪系统-告警篇
    在k8s中解决pod资源的正确识别
    SkyWalking链路追踪系统-接入篇
    Jenkins API+Pipeline深度实践之input的自动化
    SkyWalking链路追踪系统-部署篇
    DevOps建设之基于钉钉OA审批流的自动化上线
    使用kube-prometheus部署k8s监控(最新版)
    基于k8s手动部署rabbitmq集群
    ant desgin vue中table复选框根据状态disabled置灰
    ant design vue 中tree实现单选
  • 原文地址:https://www.cnblogs.com/fqfzs/p/9961198.html
Copyright © 2011-2022 走看看