zoukankan      html  css  js  c++  java
  • 洛谷 1894 [USACO4.2]完美的牛栏The Perfect Stall

    【题解】

      其实是个二分图最大匹配的模板题,直接上匈牙利算法就好了。

      

     1 #include<cstdio>
     2 #include<algorithm>
     3 #define N 1010
     4 #define rg register
     5 using namespace std;
     6 int n,m,E,ans,T,tot,last[N],v[N],from[N];
     7 struct edge{
     8     int to,pre;
     9 }e[N*N];
    10 inline int read(){
    11     int k=0,f=1; char c=getchar();
    12     while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
    13     while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar();
    14     return k*f;
    15 }
    16 int dfs(int x){
    17     for(rg int i=last[x],to;i;i=e[i].pre) if(v[to=e[i].to]!=T){
    18         v[to]=T;
    19         if(!from[to]||dfs(from[to])){
    20             from[to]=x;
    21             return 1;
    22         }    
    23     }
    24     return 0;
    25 }
    26 int main(){
    27     n=read(); m=read();
    28     for(rg int i=1;i<=n;i++){
    29         int num=read();
    30         for(rg int j=1;j<=num;j++){
    31             int v=read();
    32             e[++tot]=(edge){v,last[i]};last[i]=tot;
    33         }
    34     }
    35     for(rg int i=1;i<=n;i++) ++T,ans+=dfs(i);
    36     printf("%d
    ",ans);
    37     return 0;
    38 } 
    View Code
  • 相关阅读:
    11.7表单事件 定时器
    11.5真11.6 函数调用 数组字符串的定义和方法
    11.2 面向对象 解析
    11.1 js数据类型 作用域 原型链
    10.31js中级作用域链和this
    定时器
    生出对象的方式
    学习this
    字符串
    全局方法
  • 原文地址:https://www.cnblogs.com/DriverLao/p/8909221.html
Copyright © 2011-2022 走看看