hdu 3729 I'm Telling the Truth(二分图匹配)

来源:互联网 发布:中准会计师事务所 知乎 编辑:程序博客网 时间:2024/06/05 03:57

I'm Telling the Truth

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


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
245004 50055005 50065004 50065004 500674 52 31 22 24 42 33 4
 

Sample Output
32 3 451 3 5 6 7
 

Source
2010 Asia Tianjin Regional Contest
 

二分图匹配,一个名次对应一个人,名次为X集合,人为Y集合,求最大匹配。

#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>#include<vector>using namespace std;int n;const int MAXN = 100010;const int MAXM = 6000010;struct Edge {    int to,next;} edge[MAXM];int head[MAXN],tot;void init() {    tot = 0;    memset(head,-1,sizeof(head));}void addedge(int u,int v) {    edge[tot].to = v;    edge[tot].next = head[u];    head[u] = tot++;}int linker[MAXN];bool used[MAXN];int ans[66];int uN;bool dfs(int u) {    for(int i = head[u]; i != -1 ; i = edge[i].next) {        int v = edge[i].to;        if(!used[v]) {            used[v] = true;            if(linker[v] == -1 || dfs(linker[v])) {                linker[v] = u;                return true;            }        }    }    return false;}int hungary() {    int res = 0;    memset(linker,-1,sizeof(linker));    for(int u = uN-1; u>=0; u--) {        memset(used,false,sizeof(used));        if(dfs(u)) {            ans[res++]=u;        }    }    return res;}int main() {    //freopen("test.in","r",stdin);    int t;    cin>>t;    while(t--) {        scanf("%d",&uN);        init();        for(int i=0; i<uN; i++) {            int l,r;            scanf("%d%d",&l,&r);            for(int j=l; j<=r; j++) {                addedge(i,j);            }        }        int res=hungary();        printf("%d\n",res);        for(int i=res-1; i>=0; i--) {            printf("%d%c",ans[i]+1,i==0?'\n':' ');        }    }    return 0;}


0 0