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 }
  • 相关阅读:
    迅为i.MX6ULL终结者Linux MISC驱动运行测试
    迅为IMX6Q开发板非设备树uboot-修改默认环境变量
    Android固件编译-迅为3399开发板Android8系统编译
    迅为4412开发板-实验LEDS驱动一
    迅为i.MX6ULL终结者设备树下的Platform驱动运行测试
    迅为i.MX6ULL终结者设备树下的Platform驱动实验程序编写
    项目实战-广域网智能家居-把mosquitto移植到arm上
    迅为-iMX6ULL开发板-设置yocto文件系统开机自启动
    muduo库中的核心:std::bind和std::function
    浅析muduo库中的定时器设施
  • 原文地址:https://www.cnblogs.com/pblr/p/4810230.html
Copyright © 2011-2022 走看看