HDU 5305 Friends(dfs)
Friends
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 297 Accepted Submission(s): 127
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个关系。每一个人和别人的关喜有线上和线下,求每一个人线上和线下的关系一样多的方案数
思路: dfs
先贴别人代码,跑的快
,我的跑的慢 */ #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map> #define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1) #define eps 1e-8
using namespace std; typedef __int64 LL; #define N 100 int x[N],y[N],in[N],on[N],off[N];
int n,m;
int ans; bool judge()
{
if(m&1) return false;
for(int i=1;i<=n;i++) if(in[i]&1) return false;
return true;
} void dfs(int pos)
{
if(pos==m)
{
ans++;
return ;
}
int u=x[pos],v=y[pos];
if(on[u]<in[u]/2&&on[v]<in[v]/2) //这个边为online 边
{
on[u]++;
on[v]++;
dfs(pos+1);
on[u]--;
on[v]--;
} if(off[u]<in[u]/2&&off[v]<in[v]/2) //这个边为off边
{
off[u]++;
off[v]++;
dfs(pos+1);
off[u]--;
off[v]--;
}
}
int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(in,0,sizeof(in));
memset(on,0,sizeof(on));
memset(off,0,sizeof(off)); for(i=0;i<m;i++)
{
scanf("%d%d",&x[i],&y[i]);
in[x[i]]++;
in[y[i]]++;
}
ans=0;
if(!judge())
{
printf("0\n");
continue;
}
dfs(0);
printf("%d\n",ans);
}
return 0;
}
/*
我的代码
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map> #define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1) #define bug printf("hihi\n") #define eps 1e-8
typedef __int64 ll; using namespace std; #define INF 0x3f3f3f3f #define N 1<<8 __int64 ans; int on[N],down[N];
int n,m,f[N],in[N]; inline int get(int x)
{
int s=0;
while(x)
{
s++;
x&=(x-1);
}
return s;
} inline bool judge(int pos,int cur)
{
int i;
for(i=0;i<n;i++)
if(on[i]&(1<<pos)&&!(cur&(1<<i))) return false; for(i=0;i<n;i++)
if(down[i]&(1<<pos)&&(cur&(1<<i))) return false;
return true;
} void dfs(int pos)
{
if(pos==n)
{
ans++;
return ;
}
int i,len=1<<n;
for(i=0;i<len;i++)
{
if((f[pos]&i)!=i) continue;
int tt=get(i);
if(tt!=in[pos]/2) continue;
if(!judge(pos,i)) continue;
on[pos]=i;
down[pos]=f[pos]^i;
dfs(pos+1);
on[pos]=0;
down[pos]=0;
}
} int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
int u,v;
memset(f,0,sizeof(f));
memset(on,0,sizeof(on));
memset(down,0,sizeof(down));
memset(in,0,sizeof(in));
bool flag=false;
if(m&1) flag=true;
while(m--)
{
scanf("%d%d",&u,&v);
u--;v--;
f[u]|=1<<v;
f[v]|=1<<u;
in[u]++;
in[v]++;
} for(i=0;i<n;i++)
{
if(in[i]&1) flag=true;
}
if(flag)
{
printf("0\n");
continue;
}
ans=0;
int len=1<<n;
for(i=0;i<len;i++)
{
if((f[0]&i)!=i) continue;
int tt=get(i);
if(tt!=in[0]/2) continue;
on[0]=i;
down[0]=f[0]^i;
dfs(1);
on[0]=0;
down[0]=0;
}
printf("%I64d\n",ans);
}
return 0;
}
HDU 5305 Friends(dfs)的更多相关文章
- HDU 5965 扫雷(dfs)题解
题意:给你一个3*n的格子,中间那行表明的是周围8格(当然左右都没有)的炸弹数量,上下两行都可以放炸弹,问你有几种可能,对mod取模 思路:显然(不),当i - 1和i - 2确定时,那么i的个数一定 ...
- HDU 1518 Square(DFS)
Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end ...
- HDU 1015 Safecracker (DFS)
题意:给一个数字n(n<=12000000)和一个字符串s(s<=17),字符串的全是有大写字母组成,字母的大小按照字母表的顺序,比如(A=1,B=2,......Z=26),从该字符串中 ...
- Hdu 1175 连连看(DFS)
Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1175 因为题目只问能不能搜到,没问最少要几个弯才能搜到,所以我采取了DFS. 因为与Hdu ...
- hdu 2821 Pusher (dfs)
把这个写出来是不是就意味着把 http://www.hacker.org/push 这个游戏打爆了? ~啊哈哈哈 其实只要找到一个就可以退出了 所以效率也不算很低的 可以直接DFS呀呀呀呀 ...
- hdu 2821 Pusher(dfs)
Problem Description PusherBoy is an online game http://www.hacker.org/push . There is an R * C grid, ...
- HDU 1501 Zipper(DFS)
Problem Description Given three strings, you are to determine whether the third string can be formed ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- HDU 5934 Bomb(炸弹)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
随机推荐
- mvc使用linq to sql进行sum统计遇到查询为null的问题
mvc linq to sql,linq to entity,sum,null 昨天写了段sum的统计语句, decimal sums sums = ( from fac in db.Apply wh ...
- 联想 S5 Pro(L78041)免解锁BL 免rec 保留数据 ROOT Magisk Xposed 救砖 ZUI 5.0.123
>>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...
- mysql复制过滤参数说明
参考文档: http://www.ywnds.com/?p=6945 https://stackoverflow.com/questions/23191160/whats-the-difference ...
- Farseer.net轻量级开源框架 入门篇:分类逻辑层
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 缓存逻辑层 下一篇:Farseer.net轻量级开源框架 入门篇: 添加数据详解 ...
- js正则表达式限制文本框只能输入数字,小数点,英文字母
1.文本框只能输入数字代码(小数点也不能输入)<input onkeyup="this.value=this.value.replace(/\D/g,'')" onafter ...
- 编写Java脚本统计工程代码总行数
在新公司工作将近一年了,一直独自一人负责服务端集群的运维和代码的编写.不知不觉从一个Project发展到了七八个Project. 看着越来越多的代码,今天突然想统计一下一共写了多少代码.[这里只统计完 ...
- 如何让一个div里面的div垂直居中?
如何让一个div里面的div垂直居中? 如何让上面灰色有文字那个div和背景图标垂直居中,不管屏幕大小有好大,始终在垂直方向上的中间.上面有整个布局和样式表,谢谢高手指点 CSS3时代当然要用CSS3 ...
- Vue指令2:v-bind
v-bind 指令可以更新 HTML 属性: <a v-bind:href="url">...</a> 在这里 href 是参数,告知 v-bind 指令将 ...
- 少啰嗦!一分钟带你读懂Java的NIO和经典IO的区别
1.引言 很多初涉网络编程的程序员,在研究Java NIO(即异步IO)和经典IO(也就是常说的阻塞式IO)的API时,很快就会发现一个问题:我什么时候应该使用经典IO,什么时候应该使用NIO? 在本 ...
- CAD动态绘制样条线(网页版)
在CAD设计时,需要绘制样条线,用户可以设置样条线线重及颜色等属性. 主要用到函数说明: _DMxDrawX::SendStringToExecuteFun 把命令当着函数执行,可以传参数.详细说明如 ...