Friends

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2329    Accepted Submission(s): 1150

 

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

Author

XJZX

Source

2015 Multi-University Training Contest 2

Recommend

wange2014   |   We have carefully
selected several similar problems for you:

 

题目大意:

给出遮个点遭条边逨n

8,每个边可能是黑色也可能是白色,求每个点相连的边黑边和白边的数量都相等的图有几种。

思路:

这个题大家都是用搜索逫剪枝做的,但是知道了这个题的遤遰做法以后真的是让人感叹啊!

首先,若某个点的度是奇数,就直接不可能了,然后再考虑遤遰的状态!

我们按照边的顺序一条一条染色(默认所有边原来是白色的),遦遛適遝遛達遝表示染完(染黑或者染白)

第適条边,状态为達的时候的方法数。

達由遮个两位的二进制数组成,每两位表示一个点所连的黑边的数目,转移什么的都是显而易见的

事情,不再多说!

思考:

之所以把这个本来是搜索的题的状压遤遰的做法放在这里,主要是因为其体现了状压只是一个手段

这一点,而且从这个题来看转移的手段是非常灵活的,比如在插头遤遰里常见的四进制,和这个题里面

的两位二进制,其实有异曲同工之处!

当然,这种情况下,对位运算的灵活运用就是蛮重要的了,其实呢,个人觉得只要心中把这个数

当成是二进制数想清楚该怎样使用位运算还是比较容易的!

 

#include<cstdio>
#include<cstring>
using namespace std;
const int N=;
int f[N][<<];
int n,m,T,inc[N];
struct edge{int x,y;}e[N<<];
int main(){
// freopen("a.in","r",stdin);
for(scanf("%d",&T);T;T--){
scanf("%d%d",&n,&m);
memset(inc,,sizeof inc);
for(int i=;i<=m;i++){
scanf("%d%d",&e[i].x,&e[i].y);e[i].x--,e[i].y--;
inc[e[i].x]++;
inc[e[i].y]++;
}
bool b=;
for(int i=;i<n;i++) if(inc[i]&){b=;puts("");break;}
if(b) continue;
memset(f,,sizeof f);
f[][]=;
int ALL=<<(n<<);
for(int i=;i<=m;i++){
for(int S=;S<ALL;S++){
f[i][S]+=f[i-][S];
int u=e[i].x;
int v=e[i].y;
int tu=(S>>(u<<))&;
int tv=(S>>(v<<))&;
if(tu+>(inc[u]<<)) continue;
if(tv+>(inc[v]<<)) continue;
int su=tu+;
int sv=tv+;
int S0=S^(tu<<(u<<))^(tv<<(v<<));
S0=S0|(su<<(u<<))^(sv<<(v<<));
f[i][S0]+=f[i-][S];
}
}
int S=;
for(int i=n-;~i;i--) S<<=,S|=inc[i]>>;
printf("%d\n",f[m][S]);
}
return ;
}

hdu5305 Friends[状压dp]的更多相关文章

  1. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  2. nefu1109 游戏争霸赛(状压dp)

    题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...

  3. poj3311 TSP经典状压dp(Traveling Saleman Problem)

    题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...

  4. [NOIP2016]愤怒的小鸟 D2 T3 状压DP

    [NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...

  5. 【BZOJ2073】[POI2004]PRZ 状压DP

    [BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...

  6. bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)

    数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...

  7. HDU 1074 Doing Homework (状压dp)

    题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...

  8. 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP

    [BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...

  9. 【BZOJ1725】[Usaco2006 Nov]Corn Fields牧场的安排 状压DP

    [BZOJ1725][Usaco2006 Nov]Corn Fields牧场的安排 Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M< ...

随机推荐

  1. 碰到故障大全---cd

    office已安装32位,无法安装64位?解决方案:开始→运行→输入regedit,打开注册表编辑器,找到HKEY_CLASSES_ROOT\\Installer\\Products\ \000021 ...

  2. MDL---Material Design Lite框架推荐

    INTRO material design相比不会陌生, 现在的移动端基本遵循了这个设计规范, 微软退出过一个残次品universal design(花了半个月时间赶出来的规范)也是借鉴了MD的思想, ...

  3. 05 Oracle process

    本章提要----------------------------------------------所有的 process 都是在 PGA 内(memory)server process: 与 cli ...

  4. MapReduce生成HFile入库到HBase

    转自:http://www.cnblogs.com/shitouer/archive/2013/02/20/hbase-hfile-bulk-load.html 一.这种方式有很多的优点: 1. 如果 ...

  5. try catch 异常处理

    1.捕获指定异常 2.捕获所有异常(catch(...))

  6. MFC中的UpdateData()

    UpdateData()是MFC的窗口函数,用来刷新数据的,参数只有一个,默认为TRUE 简单的说: UpdateData(TRUE) == 将控件的值赋值给成员变量, UpdateData(FALS ...

  7. windows 下安装perl Tk 模块

    首先,安装activeperl ,安装过程中勾选自动添加PATH环境变量,这样安装后就不需要自己手动修改PATH环境变量: 通过cmd 调出命令行窗口,输入ppm ,然后回车,就开启了perl 的包管 ...

  8. c++ double float 数值比较

    浮点数在内存中的存储机制和整型数不同,其有舍入误差,在计算机中用近似表示任意某个实数.具体的说,这个实数由一个整数或定点数(即尾数)乘以某个基数(计算机中通常是2)的整数次幂得到,这种表示方法类似于基 ...

  9. 动态生成的DOM不会触发onclick事件的原因及解决方法

    最近朋友在做一个项目的时候,遇到动态加载微博内容,然后点击“展开评论”后获取该微博的所有评论.这里使用了动态加载的<span mid='123456789′ class='get_comment ...

  10. SVN目录权限配置

    1.如果要使用SVN,需要有一个项目的保存目录,例如把该目录设为“C:\MyPro”文件夹 2.把该目录发布为SVN项目目录,则需要通过以下命令行 svnadmin create c:\mypro  ...