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 }
  • 相关阅读:
    动手动脑3
    动手动脑2
    编写一个文件分割工具,能把一个大文件分割成多个小的文件。并且能再次把他们合并起来得到完整的文件
    编写一个文件加解密程序通过命令行完成加解密工作
    编写一个程序指定一个文件夹,能自动计算出其总容量
    Java中常见的异常处理汇总
    覆盖 动手动脑
    课堂代码验证
    如何在静态方法中访问类的实例成员 及查询“你已经创建了多少个对象”
    Java的字段初始化规律
  • 原文地址:https://www.cnblogs.com/pblr/p/4810230.html
Copyright © 2011-2022 走看看