HDU - 3729 I‘m Telling the Truth(二分图最大匹配)

本文介绍了一道关于通过最大匹配算法解决学生名次区间真实性问题的编程题。该题涉及了将学生及其声称的名次区间映射为图论中的最大匹配问题,并使用匈牙利算法来找出最多可能有多少名学生说了实话。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击打开题目链接

I'm Telling the Truth

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

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

Source

Recommend

zhouzeyong

Statistic | Submit | Discuss | Note

题目大意:

n个人给出自己的名次区间,可真可假,求出最多有多少人说实话。输出字典序最大。

思路:

做这个题之前匈牙利算法接触的很少,所以拿到题后就开始一直贪心(TAT),结果就一直WA。

把每个人看作X集,区间看作Y集,将点与区间内的每一个数连边,求最大匹配数。从N->1扫描使得结果字典序最大。

附上AC代码:

#include<iostream>
#include<cstring>

using namespace std;
const int maxn=100000+5;
int vis[maxn],link[maxn],mark[maxn];
int T,n,sum,flag;

struct edges{
    int x,y;
}edge[maxn];

int dfs(int t){
    for(int i=edge[t].x;i<=edge[t].y;i++){
        if(mark[i]==0){
            mark[i]=1;
            if(link[i]==0||dfs(link[i])){
                link[i]=t;
                vis[t]=1;
                return 1;
            }
        }
    }
    return 0;
}

int main(){
    ios::sync_with_stdio(false);
    cin>>T;
    while(T--){
        sum=0,flag=0;
        memset(vis,0,sizeof(vis));
        memset(link,0,sizeof(link));
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>edge[i].x>>edge[i].y;
        for(int i=n;i>=1;i--){
            memset(mark,0,sizeof(mark));
            if(dfs(i)) sum++;
        }
        cout<<sum<<endl;
        for(int i=1;i<=n;i++){
            if(vis[i]&&flag==0){
                flag=1;
                cout<<i;
            }
            else if(vis[i]){
                cout<<' '<<i;
            }
        }
        cout<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chook_lxk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值