zoukankan      html  css  js  c++  java
  • I'm Telling the Truth(二分图最大匹配) HDU

    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.

    InputThere 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, X i and Y i (1 <= X i <= Y i <= 100000), means the i-th student’s rank is between X i and Y i, inclusive.

    OutputOutput 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
    题意:较好理解
    分析:二分图匈牙利匹配
     
    AC代码:
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<algorithm>
     5 using namespace std;
     6 const int maxn = 1e5+10;
     7 #define LL long long
     8 #define INF 0x3f3f3f3f
     9 int T,n;
    10 struct node{
    11     int x,y;
    12 }Node[70];
    13 int vis[maxn];
    14 int match[maxn],res[maxn];
    15 
    16 bool Found(int p){
    17     for(int i=Node[p].x;i<=Node[p].y;i++){
    18         if(!vis[i]){
    19             vis[i]=1;
    20             if(res[i]==0||Found(res[i])){
    21                 res[i]=p;
    22                 match[p]=i;
    23                 return 1;
    24             }
    25         }
    26     }
    27     return 0;
    28 }
    29 
    30 int main(){
    31     scanf("%d",&T);
    32     while(T--){
    33         memset(match,0,sizeof(match));
    34         memset(res,0,sizeof(res));
    35         scanf("%d",&n);
    36         for(int i=1;i<=n;i++) scanf("%d%d",&Node[i].x,&Node[i].y);
    37         int ans=0;
    38         for(int i=n;i>=1;i--){
    39             memset(vis,0,sizeof(vis));
    40             if(Found(i)) ans++;
    41         }
    42         printf("%d
    ",ans);
    43 
    44         for(int i=1;i<=n;i++){
    45             if(match[i]) {
    46                 ans--;
    47                 printf("%d%c",i,ans==0?'
    ':' ');
    48             }
    49         }
    50     }
    51 
    52 
    53 
    54     return 0;
    55 }
    56 /*
    57 2
    58 4
    59 5004 5005
    60 5005 5006
    61 5004 5006
    62 5004 5006
    63 7
    64 4 5
    65 2 3
    66 1 2
    67 2 2
    68 4 4
    69 2 3
    70 3 4
    71 */
     
    有些目标看似很遥远,但只要付出足够多的努力,这一切总有可能实现!
  • 相关阅读:
    mysql 添加自增长ID(序列方式)
    获取本地IP地址信息
    Intellij 快捷键
    java转换汉字为首字母搜索,
    gitee在linux下自动备份
    七牛云续费dns的ssl证书
    shiro 的realm的授权
    realme的shiro简单实现方法
    shiro初探,最简单的认证,通过ini文件。
    git config file
  • 原文地址:https://www.cnblogs.com/Bravewtz/p/11364857.html
Copyright © 2011-2022 走看看