Friends

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

Total Submission(s): 878    Accepted Submission(s): 422

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 (1≤n≤8) and m (0≤m≤n(n−1)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 x≠y 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
 
Source



    题意:n个人,分别有各自的朋友,有的关系好,有的关系不好,如今要求
每一个人的朋友中关系好的与关系不好的数量要同样,问多少种方案?
    思路:由于须要关系好的与关系不好的数量要同样。所以朋友的数量为奇数
的能够直接输出0,(由于奇数没有办法让两者同样),推断完奇数后进行搜索,先找出朋友中关系好的,剩下的自己主动归为关系不好的之中,假设出现两个人之间有一个人好朋友与坏朋友已经满了的,直接return。一个省时间的地方。


比赛的时候让队友做的,当时没有看出来是搜索。啊啊啊啊啊








#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h> using namespace std; int n,m;
int num[10];
int pa[10],pb[10];
int sum; struct node
{
int x;
int y;
}q[100100]; void DFS(int p)
{
//printf("p = %d\n",p);
if(p == m)
{
sum++;
return ;
}
int x = q[p].x;
int y = q[p].y;
if(pa[x] && pa[y])
{
pa[x]--;
pa[y]--;
DFS(p+1);
pa[x]++;
pa[y]++;
}
if(pb[x] && pb[y])
{
pb[x]--;
pb[y]--;
DFS(p+1);
pb[x]++;
pb[y]++;
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
sum = 0;
memset(num,0,sizeof(num));
scanf("%d%d",&n,&m);
int x,y;
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
q[i].x = x;
q[i].y = y;
num[x]++;
num[y]++;
}
int flag = 0;
for(int i=1;i<=n;i++)
{
pa[i] = num[i]/2;
pb[i] = num[i]/2;
if(num[i]%2 == 1)
{
flag = 1;
break;
}
}
if(flag)
{
printf("0\n");
continue;
}
DFS(0);
printf("%d\n",sum);
}
return 0;
}


 

HDU 5305 Friends(简单DFS)的更多相关文章

  1. hdu - 1704 Rank(简单dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1704 遇到标记过的就dfs,把隐含的标记,最后计数需要注意. #include <cstdio> # ...

  2. HDU 5305 Friends(dfs)

    Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  3. HDOJ(HDU).1035 Robot Motion (DFS)

    HDOJ(HDU).1035 Robot Motion [从零开始DFS(4)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DF ...

  4. Counting Cliques HDU - 5952 单向边dfs

    题目:题目链接 思路:这道题vj上Time limit:4000 ms,HDU上Time Limit: 8000/4000 MS (Java/Others),且不考虑oj测评机比现场赛慢很多,但10月 ...

  5. HDU 1401 Solitaire 双向DFS

    HDU 1401 Solitaire 双向DFS 题意 给定一个\(8*8\)的棋盘,棋盘上有4个棋子.每一步操作可以把任意一个棋子移动到它周围四个方向上的空格子上,或者可以跳过它四个方向上的棋子(就 ...

  6. HDU 2085 核反应堆 --- 简单递推

    HDU 2085 核反应堆 /* HDU 2085 核反应堆 --- 简单递推 */ #include <cstdio> ; long long a[N], b[N]; //a表示高能质点 ...

  7. Red and Black(简单dfs)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. HDOJ(HDU).2660 Accepted Necklace (DFS)

    HDOJ(HDU).2660 Accepted Necklace (DFS) 点我挑战题目 题意分析 给出一些石头,这些石头都有自身的价值和重量.现在要求从这些石头中选K个石头,求出重量不超过W的这些 ...

  9. HDOJ(HDU).1045 Fire Net (DFS)

    HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...

  10. HDOJ(HDU).1241 Oil Deposits(DFS)

    HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

随机推荐

  1. 全志tina v3.0系统编译时的时间错误的解决(全志SDK的维护BUG)

    全志tina v3.0系统编译时的时间错误的解决(全志SDK的维护BUG) 2018/6/13 15:52 版本:V1.0 开发板:SC3817R SDK:tina v3.0 1.01原始编译全志r1 ...

  2. D1-mini esp8266的资料备份

    需要更新esp8266库 http://arduino.esp8266.com/stable/package_esp8266com_index.json 下载好即可.

  3. Ionic2/angularJs2中的静态类 PhotoLibrary 调用不上

    photoLibrary调用报错:No provider for PhotoLibrary: 在调用相册文件时有用到photolibrary,总有些莫名的报错,3月份的时候这个坑让我不知所措,现在写下 ...

  4. python数于字符串

    python数于字符串 1.了解数的类型 2.什么事字符串 3.引号的使用与区别 4.转义符 5.自然字符串 6.字符串的重复 7.子字符串 #了解数的类型 1.python中数的类型主要有五种,分为 ...

  5. 三维重建:GitHub百度Apollo 2.0

    GitHub:https://github.com/ApolloAuto/apollo 1. 关于Apollo的数据:Apollo的数据会如何开放? 自动驾驶数据将包括具有高分辨率图像和像素级别标注的 ...

  6. Crash reporter

    A crash reporter is a software application whose function is to identify report crash details and to ...

  7. MFC_2.6 使用菜单列表和控件

    使用菜单列表和控件 1.添加List Control控件 2.属性设置VIEW 为REPORT 3.初始化 // 1. 设置列表的扩展风格 m_ListCtrl.SetExtendedStyle(LV ...

  8. day19-常用模块IV(re、typing)

    目录 re模块 typing模块 爬取音频 re模块 用来从字符串(文本)中查找特定的东西 1.元字符:有特殊意义的字符 ^ 从开头匹配 import re a = re.findall('^abc' ...

  9. jmeter元件的作用域和顺序

    jmeter是一个开源的性能测试工具,它可以通过鼠标拖拽来随意改变元件之间的顺序以及元件的父子关系,那么随着它们的顺序和所在的域不同,它们在执行的时候,也会有很多不同. jmeter的test pla ...

  10. JavaScript--小白入门篇2

    一.布尔值和关系运算符.逻辑运算符 1.1 布尔值   我们上篇文章说了,学习了两种变量的类型数值型.字符串型.   实际上,还有很多变量的类型.我们今天再学习一种,叫做“布尔类型”. 数值型里面的值 ...