题目传送门:http://poj.org/problem?id=1632

Vase collection

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2308   Accepted: 901

Description

Mr Cheng is a collector of old Chinese porcelain, more specifically late 15th century Feng dynasty vases. The art of vase-making at this time followed very strict artistic rules. There was a limited number of accepted styles, each defined by its shape and decoration. More specifically, there were 36 vase shapes and 36 different patterns of decoration - in all 1296 different styles. 
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

On the first line of the input, there is a single positive integer n, telling the number of test scenarios to follow. Each test scenario begins with a line containing a single positive integer m <= 100, the number of vases in the collection. Then follow m lines, one per vase, each with a pair of numbers, si and di, separated by a single space, where si ( 0 < si <= 36 ) indicates the shape of Mr Cheng's i:th vase, and di ( 0 < di <= 36 ) indicates its decoration.

Output

For each test scenario, output one line containing the maximum k, such that there are k shapes and k decorations for which Mr Cheng's collection contains all k*k combined styles.

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【状态压缩+搜索】的更多相关文章

  1. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  2. POJ 3691 (AC自动机+状态压缩DP)

    题目链接:  http://poj.org/problem?id=3691 题目大意:给定N个致病DNA片段以及一个最终DNA片段.问最终DNA片段最少修改多少个字符,使得不包含任一致病DNA. 解题 ...

  3. [POJ 2923] Relocation (动态规划 状态压缩)

    题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开 ...

  4. POJ 2923 Relocation (状态压缩,01背包)

    题意:有n个(n<=10)物品,两辆车,装载量为c1和c2,每次两辆车可以运一些物品,一起走.但每辆车物品的总重量不能超过该车的容量.问最少要几次运完. 思路:由于n较小,可以用状态压缩来求解. ...

  5. POJ 1321 棋盘问题(状态压缩DP)

    不总结的话, 同一个地方会 WA 到死 思路: 状态压缩 DP. 1. s 表示压缩状态, 若第 i 列放了棋子, 那么该列置 1, 否则该列置 0. 假如 s = 3(0x011) 那么表示棋盘的第 ...

  6. POJ 3254 Corn Fields 状态压缩

    这题对我真的非常难.实在做不出来,就去百度了,搜到了一种状压DP的方法.这是第一种 详细见凝视 #include <cstdio> #include <cstring> #in ...

  7. POJ 3254 Corn Fields 状态压缩DP (C++/Java)

    id=3254">http://poj.org/problem? id=3254 题目大意: 一个农民有n行m列的地方,每一个格子用1代表能够种草地,而0不能够.放牛仅仅能在有草地的. ...

  8. Codeforces3C. Tic-tac-toe 题解 状态压缩+搜索

    作者:zifeiy 标签:状态压缩.搜索 题目链接:https://codeforces.com/contest/3/problem/C 题目大意: 有一个 \(3 \times 3\) 的棋盘,给你 ...

  9. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

随机推荐

  1. shell 函数与内置变量

    1,特殊shell变量 $# 传递到脚本的参数个数 $* 以一个单字符串显示所有向脚本传递的参数 $$ 脚本运行的当前进程ID号 $! 后台运行的最后一个进程的ID号 $@ 与$*相同,但是使用时加引 ...

  2. DP Intro - Tree DP Examples

    因为上次比赛sb地把一道树形dp当费用流做了,受了点刺激,用一天时间稍微搞一下树形DP,今后再好好搞一下) 基于背包原理的树形DP poj 1947 Rebuilding Roads 题意:给你一棵树 ...

  3. js模拟实现哈希表

    在算法中,尤其是有关数组的算法中,哈希表的使用可以很好的解决问题,所以这篇文章会记录一些有关js实现哈希表并给出解决实际问题的例子. 说明: 这篇博客所写并不是真正意义的哈希表,只是与哈希表的使用有相 ...

  4. 我的博客已经迁移到csdn

    博客已经迁移csdnhttp://blog.csdn.net/u013372900 博客园我很喜欢是源于他的可扩展性,可以自己去改,但遗憾的是博客园的速度似乎不是很给力.IT能有今天的 发展是源于无数 ...

  5. 简单的一个php验证登陆代码

    <?php/** */ if ( !isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || $_SERVER ...

  6. [译]理解 Windows UI 动画引擎

    本文译自 Nick Waggoner 的 "Understand what’s possible with the Windows UI Animation Engine",已获原 ...

  7. tomcat一个IP绑定多个域名,不同域名访问不同的应用

    修改conf文件夹下面的server.xml的Engine里面的内容即可原始内容如下: …… <Engine name="Catalina" defaultHost=&quo ...

  8. 高效的jQuery代码编写技巧

    缓存变量 DOM遍历是昂贵的,所以尽量将会重用的元素缓存. // 糟糕 h = $('#element').height(); $(); // 建议 $element = $('#element'); ...

  9. drupal对数据库操作

    // nodenode_load($nid = NULL, $vid = NULL, $reset = FALSE);node_load_multiple($nids = array(), $cond ...

  10. 关于li标签行内显示的问题

    在我们实现导航栏的时候,经常要用到ul标签. 通常,我们是通过给li标签设置display:inline-block来使其在一行里显示,但是这个时候出现了一个很头疼的问题. 我们先上代码. <! ...