Nyoj 16 Rectangle nested DAG model--Nested rectangle problem

Source: Internet
Author: User
Tags min first row time limit
Nested Rectangle problem Description: there are n rectangles, each of which can be described by a A, a, long and wide. A rectangle x (A, b) can be nested in the rectangle y (c,d) when and only if A<c,b<d or b<c,a<d (equivalent to rotation X90 degrees). For example (1,5) can be nested within (6,2), but not nested in (3,4). Your task is to select as many rectangles as possible, so that each rectangle can be nested within the next rectangle, except for the last one.
Rujia said: The "nested" relationship between rectangles is a typical two-yuan relationship, and a two-dollar relationship can be modeled with graphs, which are nested as directed acyclic graphs, in other words, a DAG model, which is the longest path on a dag.

Rectangle NestingTime limit: Ms | Memory limit: 65535 KB Difficulty: 4 The description has n rectangles, each rectangle can be described by a A, a, length and width. A rectangle x (A, b) can be nested in the rectangle y (c,d) when and only if A<c,b<d or b<c,a<d (equivalent to rotation X90 degrees). For example (1,5) can be nested within (6,2), but not nested in (3,4). Your task is to select as many rectangles as possible, so that each rectangle can be nested within the next rectangle, except for the last one. The first line is a positive positive N (0&LT;N&LT;10), which indicates the number of test data groups,
The first row of each set of test data is a positive positive n, indicating the number of rectangles in the group's test data (n<=1000)
The following n rows, each row has two number A, a, a (0<a,b<100), indicating the length and width of the rectangle output each set of test data output a number, representing the maximum number of rectangles that meet the criteria, each set of output for a row sample input
1
1 2
2 4
5 8
6 ten
7 9
3 1
5 8
Ten
9
7 2 2
Sample output
5
SOURCE Classic Topic Uploader

Zhang Yunzun


Code:

#include <stdio.h>
#include <string.h>
int n,g[1010][1010],d[1010];
struct node
{
    int x, y;
} A[1010];

int ma (int q,int w)
{
    return q>w?q:w;
}
int la (int num)//Memory search
{
    if (d[num]>0)
        return d[num];
    int p=1;
    for (int i=0;i<n;i++)
        if (g[num][i]==1)
            P=ma (P,la (i) +1);

    d[num]=p;
    return p;
}
int main ()
{
    int t;
    scanf ("%d", &t);
    while (t--)
    {scanf ("%d", &n);
    memset (G,0,sizeof (g));
    Memset (d,0,sizeof (d));

    int i,j;
    for (i=0;i<n;i++)
    scanf ("%d%d", &a[i].x,&a[i].y);
    for (i=0;i<n;i++) for
        (j=0;j<n;j++)
        if (((a[j].x<a[i].x) && (a[j].y<a[i].y)) | | ((A[J].X<A[I].Y) && (a[j].y<a[i].x)))
          G[i][j]=1;
          int mma=0;

   for (i=0;i<n;i++)
   if (La (i) >mma)
    Mma=la (i);
   printf ("%d\n", MMA);
    }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.