现在的位置: 首页 > 综合 > 正文

hdu3729I’m Telling the Truth (最大二分匹配,输出匹配的顶点)经典

2018年02月22日 ⁄ 综合 ⁄ 共 2281字 ⁄ 字号 评论关闭

I'm Telling the Truth

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1335 Accepted Submission(s): 686

Problem Description
After this year’s college-entrance exam, the teacher did a survey in his class on students’ score. There are 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 was between 5004th and 5005th, Student2 said he was between 5005th and 5006th, Student3 said he was between 5004th and 5006th, Student4
said he was between 5004th and 5006th, too. This situation is obviously impossible. So at least one told a lie. Because the teacher thinks most of his students are honest, he wants to know how many students told the truth at most.

Input
There is an integer in the first line, represents the number of cases (at most 100 cases). In the first line of every case, an integer n (n <= 60) represents the number of students. In the next n lines of every case, there are 2 numbers
in each line, Xi and Yi (1 <= Xi <= Yi <= 100000), means the i-th student’s rank is between Xi and Yi, inclusive.

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

Sample Input
2 4 5004 5005 5005 5006 5004 5006 5004 5006 7 4 5 2 3 1 2 2 2 4 4 2 3 3 4

Sample Output
3 2 3 4 5 1 3 5 6 7
题意:有n个学生,生个学生的总分成绩都不一样,现在老师询问这n个学生的总分,每个学生说出总分所在的一个范围,现在问最多可能有多少人说了真话,并把编号按字典序最大的输出。
分析:每个学生的总分不一样,也就是每个学生对应的总分是一对一的关系,如果用二分匹配去找一条增广路,找得到则认为该同学是说真话。(一般求最大最小值主要的算法:DP,二分匹配,贪心等)
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define N 100005
int x[65],y[65],vist[N],match[N],n;
int find(int i)
{
    for(int j=x[i];j<=y[i];j++)
    if(vist[j]==0)
    {
        vist[j]=1;
        if(match[j]==0||find(match[j]))
        {
            match[j]=i; return 1;
        }
    }
    return 0;
}
int main()
{
    int t,ans,s[65];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d%d",&x[i],&y[i]);

        memset(match,0,sizeof(match));
        ans=0;
        for(int i=n;i>0;i--)
        {
            memset(vist,0,sizeof(vist));
            if(find(i))
            s[ans++]=i;
        }
        printf("%d\n",ans);
        if(ans)printf("%d",s[ans-1]);
        for(int i=ans-2;i>=0;i--)
        printf(" %d",s[i]);
        printf("\n");
    }
}

抱歉!评论已关闭.