Friends

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

Total Submission(s): 668    Accepted Submission(s): 313

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

题目大意:给出n个人,m个朋友关系,每一个关系能够为在线或离线,问假设要让每一个人的在线朋友数和离线朋友数同样,那么关系组成有多少种情况。(n<=8,m<=n*(n-1)/2)

由题目能够推出假设m=25,26,27,28,那么n一定为8,那么总会有一个人的朋友数位奇数。关系组成情况数为0

如今m最大为24,那么就非常好做了,能够对关系进行状压(0:离线。1:在线),或者直接深搜。方式都是一样的,遍历全部的情况。

注意:m为24时数目还是比較大的,须要剪枝一下。能够在每一个人的关系中选出一条,这一条边是不用枚举的。由于能够由已经推出的情况来决定这条边是不是在线。这样至少会降低1条边。每降低一条边。那么搜索的复杂度就会降低一半。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
struct node{
int u , v ;
}p[30];
int n , m ;
int sum[10] , num[10] , cnt , flag[10] , vis[10] ;
void dfs(int s) {
int i ;
if( s == m ) {
for(i = 1 ; i <= n ; i++) {
if( sum[i]/2 == num[i]+1 && i < flag[i] ) {
num[i]++ ;
num[ flag[i] ]++ ;
vis[i] = 1 ;
}
if( sum[i]/2 != num[i] ) break ;
}
if( i > n ) cnt++ ;
for(i = 1 ; i <= n ; i++) {
if( vis[i] ) {
num[i]-- ;
num[ flag[i] ]-- ;
vis[i] = 0 ;
}
}
return ;
}
dfs(s+1) ;
num[ p[s].u ]++ ;
num[ p[s].v ]++ ;
dfs(s+1) ;
num[ p[s].u ]-- ;
num[ p[s].v ]-- ;
}
int main() {
int t , i ;
int u , v , cid ;
//freopen("f.in","r",stdin) ;
//freopen("1.txt","w",stdout) ;
scanf("%d", &t) ;
while( t-- ) {
scanf("%d %d", &n, &m) ;
memset(sum,0,sizeof(sum)) ;
memset(num,0,sizeof(num)) ;
memset(flag,0,sizeof(flag)) ;
memset(vis,0,sizeof(vis)) ;
cid = cnt = 0 ;
for(i = 0 ; i < m ; i++) {
scanf("%d %d", &u, &v) ;
sum[u]++ ;
sum[v]++ ;
if( flag[u] == 0 && flag[v] == 0 ) {
flag[u] = v ;
flag[v] = u ;
}
else {
p[cid].u = u ;
p[cid++].v = v ;
}
}
m = cid ;
for(i = 1 ; i <= n ; i++)
if( sum[i]%2 ) break ;
if( i <= n ) {
printf("0\n") ;
continue ;
}
/*if( n == 8 && m == 24 ) {
printf("2648\n") ;
continue ;
}*/
dfs(0) ;
printf("%d\n", cnt) ;
}
return 0 ;
}

