[POJ1632] Vase collection
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
Northwestern Europe 2003
题意
\(n\)个瓶子,每个瓶子有形状颜色两种属性,且这两种属性都有\(36\)个类型,告诉你这些瓶子的两种属性的类型,求最大的k满足存在\(k*k\)个瓶子,使得这些瓶子的两种属性有\(k\)个类型,且覆盖了\(k*k\)个属性组合
题解
将每一个花瓶的两种属性建边,即形状与颜色建边,由于本题的数据较小,所以我们可以用到二进制来记录建边的状态。用\(vase[]\)来存放形状和颜色,然后将颜色状态压缩成为数组元素的值,进行\(Dfs\)。
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
const int N=38;
int n,Ans;
ll vase[N],two[N];
int Calc(ll nw)
{
int tot=0;
for(;nw;nw>>=1)
tot+=(nw&1);
return tot;
}
void Dfs(int k,int i,ll nw)
{
if(k>Ans) Ans=k;
ll YH;
for(;i<=36;++i)
{
YH=(nw&vase[i]);
if(Calc(YH)>k) Dfs(k+1,i+1,YH);//注意此处不得为Calc(YH)>=k,原因见下
}
}
int main()
{
int T,x,y;
two[0]=1ll; for(int i=1;i<N;++i) two[i]=two[i-1]<<1;
for (scanf("%d",&T);T;--T)
{
scanf("%d",&n);
for(int i=1;i<=36;++i) vase[i]=0ll;
for(int i=1;i<=n;++i)
scanf("%d%d",&x,&y),vase[x]|=two[y];//记录
Ans=0;
Dfs(0,1,two[37]-1),
printf("%d\n",Ans);
}
return 0;
}
下面是注意处的说明:
如上图,可见\(1\)与\(2\),\(4\)有边,\(3\)与\(2\)有边,则输出应该为\(1\),而若改成注释所说,则输出\(2\)。
本文作者:OItby @ https://www.cnblogs.com/hihocoder/
未经允许,请勿转载。