原题链接

二分图最大匹配板子。

每个奶牛向它愿意去的牛棚连边,跑二分图最大匹配即可。

这里我用的是匈牙利算法。

#include<cstdio>
#include<cstring>
using namespace std;
const int N = 410;
const int M = N * N;
int fi[N], ne[M], di[M], mtc[N], l;
bool v[N];
inline int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c < '0' || c > '9'; c = getchar())
p |= c == '-';
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
return p ? -x : x;
}
inline void add(int x, int y)
{
di[++l] = y;
ne[l] = fi[x];
fi[x] = l;
}
bool dfs(int x)
{
int i, y;
for (i = fi[x]; i; i = ne[i])
if (!v[y = di[i]])
{
v[y] = 1;
if (!mtc[y] || dfs(mtc[y]))
{
mtc[y] = x;
return true;
}
}
return false;
}
int main()
{
int i, j, n, m, x, s = 0;
n = re();
re();
for (i = 1; i <= n; i++)
{
m = re();
for (j = 1; j <= m; j++)
{
x = re();
add(i, x + n);
}
}
for (i = 1; i <= n; i++)
{
memset(v, 0, sizeof(v));
if (dfs(i))
s++;
}
printf("%d", s);
return 0;
}

洛谷1894 [USACO4.2]完美的牛栏The Perfect Stall的更多相关文章

  1. 洛谷 1894 [USACO4.2]完美的牛栏The Perfect Stall

    [题解] 其实是个二分图最大匹配的模板题,直接上匈牙利算法就好了. #include<cstdio> #include<algorithm> #define N 1010 #d ...

  2. 洛谷——P1894 [USACO4.2]完美的牛栏The Perfect Stall

    P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...

  3. 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall(二分图)

    P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...

  4. 洛谷 P1894 [USACO4.2]完美的牛栏The Perfect Stall

    P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...

  5. 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall

    题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在 ...

  6. 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall题解

    题目 二分图最大匹配问题 cow数组标现在牛栏里的牛是几号牛 每次寻找都要清空vis数组 如果可行有两种情况 1.这个牛栏里没有牛 2.这个牛栏里的牛可以到别的牛栏去 根据此递归即可 Code: #i ...

  7. Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配)

    Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配) Description 农夫约翰上个 ...

  8. [USACO4.2]完美的牛栏The Perfect Stall

    题目:USACO Training 4.2(在官网上提交需加文件输入输出).洛谷P1894. 题目大意:有n头奶牛m个牛栏,每头牛只会在自己喜欢的牛栏里产奶,问一次最多有多少奶牛能产奶. 解题思路:二 ...

  9. 题解 P1894 【[USACO4.2]完美的牛栏The Perfect Stall】

    题面 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...

随机推荐

  1. tf.identity 个人理解

    tf.identity is useful when you want to explicitly transport tensor between devices (like, from GPU t ...

  2. nbtscan工具

    这是一款用于扫描Windows网络上NetBIOS名字信息的程序.该程序对给出范围内的每一个地址发送NetBIOS状态查询,并且以易读的表格列出接收到的信息,对于每个响应的主机,NBTScan列出它的 ...

  3. HDU 6351 Naive Operations(线段树)

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=6315 Naive Operations Time Limit: 6000/3000 MS (Java/O ...

  4. ServiceWork的五种状态

    [ServiceWork的五种状态] installing.installed.activating.activated.redundant 参考:https://developer.mozilla. ...

  5. linus jsch上传文件

    package com.osplat.util; import java.io.*; import com.jcraft.jsch.*;import com.osplat.bean.Resultmod ...

  6. Vue之VueRouter

    Vue之VueRouter实现原理 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  7. Fraction to Recurring Decimal(STRING-TYPE CONVERTION)

    QUESTION Given two integers representing the numerator and denominator of a fraction, return the fra ...

  8. Intent Activity跳转 传递数据 Bundle

    1.普通跳转: Intent intent=new Intent(); intent.setClass(MainActivity.this,NewActivity.class); //新建一个Inte ...

  9. PTA 7-2 符号配对(栈模拟)

    请编写程序检查C语言源程序中下列符号是否配对:/*与*/.(与).[与].{与}. 输入格式: 输入为一个C语言源程序.当读到某一行中只有一个句点.和一个回车的时候,标志着输入结束.程序中需要检查配对 ...

  10. Delphi: 模态窗体最小化

    源起: 近期所介入的几个项目中,最后视频生成窗体,为一模态对话框.因生成时间可能较长,所以其窗体可以最小化,它最小化时同时最小化主程序,唤醒时主程序再复原. 代码亦是8年前本人所写,一直那样用了,也没 ...