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. jquery方法操作iframe元素

    操作iframe父元素 $("#rolesCtl",parent.document).find( 'button' ).trigger( 'click' ); 在父页面获取ifra ...

  2. [从jQuery看JavaScript]-变量与作用域链

    jQuery片段: var // Will speed up references to window, and allows munging its name. window = this, //  ...

  3. msysgit使用方法

    安装好后运行"Git Bash",出现命令框. 输入 ssh-keygen -t rsa -C “your_email@youremail.com” 会提示SSH Public K ...

  4. 关于Cocos2d-x中类与类之间调用彼此方法的机制

    1.一般情况下都是把需要实例化的对象类的头文件包含到一个GameController.h中,再在GameController.cpp文件里面去使用各种对象类的方法来实现游戏的逻辑. 2.还有的时候会把 ...

  5. e662. 取的图像的色彩模型

    // This method returns the color model of an image public static ColorModel getColorModel(Image imag ...

  6. 【复杂】CentOS 6.4下PXE+Kickstart无人值守安装操作系统

    一.简介 1.1 什么是PXE PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持 ...

  7. Linux,ubuntu14.04.5下安装软件

    没有aptitude 使用:  sudo apt-get install  ***

  8. centos7 修改主机名的方法(在centos7有效)

    参考链接:http://www.centoscn.com/CentOS/config/2014/1031/4039.html 在CentOS或RHEL中,有三种定义的主机名:a.静态的(static) ...

  9. C语言中FILE是结构体,文件类型的指针

    c语言文件类型指针 我们在定义文件类型指针变量后,称作该指针指向该文件,但本质上,它不是指向一个存储文件信息的结构型变量么?那么我们在用各个函数对所谓的“文件指针”进行操作时,本质上是不是函数通过获取 ...

  10. 巧妙的利用Mongodb做地理空间查询