Uvalive 5033 I ' m telling the Truth binary graph Max match (slightly modified)

Source: Internet
Author: User

I-i ' m telling the TruthTime limit:3000MS Memory Limit:0KB 64bit IO Format:%lld &%lluSubmit Status Practice uvalive 5033

Description

After this year's college-entrance exam, the teacher did a survey in his class on students ' score. There is n students in the class. The students didn ' t want to tell their teacher their exact score; They only told their teacher their rank in the province (in the form of intervals).

After asking all the students, the teacher found that some students didn ' t tell the truth. For example, Student1 said he is between 5004-th and 5005-th, Student2 said he was between 5005-th and 5006-th, Student3 Said he was between 5004-th and 5006-th, Student4 said he was between 5004-th and 5006-th, too. This situation is obviously impossible. So at least one told a lie. Because the teacher thinks most of his students is honest, he wants to know how many students told the truth at most.

Input

There is a integer in the first line, represents the number of cases (for most cases). In the first line of every case, an integer n(n) represents the number of students. In the next n lines of every case, there is 2 numbers in each line, Xi and Yi (1XiYi100000), means the i-th student ' s rank is between XI and c17>Yi, inclusive.

Output

Output 2 lines for every case. Output a single number on the first line, which means the number of students who told the truth at most. In the second line, the output of the students who tell the truth, separated by a space. Please note that there is no spaces at the head or tail of each line. If there is more than one, output the list with maximum lexicographic. (in the example above, 1 2 3;1 2 4;1 3 4;2 3 4 is all OK, and 2-3 4 with maximum lexicographic)

Sample Input

2 4 5004 50055005 50065004 50065004 50067 4 5 2 3 1 2 2 2 4 4 2 33 4

Sample Output

3 2 3 4 5 1 3 5 6 7

That's great!

I in Baidu search hdu the best match, Baidu gives the first result is this problem. At the beginning of this problem, with my two-figure matching very shallow understanding, I can not understand how the problem of the two-figure maximum matching.

Later I realized that for the first person, we knew his legal interval [A[I].X,A[I].Y]. First we match it with the first value in his legal interval, and if there is a subsequent need to match this value, we let the person and the value of the other legal interval match, if the match succeeds, the value of his previous match to the later people, if the match failed, continue to occupy the position.

The important point here is that for a value match, we abide by the principle of first served, for the first to have already matched the success of the person, if not found other qualifying position, this person occupies the position is absolutely cannot let out. This is critical, as the title also requires the output of the maximum number of dictionaries in all possibilities.

Feel finished this problem to the two-dimensional map of the maximum matching understanding a lot of deep, before just understand the template. And it's 1 a. Cool!




#include <stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespacestd;Const intMAXN =100005;Const intINF =1000000000;BOOLVIS[MAXN];//the points in the right collection of the query have not been accessedintLINK[MAXN];//Link[i] means that the I point in the right collection is connected by which point in the left collectionintg[ -][MAXN];//adjacency Matrixintx_cnt;inty_cnt;//The number of points around the collectioninta[ -];intTmin,tmax;BOOLFindintU//to find the augmented path .{     for(inti = tmin; I <= Tmax; i++)//iterate through each point in the right collection    {        if(!vis[i] && g[u][i])//Has not been accessed and is connected to the U-point{Vis[i]=true;//Mark this point            if(Link[i] = =-1||find (Link[i])) {                //the point is the end of the augmented path or the point where you can find an augmented path .Link[i] = u;//more new wide rouges even inverted                return true;//means to find an augmented path .            }        }    }    return false;//If you find all the points in the right collection and have not found the augmented path from that point, the point becomes non-existent augmented path}intsolve () {intnum =0; memset (Link,-1,sizeof(link));//initialized to-1 means that there is no link to any element in the left collection     for(intI =x_cnt; I >=1; i--)//iterating the left collection{memset (Vis,false,sizeof(VIS));//every time you need to clear the Mark        if(Find (i)) Num++;//find a way to widen the road and num++    }    returnnum;}intMain () {intT; scanf ("%d",&t);  while(t--){            intN; scanf ("%d",&N); Tmin=10000000, tmax=-1; intu,v; X_cnt=N; memset (G,0,sizeof(G));  for(intI=1; i<=n;i++) {scanf ("%d%d",&u,&v); if(u<tmin) Tmin=u; if(v>Tmax) {Tmax=v; }             for(intj=u;j<=v;j++) G[i][j]=1; }        intCnt=0; intans=solve (); printf ("%d\n", ans); Memset (A,0,sizeof(a));  for(inti=tmin;i<=tmax;i++){            if(link[i]!=-1) a[cnt++]=Link[i]; } sort (A,a+CNT);  for(intI=0; i<cnt;i++) {printf ("%d%c", a[i],i==cnt-1?'\ n':' '); }     }    return 0;}




Uvalive 5033 I ' m telling the Truth binary graph Max match (slightly modified)

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.