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. Android Studio3.0打包APK的时候 报错:

    Error:Execution failed for task ':app:transformDexWithDexForRelease'.> com.android.build.api.tran ...

  2. PKI中常用编码:ASN.1 DER BER Base64

    迟到了两年的笔记... 在PKI的应用中,常会用到以下几个编码概念: ASN.1(Abstract Syntax Notation One, 抽象语法标记) 定义:A standard interfa ...

  3. Linux系统命令及文件的浏览、管理和维护

    在linux中什么是一个文件的路径呢,说白了就是这个文件存在的地方,例如在上一章提到的/root/.ssh/authorized_keys 这就是一个文件的路径.如果你告诉系统这个文件的路径,那么系统 ...

  4. HDU_3496_(二维费用背包)

    Watch The Movie Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)T ...

  5. codeforces_456C_dp

    链接:http://codeforces.com/problemset/problem/456/C C. Boredom time limit per test 1 second memory lim ...

  6. js延时加载的方法

    js的延迟加载有助与提高页面的加载速度,以下是延迟加载的几种方法: 1.使用setTimeout延迟方法的加载时间 延迟加载js代码,给网页加载留出更多时间 <script type=" ...

  7. ocelot+consul+identity4的使用demo

    Ocelot网关搭建 搭建core控制台项目 本demo采用2.1版本 命名为APPIGateWay 在Nuget包中添加Ocelot引用 选用8.0.0版本 添加Ocelot.json 文件 内容为 ...

  8. Linux常用命令——关机与重启命令

    1.shutdown命令 shutdown [选项] 时间 --使用shutdown进行关机或重启会正确保存正在使用的服务,其他命令有一定的危险性,建议最好使用shutdown命令进行关机重启 选项: ...

  9. 如何使用fio模拟线上环境

    线上表现 这里我想通过fio来模拟线上的IO场景,那么如何模拟呢? 首先使用iostat看线上某个盘的 使用情况,这里我们需要关注的是 avgrq-sz, avgrq-qz. #iostat -dx ...

  10. spring 中属性scope 的prototype(有状态)和singleton(无状态)

    默认情况下,从bean工厂所取得的实例为Singleton(bean的singleton属性) Singleton: Spring容器只存在一个共享的bean实例, 默认的配置. Prototype: ...