Vase Collection
Time limit1sMemory limit128 MB
Given up to 100 shape-decoration pairs drawn from a 36 by 36 grid, find the largest k for which some k shapes and k decorations form a complete k by k block of owned pairs.
- Level
Medium7 of 10
- Topics
- Brute force, Backtracking, Graph, Bit manipulation
- Solved
- No attempts yet
Problem
Mr. Cheng collects old Chinese porcelain, and in particular late-15th-century Feng dynasty vases. Vase-making of that era followed very strict artistic rules. There were exactly 36 shapes and 36 decoration patterns, giving distinct styles in all.
The obvious goal for a collector is to own one sample of every one of the 1296 styles. Like most collectors, however, Mr. Cheng can never afford a complete collection, so he concentrates on some shapes and some decorations. Because symmetry between shape and decoration was a central aesthetic principle of the Feng dynasty, Mr. Cheng wants the largest possible balanced sub-collection: for as large a as possible, he wants a set of shapes and decorations such that his collection contains all combined styles of those shapes and decorations.
Determining this for a given collection is not always easy, which means his collection might actually be better than he thinks. Given the vases he owns, help him find the largest such .
Input
The first line contains a single positive integer , the number of test scenarios that follow.
Each scenario begins with a line containing a single positive integer (), the number of vases in the collection. Then follow lines, one per vase, each containing two integers and separated by a single space, where () is the shape of the -th vase and () is its decoration.
Output
For each scenario, output one line containing the maximum such that there exist shapes and decorations for which the collection contains all combined styles.