hdu5305(2015多校2)--Friends(状压,深搜)
Friends
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 668 Accepted Submission(s): 313
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.
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.
2
3 3
1 2
2 3
3 1
4 4
1 2
2 3
3 4
4 1
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(状压,深搜)的更多相关文章
- bzoj1054: [HAOI2008]移动玩具 状压+爆搜即可
题意:在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移动次数将初的玩具状态 ...
- UVA 818 Cutting Chains(状压 + 暴搜)题解
题意:有1~n个小环,他们中的有些互相扣在一起,问你至少切开几个能把这写小环串成一条链 思路:还是太菜了,题目给的n<=15,显然可以暴力解决. 用二进制表示每个环切还是不切,然后搜索所有情况. ...
- 【Luogu】P4363一双木棋(状压爆搜)
题目链接 唉,只有AC了这道题才会感叹考场上没有想出解法的我是多么智障. 我甚至连任何想法都没有. 天啊我当时到底在想些什么. AC这道题我就能进前15了诶. 我们发现只要确定了轮廓线那么此时的状态就 ...
- 多校7 HDU5816 Hearthstone 状压DP+全排列
多校7 HDU5816 Hearthstone 状压DP+全排列 题意:boss的PH为p,n张A牌,m张B牌.抽取一张牌,能胜利的概率是多少? 如果抽到的是A牌,当剩余牌的数目不少于2张,再从剩余牌 ...
- hdu5305 Friends[状压dp]
Friends Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Su ...
- [多校联考2019(Round 5 T1)] [ATCoder3912]Xor Tree(状压dp)
[多校联考2019(Round 5)] [ATCoder3912]Xor Tree(状压dp) 题面 给出一棵n个点的树,每条边有边权v,每次操作选中两个点,将这两个点之间的路径上的边权全部异或某个值 ...
- BZOJ 4197 NOI 2015 寿司晚宴 状压DP
4197: [Noi2015]寿司晚宴 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 694 Solved: 440[Submit][Status] ...
- 牛客多校3 A-PACM Team(状压降维+路径背包)
PACM Team 链接:https://www.nowcoder.com/acm/contest/141/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144 ...
- NOI 2015 寿司晚宴 (状压DP+分组背包)
题目大意:两个人从2~n中随意取几个数(不取也算作一种方案),被一个人取过的数不能被另一个人再取.两个人合法的取法是,其中一个人取的任何数必须与另一个人取的每一个数都互质,求所有合法的方案数 (数据范 ...
随机推荐
- NoSQL与关系数据库
关系型数据库:完全支持关系代数理论作为基础:有较大的数据规模:固定的数据库模式:查询效率快:强一致性:数据完整性较易实现:扩展性一般:可用性好. NoSQL:部分支持关系代数理论作为基础:有超大数据规 ...
- Android 读取asset文件
* * 从Assets中读取图片 */ private Bitmap getImageFromAssetsFile(String fileName) { Bitmap image = null; As ...
- dutacm.club_1089_A Water Problem_(dp)
题意:要获得刚好后n个'k'的字符串,有两种操作,1.花费x秒,增加或删除1个'k'; 2.花费y秒,使个数翻倍.问最少需要多少时间获得这个字符串. 思路:i为偶数个'k',dp[i]=min(dp[ ...
- RocketMQ学习笔记(13)----RocketMQ的Consumer消息重试
1. 概念 Producer端重试: 生产者端的消息失败,也就是Producer往MQ上发消息没有发送成功,比如网络抖动导致生产者发送消息到MQ失败. 这种消息失败重试我们可以手动设置发送失败重试的次 ...
- jboss解决ip访问受限问题
jboss启动后,localhost可以访问,127.0.0.1可以访问,但是内网ip却访问不了,比如ip是192.168.1.2,这个192.168.1.2就访问不到web页面 解决方案: jbos ...
- vue项目中添加百度地图功能及解决遇到的问题详解
第一步,在百度地图开放平台 申请密钥 (如果有密钥可以省略此步骤,朋友有也可以借) 地址:http://lbsyun.baidu.com/ 第二步,创建应用并填写表单(下面链接可参考) http:// ...
- centos7服务器安装fail2ban配合Firewalld防护墙防止SSH爆破与防护网站CC攻击
centos7服务器安装fail2ban配合Firewalld防护墙防止SSH爆破与防护网站CC攻击 1.检查firewalld是否启用 #如果您已经安装iptables建议先关闭 service i ...
- Oracle 把一个用户所有表的读权限授予另一个用户
create user <USER_NAME> identified by <PASSWORD>; grant create session TO <USER_NAME& ...
- react native 从头开始
1.react-native run-android 报错SDK location not found. Define location with sdk.dir in the local.prope ...
- top命令的用法
top命令的用法 2018年07月15日 09:50:04 zhuoya_ 阅读数:1858 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/z ...