Vase collection
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 2243 |
|
Accepted: 864 |
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
我们用一个数组(comb[])存放形状和颜色,数组的下标为形状,然后将颜色状态压缩成为数组元素的值。这样一个数组元素就代表着,一个形状它对应了多少种颜色,而这个值也是这个形状对应的花瓶数。所以当与另一种形状的数组值,相与时,得到的结果如果大于等于2,那么也就代表着这两个形状至少有两种颜色是可以和他们任意匹配的,即找到了一个k=2,使得题目要求成立,而题目是求最大的k,所以我们记录当前算的k值为ans,两个数组值相与的值为cob,这样再与下一个形状的数组值相与,类似两个的计算,得到结果,更新ans即可。
AC:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
int ans;
LL state[200];
int check(LL st)
{
int cnt=0;
for(;st;st>>=1)
cnt+=(st&1);
return cnt;
}
void dfs(int k,int i,LL st)
{
if(k>=ans)
ans=k;
for(;i<=36;i++)
{
if(check(st&state[i])>=k+1)
dfs(k+1,i+1,st&state[i]);
}
}
int main()
{
int n,T;
for(scanf("%d",&T);T;T--)
{
scanf("%d",&n);
ans=0;
memset(state,0,sizeof(state));
for(int i=0,a,b;i<n;i++)
{
scanf("%d%d",&a,&b);
state[a]|=(1LL<<b);
}
dfs(0,1,(1LL<<36)-1);
printf("%d\n",ans);
}
return 0;
}