MDOLLS  Nested Dolls
English  Vietnamese 
Dilworth is the world's most prominent collector of Russian nested dolls: he literally has thousands of them! You know, the wooden hollow dolls of different sizes of which the smallest doll is contained in the second smallest, and this doll is in turn contained in the next one and so forth. One day he wonders if there is another way of nesting them so he will end up with fewer nested dolls? After all, that would make his collection even more magnificent! He unpacks each nested doll and measures the width and height of each contained doll. A doll with width w1 and height h1 will fit in another doll of width w2 and height h= if and only if w1 < w2 and h1 < h2. Can you help him calculate the smallest number of nested dolls possible to assemble from his massive list of measurements?
Input
On the first line of input is a single positive integer 1 ≤ t ≤ 20 specifying the number of test cases to follow. Each test case begins with a positive integer 1 ≤ m ≤ 20000 on a line of itself telling the number of dolls in the test case. Next follow 2m positive integers w1, h1,w2, h2, ... ,wm, hm, where wi is the width and hi is the height of doll number i. 1 ≤ wi, hi ≤ 10000 for all i. SAMPLE INPUT 4 3 20 30 40 50 30 40 4 20 30 10 10 30 20 40 50 3 10 30 20 20 30 10 4 10 10 20 30 40 50 39 51
Output
For each test case there should be one line of output containing the minimum number of nested dolls possible. SAMPLE OUTPUT 1 2 3 2
hide comments
egoista_:
20180521 07:48:48
Great Problem. Learnt Dilworth's Algorithm and implementd using largest nonincreasing subsequence. 

shashankpathak:
20180320 07:41:54
How to approach this problem using Dilsworth theorem??


biradarganesh:
20180102 06:28:49
Can someone please explain the problem? I've been trying for an hour to understand the problem, but I'm unable to even understand what they're asking us to do!


anmolx123:
20170724 22:10:03
How is the third Test case giving output 3?? 

amiri:
20170622 08:47:34
There is an beautiful O(n log n) solution using LIS Last edit: 20170622 14:15:00 

candide:
20170501 19:27:21
Solved in C and Python with binary search. Didn't try maximum bipartite matching. Last edit: 20170501 21:16:26 

César Giglio Badoino:
20160403 05:23:01
@naruto09 2 dolls 

naruto09:
20160116 09:03:44
i/o :


sughosh v kaushik:
20150815 14:03:09
greedy is good but with binary search this time :) Last edit: 20150815 14:20:02 

Andrei Dinu:
20141030 20:04:08
Can you please provide more tests? I made a solution with sorting and longest increasing subsequence, but I don't see where I am wrong. I get wrong answer starting with test no. 3. 
Added by:  ~!(*(@*!@^& 
Date:  20090224 
Time limit:  0.156s0.259s 
Source limit:  50000B 
Memory limit:  1536MB 
Cluster:  Cube (Intel G860) 
Languages:  All except: ERL JSRHINO NODEJS PERL6 VB.NET 
Resource:  Nordic 2007 