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. 微信小程序一些常见的坑

    1.小程序都报wxss编译错误 解决方法: 在控制台输入openVendor() ,清除里面的wcsc wcsc.exe 然后重启工具 2.微信小程序wx:for警告 Now you can prov ...

  2. 关于百度地图导航AndroidSDK的初始化问题

    使用百度地图有一段时间了,导航是一个一直困扰我的问题.今天刚发现百度地图的导航SDK并不是对Android6.0版本不兼容.而是对某一部分手机不兼容,准确的说是对X64或X86的cpu不兼容.下载百度 ...

  3. radiobutton group

    1. 环境:VS2010 2. 分组 将radio1.radio2.radio3分为1组,radio4.radio5分为另一组: 方法:设置  radio1  的 属性:  group.tabstop ...

  4. ThinkPHP---thinkphp模型(M)

    (1)配置数据库连接 数据库的连接配置可以在系统配置文件ThinkPHP/Conf/convention.php中找到 /* 数据库设置 */ 'DB_TYPE' => '', // 数据库类型 ...

  5. 04Oracle Database 登陆

    Oracle Database 登陆 EM Express Login https://localhost:5500/em/login cmd sqlplus SQL/PLUS system/code ...

  6. 后台中的sql注入

    aa.getSqlMap().put("order"," and a.id not in(\'"+po.getId()+"\')"); \' ...

  7. scala学习(3)-----wordcount【sparksession】

    参考: spark中文官方网址:http://spark.apachecn.org/#/ https://www.iteblog.com/archives/1674.html 一.知识点: 1.Dat ...

  8. php第二十九节课

    文件 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

  9. [Algorithm] 4. Ugly Number II

    Description Ugly number is a number that only havefactors 2, 3 and 5. Design an algorithm to find th ...

  10. UVA - 1615 Highway(贪心-区间选点问题)

    题目: 给定平面上n(n≤105)个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里得距离不超过D. 思路: 先自己造区间,然后贪心选点就可以了.之前做过一 ...