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中随意取几个数(不取也算作一种方案),被一个人取过的数不能被另一个人再取.两个人合法的取法是,其中一个人取的任何数必须与另一个人取的每一个数都互质,求所有合法的方案数 (数据范 ...
随机推荐
- Java线程及Jvm监控工具
Java线程状态 线程的五种状态 * 新建:new(时间很短) * 运行:runnable * 等待:waitting(无限期等待),timed waitting(限期等待) * 阻塞:blocked ...
- HTML5——移动端的点击、拖拽
移动端浏览器不支持mouse事件 https://www.cnblogs.com/joyco773/p/6519668.html https://www.cnblogs.com/yjhua/p/525 ...
- Windows离线安装Python第三方库的方法
在window中,离线安装第三方模块, 1.下载第三方库的压缩文件,解压,将解压后的文件放到Python安装目录下的Lib\site_packages中 2. 将Python添加到环境变量里 3.进入 ...
- 动软生成器添加Mysql注释
1.解决没有mysql注释问题 替换原文件下载地址 2.更新Models模板 <#@ template language="c#" HostSpecific="Tr ...
- Apache 和 Nginx 下的 URL 重写
URL 重写和重定向 URL 重写是将页面映射到本站另一页面, 而重定向则是将页面映射到另一主机(域名). 其中临时重定向(R=302)和永久重定向(R=301)都是亲搜索引擎的, 是 SEO 的重要 ...
- JPA API与注解
一.JPA API Persistence 类:用于获取 EntityManagerFactory 实例,该类含有静态方法 createEntityManagerFactory. //persiste ...
- POJ3616 Milking Time【dp】
Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her producti ...
- 搭建私有docker仓库
安装docker yum install docker 启动docker systemctl start docker 拉取registry镜像 docker pull registry 创建仓库配置 ...
- linux动态库加载路径修改
1.在 /etc/ld.so.conf 文件中添加搜索路径,重启或者 ldconfig 生效: 2.在 /etc/ld.so.conf.d 目录下添加 *.conf 文件,其中可以添加搜索路径,重启获 ...
- 【Codeforces 1091D】New Year and the Permutation Concatenation
[链接] 我是链接,点我呀:) [题意] 把1~n的n!种排列依次连接成一个长度为nn!的序列. 让你在这个序列当中找长度为n的连续段,使得连续段中的数字的和为n(n-1)/2 输出符合要求的连续段的 ...