hdu5305 Friends(dfs+map/hash)
题目: pid=5305">http://acm.hdu.edu.cn/showproblem.php?pid=5305
题意:给定N个人和M条朋友关系,是朋友关系的两个人之间有两种联系方式online和offline。使每一个人的online的数量和offline的数量相等,求方案数。
分析:因为M<=28,暴力枚举的话2^28非常大,会超时。能够考虑把全部的状态平分成两半,即枚举前面M/2条关系,暴力求出前面的2^(M/2)种状态,然后枚举后面M/2条关系,暴力求出后面2^(M/2)种状态,再枚举后面一半的状态,对于每一种状态直接在前面一半的状态里面找出满足条件的就可以。找状态用dfs比二进制枚举要快,查询的话map比hash方便。。。
代码:
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
int N,M,cnt[20],buf[20],f[20],ans,tp;
struct node
{
int a,b;
}e[200];
map <vector<int>,int> mp;
void dfs(int x,int y)
{
if(x>y)
{
if(tp==1)
mp[vector <int> (buf+1,buf+N+1)]++;
else
{
for(int i=1;i<=N;i++)
f[i]=(cnt[i]>>1)-buf[i];
if(mp.find(vector <int> (f+1,f+N+1))!=mp.end())
ans+=mp[vector <int> (f+1,f+N+1)];
}
return ;
}
buf[e[x].a]++;
buf[e[x].b]++;
if(buf[e[x].a]<=(cnt[e[x].a]>>1) && buf[e[x].b]<=(cnt[e[x].b]>>1))
dfs(x+1,y);
buf[e[x].a]--;
buf[e[x].b]--; dfs(x+1,y);
}
int main()
{
int ncase,i,j,z;
scanf("%d",&ncase);
while(ncase--)
{
scanf("%d%d",&N,&M);
mp.clear();
memset(cnt,0,sizeof(cnt));
ans=0;
for(i=1;i<=M;i++)
{
scanf("%d%d",&e[i].a,&e[i].b);
cnt[e[i].a]++;
cnt[e[i].b]++;
}
z=1;
for(i=1;i<=N;i++)
if(cnt[i]&1)
z=0;
if(!z)
{
printf("0\n");
continue ;
}
tp=1;
dfs(1,M/2);
tp=2;
dfs(M/2+1,M);
printf("%d\n",ans);
}
return 0;
}
另一种更简单的方法,直接dfs然后剪一下枝。假设一个人有x个朋友,那么暴力搜的过程中出现online的个数>x/2或者offline的个数>x/2就不满足条件也就不用搜了。
#include <iostream>
#include <cstdio>
using namespace std; int on[10],off[10],d[10],N,M,ans,x[30],y[30];
void dfs(int cur)
{
if(cur>M)
{
for(int i=1;i<=N;i++)
if(on[i]!=off[i])
return ;
++ans;
return ;
}
int u=x[cur],v=y[cur];
if(on[u]<d[u]/2 && on[v]<d[v]/2)
{
on[u]++;on[v]++;
dfs(cur+1);
on[u]--;on[v]--;
}
if(off[u]<d[u]/2 && off[v]<d[v]/2)
{
off[u]++;off[v]++;
dfs(cur+1);
off[u]--;off[v]--;
}
}
int main()
{
int ncase,i,j,k;
scanf("%d",&ncase);
while(ncase--)
{
scanf("%d%d",&N,&M);
memset(d,0,sizeof(d));
for(i=1;i<=M;i++)
{
scanf("%d%d",&x[i],&y[i]);
d[x[i]]++;
d[y[i]]++;
}
ans=0;
dfs(1);
printf("%d\n",ans);
}
return 0;
}
hdu5305 Friends(dfs+map/hash)的更多相关文章
- TTTTTTTTTTTT 百度之星D map+hash
Problem D Accepts: 2806 Submissions: 8458 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6 ...
- Codeforces Round #267 (Div. 2)D(DFS+单词hash+简单DP)
D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 【BZOJ-3578】GTY的人类基因组计划2 set + map + Hash 乱搞
3578: GTY的人类基因组计划2 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 367 Solved: 159[Submit][Status][ ...
- POJ 1840 Eqs 二分+map/hash
Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...
- Hash Map (Hash Table)
Reference: Wiki PrincetonAlgorithm What is Hash Table Hash table (hash map) is a data structure use ...
- Wannafly挑战赛29-A御坂美琴 (dfs+map)
链接:https://ac.nowcoder.com/acm/contest/271/A来源:牛客网 御坂美琴 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言 ...
- [JSOI2017]原力(分块+map(hash))
题目描述 一个原力网络可以看成是一个可能存在重边但没有自环的无向图.每条边有一种属性和一个权值.属性可能是R.G.B三种当中的一种,代表这条边上 原力的类型.权值是一个正整数,代表这条边上的原力强度. ...
- hdu5305 Friends(dfs,多校题)
Friends Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- bzoj 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列——map+hash+转换
Description N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色, 每头牛有多种特色,用二进制01表示它的特色ID.比如特色ID为13(1101), ...
随机推荐
- BZOJ4259: 残缺的字符串 & BZOJ4503: 两个串
[传送门:BZOJ4259&BZOJ4503] 简要题意: 给出两个字符串,第一个串长度为m,第二个串长度为n,字符串中如果有*字符,则代表当前位置可以匹配任何字符 求出第一个字符串在第二个字 ...
- What are the differences between WebAPI and WebAPI 2
http://stackoverflow.com/questions/21298961/what-are-the-differences-between-webapi-and-webapi-2 Maj ...
- FZOJ--2221-- RunningMan(水题)
Problem 2221 RunningMan Accept: 4 Submit: 10 Time Limit: 1000 mSec Memory Limit : 32768 KB Pro ...
- Boostrap零散
12 row 是核心控件 class="form-control" 弹窗口<input data-toggle="modal" data-target=& ...
- List<List<model>>如何更快捷的取里面的model?
访问接口返回数据类型为List<List<model>>,现在想将其中的model插入数据库,感觉一点点循环有点傻,0.0...,各位有没有其他的方法? List<Lis ...
- C# HashSet<T> 简单使用
一个简单的HashSet<T> 的例子,介绍其简单的方法,深入学习可参考微软:https://msdn.microsoft.com/en-us/library/bb359438(v=vs. ...
- C#中Request.Cookies 和 Response.Cookies 的区别分析
.NET中提供了读写Cookie的多种方法,Request.Cookies 是客户端通过 Cookie 标头形式由客户端传输到服务器的 Cookie:Response.Cookies 在服务器上创建并 ...
- python制造模块
制造模块: 方法一: 1.mkdir /xxcd /xx 2.文件包含: 模块名.py setup.py setup.py内容如下:#!/usr/bin/env pythonfrom distutil ...
- UI Framework-1: Aura Overview
Aura Overview From the perspective of the user, Aura provides Window and Event types, as well as a ...
- Database Exception – yii\db\Exception
在使用Yii2框架时遇到数据库无法访问的问题: 这个是由于 通常我们在参考 教程在 MAC OS LINUX下安装 MYSQL 时,默认将PHP.ini 中的以下三项留空导致的Yii2所需的PDO组建 ...