HDOJ 3729 - I'm Telling the Truth 水二分图最大匹配

来源:互联网 发布:老司机 种子 知乎 编辑:程序博客网 时间:2024/06/07 10:45

                    题意:

                               每个学生说出其分数在哪个名次区间...问最多有多少个学生可能说真话...并且按字典序大的输出

                    题解: 

                               应该是2010年天津区域赛的签到题...将每个学生和其可能的分数都建边..注意..用邻接表来建....为了保证字典序最大..那么反过来.从最后一个学生开始往前面找...


Program:

#include<iostream>#include<stdio.h>#include<algorithm>#include<cmath>#include<stack>#include<string.h>#include<queue>#define ll long long#define esp 1e-5#define MAXN 65#define MAXM 50000000#define oo 100000007using namespace std; struct node{       int x,y,next;}line[MAXM];int Lnum,_next[MAXN],match[100005];bool used[100005],ok[MAXN];void addline(int x,int y){       line[++Lnum].next=_next[x],_next[x]=Lnum,line[Lnum].x=x,line[Lnum].y=y;}bool dfs(int x){       for (int k=_next[x];k;k=line[k].next)       {                int y=line[k].y;                if (used[y]) continue;                used[y]=true;                if (!match[y] || dfs(match[y]))                {                       match[y]=x;                       return true;                }       }       return false; }int getmax(int n){       int sum=0;       memset(match,0,sizeof(match));       for (int i=n;i>=1;i--)       {               memset(used,false,sizeof(used));               sum+=dfs(i);       }              return sum;  }int main()  {                 int cases,n,x,i;        scanf("%d",&cases);       while (cases--)       {               scanf("%d",&n);               Lnum=0,memset(_next,0,sizeof(_next));               for (x=1;x<=n;x++)               {                      int l,r;                      scanf("%d%d",&l,&r);                      for (i=l;i<=r;i++) addline(x,i);               }               printf("%d\n",getmax(n));               memset(ok,false,sizeof(ok));               for (i=1;i<=100000;i++) ok[match[i]]=true;               bool f=true;               for (i=1;i<=n;i++)                  if (ok[i])                   {                        if (f)  printf("%d",i);                           else printf(" %d",i);                        f=false;                   }               printf("\n");       }       return 0;  }