POJ 1632 Vase collection【状态压缩+搜索】
题目传送门:http://poj.org/problem?id=1632
Vase collection
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 2308 | Accepted: 901 |
Description
For a collector, the obvious goal is to own a sample of each of the 1296 styles. Mr Cheng however,like so many other collectors, could never afford a complete collection, and instead concentrates on some shapes and some decorations. As symmetry between shape and decoration was one of the main aestheathical paradigms of the Feng dynasty, Mr Cheng wants to have a full collection of all combinations of k shapes and k decorations, for as large a k as possible. However, he has discovered that determining this k for a given collection is not always trivial. This means that his collection might actually be better than he thinks. Can you help him?
Input
Output
Sample Input
2
5
11 13
23 5
17 36
11 5
23 13
2
23 15
15 23
Sample Output
2
1
Source
题意概括:
N个瓶子,每个瓶子有两种属性 形状 和 颜色,我们要找到最大 K, 使得K*K个瓶子 都是由 K种 形状和颜色组成。
不是很好理解...
另一种理解可以把shape和decoration看成点,它们之际的对应关系看成边,这样就得到两个集合的映射。用A表示shape的集合,B表示decoration的集合。题目要求的就是原图的一个最大子图:使得该子图也可以分为A的子集A’和B的子集B’两部分,且从A’的每个点出发,到B’的任意点都存在边。
参考:https://blog.csdn.net/sj13051180/article/details/6612732
解题思路:
呃...我觉得这道题最大的难点在于理解题意了,看题目看到怀疑人生。
题意搞懂之后很容易想到状态压缩,之前搞状态压缩都在DP上搞,这次搬到搜索上有意思。
我们不妨设一个数组 Cp[ x ] = y; x(数组下标)表示shape, 数值 y 表示decoratio;y 最大可以到达 2^36, 所以数组定义个 long long 即可。 (有点像浓缩版的vector)
接下来就是暴力深搜去匹配了,两两匹配,如果匹配成功则两个合并后继续匹配(试试能否继续壮大),如果匹配失败则分道扬镳(各自寻找属于自己的那群小伙伴),不断匹配去寻找K的最大值。
AC code(116k 0ms):
//DFS 状态压缩
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long int
using namespace std; const int MAXN = ;
ll Cp[MAXN];
int N, ans; int cmp(ll num)
{
int cnt = ;
while(num)
{
cnt+=(num&);
num>>=;
}
return cnt;
}
void dfs(int k, int st, ll tp) ///花瓶数 当前花瓶编号 当前累积的颜色值
{
if(k > ans) ans = k;
for(int i = st; i <= ; i++)
{
if(cmp(Cp[i]&tp) > k)
dfs(k+, i+, (Cp[i]&tp));
}
}
int main()
{
int T, u, v;
scanf("%d", &T);
while(T--)
{
memset(Cp, , sizeof(Cp));
ans = ;
scanf("%d", &N);
for(int i = ; i < N; i++)
{
scanf("%d%d", &u, &v);
Cp[u]|=(1ll<<v);
}
dfs(, , (1ll>>)-);
printf("%d\n", ans);
}
return ;
}
POJ 1632 Vase collection【状态压缩+搜索】的更多相关文章
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 3691 (AC自动机+状态压缩DP)
题目链接: http://poj.org/problem?id=3691 题目大意:给定N个致病DNA片段以及一个最终DNA片段.问最终DNA片段最少修改多少个字符,使得不包含任一致病DNA. 解题 ...
- [POJ 2923] Relocation (动态规划 状态压缩)
题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开 ...
- POJ 2923 Relocation (状态压缩,01背包)
题意:有n个(n<=10)物品,两辆车,装载量为c1和c2,每次两辆车可以运一些物品,一起走.但每辆车物品的总重量不能超过该车的容量.问最少要几次运完. 思路:由于n较小,可以用状态压缩来求解. ...
- POJ 1321 棋盘问题(状态压缩DP)
不总结的话, 同一个地方会 WA 到死 思路: 状态压缩 DP. 1. s 表示压缩状态, 若第 i 列放了棋子, 那么该列置 1, 否则该列置 0. 假如 s = 3(0x011) 那么表示棋盘的第 ...
- POJ 3254 Corn Fields 状态压缩
这题对我真的非常难.实在做不出来,就去百度了,搜到了一种状压DP的方法.这是第一种 详细见凝视 #include <cstdio> #include <cstring> #in ...
- POJ 3254 Corn Fields 状态压缩DP (C++/Java)
id=3254">http://poj.org/problem? id=3254 题目大意: 一个农民有n行m列的地方,每一个格子用1代表能够种草地,而0不能够.放牛仅仅能在有草地的. ...
- Codeforces3C. Tic-tac-toe 题解 状态压缩+搜索
作者:zifeiy 标签:状态压缩.搜索 题目链接:https://codeforces.com/contest/3/problem/C 题目大意: 有一个 \(3 \times 3\) 的棋盘,给你 ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
随机推荐
- innosetup区分正常状态和静默安装状态(通过传递的参数)
命令行运行程序,如: myprogram.exe /abc /bcd 如果我们想获取其中的参数,“/abc”.“/bcd” 1. 直接使用innosetup自带的方法, GetCmdTail() ...
- 对C++ Local的经典分析(转)
对C++ Local的经典分析 本贴转载自:再别流年的技术实验室 文章地址: http://kittsoft.xp3.biz/?p=86 “这个问题比你想象中复杂”(我也学下BS的风格,虽然这句话是我 ...
- spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式
spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式[部分内容转载] 2018年03月27日 18:58:41 守望dfdfdf 阅读数:62更多 个人分类: 工 ...
- 【学习笔记】HTML基础:列表、表格与媒体元素
一.列表是信息资源的一种展现形式,它可以使信息结构化和条理化,并以列表的样式显示出来,以便浏览者能够快速的获取相应的信息. 1.无需列表 <ul> <li>第一项</li ...
- 3.storm-starter打包在storm集群上运行
1.使用maven或者其他打包工具将storm-starter打成jar包 2.请将jar包用解压工具打开在根目录下找到defaults.yaml文件并将其删除不然到时会报有multiply defa ...
- python unix时间戳
这是第一次用着python感到怒了,从datetime转化到timestamp数值居然没有直接的函数 直接获取当前时间戳倒是方便: import time timestamp = time.time( ...
- shiro 安全管理框架配置
step1 web.xml <!-- Shiro filter start --> <filter> <filter-name>shiroFilter</f ...
- SQLAlchemy的使用---外键ForeignKey数据库创建与连接
# 一对多建表操作 from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() from sql ...
- css background 背景知识详解
background 背景属性 我们知道元素有前景色color,与之对应的还有背景色,通过background我们可以为元素添加实色(background-color)和任意多个背景图片(backgr ...
- 触摸事件MotionEvent
触摸事件MotionEvent在用户交互中,占着非常重要的地位.首先,来看看MotionEvent中封装的一些常用的事件常量,它定义了触摸事件的不同类型. 1.单点触摸按下动作 public stat ...