zoukankan      html  css  js  c++  java
  • 题解 【重庆八中模拟赛】寻找代表元

    【重庆八中模拟赛】寻找代表元

    Description

    八中一共有n个社团,分别用1到n编号。
    八中一共有m个人,分别用1到m编号。每个人可以参加一个或多个社团,也可以不参加任何社团。
    每个社团都需要选一个代表。我们希望更多的人能够成为代表。这里,每个人至多代表一个社团且每个社团至多有一个代表。

    Input

    第一行输入两个数n和m。
    以下n行每行若干个数,这些数都是不超过m的正整数。其中第i行的数表示社团i的全部成员。每行用一个0结束。

    Output

    输出最多的能够成为代表的人数。

    Sample Input

    4 4 
    1 2 0 
    1 2 0 
    1 2 0 
    1 2 3 4 0

    Sample Output

    3

    Hint

    数据范围
    n,m<=200

    Source

    图论 ,二分图

     
     

    解析

    这就是一道二分图匹配的模板题啊(适合像本人一样的新手入门)

    所以直接写模板就行了。

    话不多说,直接上代码吧:

    #include<bits/stdc++.h>
    using namespace std;
     
    inline int read(){
        int sum=0,f=1;char ch=getchar();
        while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0' && ch<='9'){sum=sum*10+ch-'0';ch=getchar();}
        return f*sum;
    }
     
    struct road{
        int next,to;
    }e[100001];
    int n,m;
    int head[100001],cnt=0;
    int linker[100001],vis[100001];
     
    void add(int x,int y){
        e[++cnt].to=head[x];
        e[cnt].next=y;
        head[x]=cnt;
    }
     
    bool dfs(int x){
        for(int i=head[x];i;i=e[i].to){
            int k=e[i].next;
            if(vis[k]) continue;
            vis[k]=1;
            if(linker[k]==-1||dfs(linker[k])){
                linker[k]=x;
                return 1;
            }
        }
        return 0;
    }
     
    int hungarian(int m){
        int ans=0;
        for(int i=0;i<=m+n;i++) linker[i]=-1;
        for(int i=1;i<=m;i++){
            memset(vis,0,sizeof(vis));
            if(dfs(i)) ans++;
            //    cout<<"ok"<<endl;
        }
        return ans;
    }
     
    int main(){
        n=read();m=read();
        int x;
        for(int i=1;i<=n;i++){
            while(scanf("%d",&x)!=EOF&&x!=0)   
                          add(x,i+m),add(i+m,x);
            
        }
        printf("%d
    ",hungarian(m));
        return 0;
    }            
  • 相关阅读:
    CNN comprehension
    Gradient Descent
    Various Optimization Algorithms For Training Neural Network
    gerrit workflow
    jenkins job配置脚本化
    Jenkins pipeline jobs隐式传参
    make words counter for image with the help of paddlehub model
    make words counter for image with the help of paddlehub model
    git push and gerrit code review
    image similarity
  • 原文地址:https://www.cnblogs.com/zsq259/p/10503261.html
Copyright © 2011-2022 走看看