I'm Telling the Truth HDU

来源:互联网 发布:抽奖软件注册机 编辑:程序博客网 时间:2024/05/16 07:25

题目描述:每位同学说了自己名次所在的范围,有人可能说谎了,求最多有多少人说的是实话。

思路:一开始没想明白,学弟说贪心试试,但这想法立马被我给否了,没有贪心策略符合所有情况。对桌的学弟看了题后一直大喊这是二分图最大匹配问题我做过啊,我一开始以为他在开玩笑没当回事。后来想了想,还真是一道很裸的二分匹配模板题。靠年轻人的思路写的,很惭愧啊。

代码如下:

#include<iostream>#include<cstdio>#include<vector>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<string>#include<iostream>using namespace std;int const maxn = 100000 + 66;vector<int> g[200];bool vis[maxn];int match[maxn];int n;bool dfs(int v){    vis[v] = true;    int len = g[v].size();    for(int i = 0; i < len; ++i){        int u = g[v][i], w = match[u];        if(w < 0 || !vis[w] && dfs(w)){            match[v] = u;            match[u] = v;            return true;        }    }    return false;}int main(){    int t;    scanf("%d", &t);    while(t--){        for(int i = 0; i < 200; ++i) g[i].clear();        int u, v;        scanf("%d", &n);        for(int i = 0; i < n; ++i){            scanf("%d%d", &u, &v);            for(int j = u; j <= v; ++j){                g[i].push_back(60 + j);            }        }        int cnt = 0;        memset(match, -1, sizeof match);        for(int i = n - 1; i >= 0; --i){            if(match[i] < 0){                memset(vis, 0, sizeof vis);                if(dfs(i))++cnt;            }        }        printf("%d\n", cnt);        bool ok = false;        for(int i = 0; i < n; ++i){            if(match[i] >= 0){                if(ok) printf(" ");                else ok = true;                printf("%d", i + 1);            }        }        printf("\n");    }    return 0;}


原创粉丝点击