题目传送门: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. query纠错方法

    1. 第一种,在norvig介绍的方法中,详细的阐述了argmaxc P(c|w)的转换和求解办法. 这个概率不好直接算,但可以根据贝叶斯定理等价于argmaxc P(w|c)*P(c) / P(w) ...

  2. python 爬虫系列02-小说

    本爬虫为网络上的.. # # -*- coding:UTF-8 -*- # from bs4 import BeautifulSoup # import requests # if __name__ ...

  3. GitHub(hexo)博客页面访问量错误以及中文乱码解决

    如果访问量不显示(乱码形状),是因为不蒜子域名更新,所以你的域名也需要更新 <script async src="//busuanzi.ibruce.info/busuanzi/2.3 ...

  4. 案例46-crm练习客户登录

    1 login.jsp代码 <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  5. URAL —— 1255 & HDU 5100——Chessboard ——————【数学规律】

    用 k × 1 的矩形覆盖 n × n 的正方形棋盘 用 k × 1 的小矩形覆盖一个 n × n 的正方形棋盘,往往不能实现完全覆盖(比如,有时候 n × n 甚至根本就不是 k 的整倍数). 解题 ...

  6. hrb——开锁魔法I——————【规律】

    解题思路:从1到n的倒数之和. #include<stdio.h> #include<string.h> #include<algorithm> using nam ...

  7. c# xml API操作

    LoginInfo loginInfo = new LoginInfo(); xmlNode = _xml.SelectSingleNode(loginUrl); loginInfo.LoginUrl ...

  8. SQL Exists 的用法 转载

    比如在Northwind数据库中     有一个查询为 SELECT c.CustomerId, CompanyName FROM Customers c WHERE EXISTS( SELECT O ...

  9. C#博客记录二

    1.认识运算符 我认为其中 最重要的就是逻辑运算符,对于每个人来说学习web前端就是要有一个好的思维.能够更好的运用. 2.算数运算符 变量名++意味先输出,值后增加. ++变量名意味值先增加,后输出 ...

  10. maven module

    通过将一个maven项目拆分成多个module,会引入一定的项目复杂度,但随着后期项目代码的逐渐增多,最直观的感受是,每次build代码,不必build整个项目,可节省很多时间. 如果各个module ...