zoukankan      html  css  js  c++  java
  • hdu 5305 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 (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
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cmath>
     5 using namespace std;
     6 int ans,n,m;
     7 int v[10],v1[10],v2[10];
     8 struct p
     9 {
    10    int u,v;
    11 };p s[60];
    12 void dfs(int i)
    13 {
    14      if (i==m+1)
    15      {
    16          ans++;
    17          return ;
    18      }
    19      if (v1[s[i].u]&&v1[s[i].v])
    20      {
    21           v1[s[i].u]--;
    22           v1[s[i].v]--;
    23           dfs(i+1);
    24           v1[s[i].u]++;
    25           v1[s[i].v]++;
    26      }
    27      if (v2[s[i].u]&&v2[s[i].v])
    28      {
    29           v2[s[i].u]--;
    30           v2[s[i].v]--;
    31           dfs(i+1);
    32           v2[s[i].u]++;
    33           v2[s[i].v]++;
    34      }
    35 }
    36 int main()
    37 {
    38     int i,j,flag,t;
    39     scanf("%d",&t);
    40     while (t--)
    41     {
    42         scanf("%d%d",&n,&m);
    43         flag=0;
    44         memset(v,0,sizeof(v));
    45         memset(v1,0,sizeof(v1));
    46         memset(v2,0,sizeof(v2));
    47         for (i=1;i<=m;i++)
    48         {
    49            scanf("%d%d",&s[i].u,&s[i].v);
    50            v[s[i].u]++;
    51            v[s[i].v]++;
    52         }
    53         for (i=1;i<=n;i++)
    54         {
    55            if (v[i]%2==1)
    56            {
    57               flag=1;
    58               break;
    59            }
    60            v1[i]=v2[i]=v[i]/2;
    61         }
    62         ans=0;
    63         dfs(1);
    64         printf("%d
    ",ans);
    65     }
    66 }
  • 相关阅读:
    php 加反斜杠的原因与处理办法
    python2.7 正则表达式的学习
    关于thinkhphp3.1中废弃 preg_replace /e 修饰符
    python2.7 函数的参数学习
    Laravel中Homestead添加多站点时遇到问题
    安装pywin32时,出现找不到python27注册信息的解决办法
    递归例子
    在SUSE平台启动和关闭mysql服务
    SUSE11 安装mysql
    delphi调用dll动态库
  • 原文地址:https://www.cnblogs.com/pblr/p/4810230.html
Copyright © 2011-2022 走看看