hdu5305(2015多校2)--Friends(状压,深搜)的更多相关文章

  1. bzoj1054: [HAOI2008]移动玩具 状压+爆搜即可

    题意:在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移动次数将初的玩具状态 ...

  2. UVA 818 Cutting Chains(状压 + 暴搜)题解

    题意:有1~n个小环,他们中的有些互相扣在一起,问你至少切开几个能把这写小环串成一条链 思路:还是太菜了,题目给的n<=15,显然可以暴力解决. 用二进制表示每个环切还是不切,然后搜索所有情况. ...

  3. 【Luogu】P4363一双木棋(状压爆搜)

    题目链接 唉,只有AC了这道题才会感叹考场上没有想出解法的我是多么智障. 我甚至连任何想法都没有. 天啊我当时到底在想些什么. AC这道题我就能进前15了诶. 我们发现只要确定了轮廓线那么此时的状态就 ...

  4. 多校7 HDU5816 Hearthstone 状压DP+全排列

    多校7 HDU5816 Hearthstone 状压DP+全排列 题意:boss的PH为p,n张A牌,m张B牌.抽取一张牌,能胜利的概率是多少? 如果抽到的是A牌,当剩余牌的数目不少于2张,再从剩余牌 ...

  5. hdu5305 Friends[状压dp]

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

  6. [多校联考2019(Round 5 T1)] [ATCoder3912]Xor Tree(状压dp)

    [多校联考2019(Round 5)] [ATCoder3912]Xor Tree(状压dp) 题面 给出一棵n个点的树,每条边有边权v,每次操作选中两个点,将这两个点之间的路径上的边权全部异或某个值 ...

  7. BZOJ 4197 NOI 2015 寿司晚宴 状压DP

    4197: [Noi2015]寿司晚宴 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 694  Solved: 440[Submit][Status] ...

  8. 牛客多校3 A-PACM Team(状压降维+路径背包)

    PACM Team 链接:https://www.nowcoder.com/acm/contest/141/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144 ...

  9. NOI 2015 寿司晚宴 (状压DP+分组背包)

    题目大意:两个人从2~n中随意取几个数(不取也算作一种方案),被一个人取过的数不能被另一个人再取.两个人合法的取法是,其中一个人取的任何数必须与另一个人取的每一个数都互质,求所有合法的方案数 (数据范 ...

随机推荐

  1. 【C++】智能指针简述(一):智能指针的引入

    智能指针是C++中一种利用RAII机制(后面解释),通过对象来管理指针的一种方式. 在C++中,动态开辟的内存需要我们自己去维护,在出函数作用域或程序异常退出之前,我们必须手动释放掉它,否则的话就会引 ...

  2. 使ThinkPHP(3.2.3)的分页类支持Bootstrap风格

    ThinkPHP 3.2.3自带的分页类位于:/ThinkPHP/Library/Think/Pages.class.php ,官方文档在这里:ThinkPHP3.2.3数据分页 Pages.clas ...

  3. java内存组成

     java内存组成介绍:堆(Heap)和非堆(Non-heap)内存 按照官方的说法:“Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配.堆是在 Java 虚拟机启动 ...

  4. arx代码片段

    ObjectARX代码片段二   转载自网络 一  在ARX中禁用AutoCAD的某个命令 以LINE命令为例,在程序中加入下面的一句即可禁用LINE命令: acedCommand(RTSTR, &q ...

  5. 梦想CAD控件关于曲线问题

    IMxDrawCurve 接口 控件中的曲线接口,实现了曲线的相关操作,如求曲线的长度,最近点,面积,曲线上任一点在曲线上的长度 切向方向,曲线交点,坐标变换,打断,偏移,离散等功能. 一.返回曲线组 ...

  6. php中 如何找到session 的保存位置

    [前言] 刚刚想测试FQ操作,需要删除session,这里记录分享下 [主体] (1)想要查看session保存的目录,需要先找到 php.ini配置文件 (2)在php.ini文件中查找 sessi ...

  7. 13Microsoft SQL Server SQL 高级事务,锁,游标,分区

    Microsoft SQL Server SQL高级事务,锁,游标,分区 通过采用事务和锁机制,解决了数据库系统的并发性问题. 9.1数据库事务 (1)BEGIN TRANSACTION语句定义事务的 ...

  8. 字符串系列——KMP模板整理

    KMP模板整理 KMP与扩展KMP: /*vs 2017/ vs code以外编译器,去掉windows.h头文件和system("pause");*/ #include<i ...

  9. 【搜索、bfs】Find The Multiple

    Problem   Given a positive integer n, write a program to find out a nonzero multiple m of n whose de ...

  10. react-router v4 学习实践

    最近学习了 react-router v4,根据官方 API 文档和网上资源做了一个简单的路由示例. 先用官方的工具 create-react-app  初始化一个 react 项目模板,再根据自己的 ...