Section 7.3

master
大蒟蒻 9 years ago
parent d6f82448e1
commit 46f151a0d9

@ -0,0 +1,33 @@
#include <cstdio>
int adj[505], nxt[10005], to[10005], ecnt, mate[505], vis[505], idx, n, m;
inline void addEdge(int f, int t)
{
nxt[++ecnt] = adj[f], adj[f] = ecnt, to[ecnt] = t;
}
bool hungry(int u)
{
for (int e = adj[u]; e; e = nxt[e])
if (!mate[to[e]])
return mate[to[e]] = u, true;
for (int e = adj[u]; e; e = nxt[e])
if (vis[to[e]] != idx)
{
vis[to[e]] = idx;
if (hungry(mate[to[e]])) return mate[to[e]] = u, true;
}
return false;
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 0, x, y; i < m; i++)
scanf("%d%d", &x, &y), addEdge(x, y);
int ans = 0;
for (int i = 1; i <= n; i++)
{
idx++;
if (hungry(i)) ans++;
}
printf("%d", ans);
return 0;
}

@ -0,0 +1,33 @@
#include <cstdio>
int adj[505], nxt[10005], to[10005], ecnt, mate[505], vis[505], idx, n, m;
inline void addEdge(int f, int t)
{
nxt[++ecnt] = adj[f], adj[f] = ecnt, to[ecnt] = t;
}
bool hungry(int u)
{
for (int e = adj[u]; e; e = nxt[e])
if (!mate[to[e]])
return mate[to[e]] = u, true;
for (int e = adj[u]; e; e = nxt[e])
if (vis[to[e]] != idx)
{
vis[to[e]] = idx;
if (hungry(mate[to[e]])) return mate[to[e]] = u, true;
}
return false;
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 0, x, y; i < m; i++)
scanf("%d%d", &x, &y), addEdge(x, y);
int ans = 0;
for (int i = 1; i <= n; i++)
{
idx++;
if (hungry(i)) ans++;
}
printf("%d", ans);
return 0;
}

@ -523,4 +523,13 @@ Manacher 模板题
若确定了底面半径,则其他值都可以依次计算出来,同时根据上面几个有关圆锥的公式也可以看出,$V$是关于$r$的一个单峰函数,因此三分底面半径求解
\codeinput[\href{http://poj.org/problem?id=3737}{POJ3737} - UmBasketella]{assets/day7/poj3737.cpp}
\subsection{Hash} 人品算法没有什么意思附一个UOJ模数$998244353=7*17*2^{23}+1$
\subsection{二分图匹配}
给定一个二分图$G$,在$G$的一个子图$M$中,$M$的边集$E$中的任意两条边都不依附于同一个顶点,则称$M$是一个匹配。在二分图$G$中所有的匹配$M$中,边数最多的匹配,称为二分图的最大匹配。
\paragraph{求解二分图最大匹配}
\subparagraph{使用网络流算法} 实际上,可以将二分图最大匹配问题看成是最大流问题的一种特殊情况。首先:建立源点$s$和汇点$t$,从$s$$X$集合的所有顶点引一条边,容量为$1$,从$Y$集合的所有顶点向$t$引一条边,容量为$1$。然后将二分图的所有边看成是从$X_i$$Y_j$的一条有向边,容量为$1$。求最大匹配就是求$s$$t$的最大流。
\subparagraph{使用匈牙利算法:}
利用所有边的容量都是$1$以及二分图的性质,我们还可以将二分图最大匹配算法更简单地实现:\\
假设$M$是二分图$G$的一个匹配,则称$M$中边所依附的顶点是已匹配的顶点,若$P$是图$G$中一条连通两个未匹配顶点的路径,并且不属于$M$的边和属于$M$的边(即待匹配的边和已匹配边)在$P$上交替出现,则称$P$为相对于$M$的一条增广路径。如果我们将$P$的路径看着边的集合,我们令$M'=M \mathop{xor} P$,则$M'$是比$M$多一条边的匹配,即更大的匹配$M'$包含了,或者属于$M$,或者属于$P$,但不同时属于$M$$P$的边。当找不到增广路的时候,则这时候的$M$是最大匹配。
\paragraph{例题} 把光束当作图的顶点,小行星当作连接对应光束的边。
\codeinput[\href{http://poj.org/problem?id=3041}{POJ3041} - Asteroids]{assets/day7/poj3041.cpp}
\end{document}
Loading…
Cancel
Save