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. C++ 递归实现汉诺塔

    C++实现汉诺塔 #include <iostream> using namespace std; void move(int n,char x,char y,char z) { ) { ...

  2. libtommath.a: could not read symbols: Bad value 编译错误

    最近做个项目需要RSA,便调用了tommath,平时开发环境都在32位的系统上,编译运行一切都没问题,但当把程序换到一台64位系统上编译时出现: /usr/bin/ld: /usr/lib/gcc/x ...

  3. C++类的实例化对象的大小之sizeof()

    之所以写这篇<C++类的实例化对象的大小之sizeof()>.是由于在參加笔试的时候遇到例如以下这么一道题,当时感觉就是这个一个坑,但.我还是义无反顾的跳了下去,由于存在知识点盲区啊.现, ...

  4. 数据导入报错 Got a packet bigger than‘max_allowed_packet’bytes

    数据导入报错:Got a packet bigger than‘max_allowed_packet’bytes的问题 2个解决方法: 1.临时修改:mysql>set global max_a ...

  5. 在网页中使用SVG

    SVG可以作为一个独立的文件存在.但更多的时候,我们希望他能集成在某个更大的文档中,比如HTML.将SVG插入到HTML中主要有以下几种方式: 将 SVG 作为图像导入 将 SVG放入 iframe ...

  6. sparkR could not find function "textFile"

    Yeah, that’s probably because the head() you’re invoking there is defined for SparkR DataFrames[1] ( ...

  7. Ubuntu-Python2.7安装 scipy,numpy,matplotlib 和pip

    一. scipy,numpy,matplotlib sudo apt-get install python-scipy sudo apt-get install python-numpy sudo a ...

  8. 关于Cocos Studio制作游戏资源

    没想到,Cocos Studio居然是做游戏资源的,而且可以做骨骼动画,虽然我还不会做,只能自己一个人慢慢研究了.学长以前说,Coocs Studio只是用来打包项目成Apk的,没有什么卵用,刚开始我 ...

  9. 有2个表,结构相似,有一个字段关联,现在怎么把A表的数据添加到B表中,条件是A表不在B表的数据?? 请各位高手多多指点,是oracle的数据库

    : insert into b(col1,col2 )select col1,col2 where not exists(select a.col1 from a where a.col1 = b.c ...

  10. TextView不用获取焦点也能实现跑马灯

    1.写一个类继承TextView package com.example.tt; import android.content.Context; import android.graphics.Rec ...