经典二分图匹配问题。把每个点拆成两个,对于原图中的每一条边(i,j)连接(i,j+n),最小路径覆盖就是点数n-二分图最大匹配。方案直接顺着匹配dsf。。

#include<iostream>
#include<cstdio>
using namespace std;
const int N=505,M=120005;
int n,m,h[N],cnt,lk[N],t,v[N],ans;
struct qwe
{
int ne,to;
}e[M];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
h[u]=cnt;
}
bool findd(int u)
{
for(int i=h[u];i;i=e[i].ne)
if(v[e[i].to]!=t)
{
v[e[i].to]=t;
if(!lk[e[i].to]||findd(lk[e[i].to]))
{
lk[e[i].to]=u;
lk[u]=e[i].to;
return 1;
}
}
return 0;
}
void prin(int u)
{
u+=n;
do
printf("%d ",u=u-n);
while(v[u]=t,u=lk[u]);
puts("");
}
int main()
{
n=read(),m=read();
for(int i=1;i<=m;i++)
{
int x=read(),y=read();
add(x,y+n);
}
for(int i=1;i<=n;i++)
if(!lk[i])
{
t++;
if(findd(i))
ans++;
}
t++;
for(int i=1;i<=n;i++)
if(v[i]!=t)
prin(i);
printf("%d\n",n-ans);
return 0;
}

洛谷 P2764 最小路径覆盖问题【匈牙利算法】的更多相关文章

  1. 洛谷 P2764 最小路径覆盖问题 解题报告

    P2764 最小路径覆盖问题 问题描述: 给定有向图\(G=(V,E)\).设\(P\) 是\(G\) 的一个简单路(顶点不相交)的集合.如果\(V\) 中每个顶点恰好在\(P\) 的一条路上,则称\ ...

  2. 洛谷 P2764 最小路径覆盖问题【最大流+拆点+路径输出】

    题目链接:https://www.luogu.org/problemnew/show/P2764 题目描述 «问题描述: 给定有向图G=(V,E).设P 是G 的一个简单路(顶点不相交)的集合.如果V ...

  3. 洛谷P2764 最小路径覆盖问题

    有向无环图的最小路径点覆盖 最小路径覆盖就是给定一张DAG,要求用尽量少的不相交的简单路径,覆盖有向无环图的所有顶点. 有定理:顶点数-路径数=被覆盖的边数. 要理解的话可以从两个方向: 假设DAG已 ...

  4. 【刷题】洛谷 P2764 最小路径覆盖问题

    题目描述 «问题描述: 给定有向图G=(V,E).设P 是G 的一个简单路(顶点不相交)的集合.如果V 中每个顶点恰好在P 的一条路上,则称P是G 的一个路径覆盖.P 中路径可以从V 的任何一个顶点开 ...

  5. 洛谷P2764 最小路径覆盖问题(最大流)

    传送门 先说做法:把原图拆成一个二分图,每一个点被拆成$A_i,B_i$,若原图中存在边$(u,v)$,则连边$(A_u,B_v)$,然后$S$对所有$A$连边,所有$B$对$T$连边,然后跑一个最大 ...

  6. 洛谷 P2764(最小路径覆盖=节点数-最大匹配)

    给定有向图G=(V,E).设P 是G 的一个简单路(顶点不相交)的集合.如果V 中每个顶点恰好在P 的一条路上,则称P是G 的一个路径覆盖.P 中路径可以从V 的任何一个顶点开始,长度也是任意的,特别 ...

  7. 洛谷P2764 最小路径覆盖问题(二分图)

    题意 给出一张有向无环图,求出用最少的路径覆盖整张图,要求路径在定点处不相交 输出方案 Sol 定理:路径覆盖 = 定点数 - 二分图最大匹配数 直接上匈牙利 输出方案的话就不断的从一个点跳匹配边 # ...

  8. 洛谷 [P2764]最小路径覆盖问题

    二分图应用模版 #include <iostream> #include <cstdio> #include <algorithm> #include <cs ...

  9. 洛谷-p2764(最小路径覆盖)(网络流24题)

    #include<iostream> #include<algorithm> #include<queue> #include<cstring> #in ...

随机推荐

  1. python学习之 - configparser模块

    configparser模块功能:用于生成和修改常见配置文件.基本常用方法如下: read(filename):直接读取配置文件write(filename):将修改后的配置文件写入文件中.defau ...

  2. @Aspect注解无效

    Pointcut的execution配置正确的话,检查下,是否加了以下jar包 <!-- http://mvnrepository.com/artifact/org.aspectj/aspect ...

  3. 扫描仪共享工具(BlindScanner Pro) 3.23 特别版

    http://www.xdowns.com/soft/1/126/2014/Soft_125206.html

  4. sklearn特征工程总结

    转自: http://www.cnblogs.com/jasonfreak/p/5448385.html https://www.zhihu.com/question/28641663/answer/ ...

  5. Codeforces div.2 B. The Child and Set

    题目例如以下: B. The Child and Set time limit per test 1 second memory limit per test 256 megabytes input ...

  6. Unity UGUI——概述、长处

    Unity4.6推出的新UI系统 长处:灵活.高速.可视化.效率高效果好.易于使用和扩展

  7. sqlserver中All、Any和Some用法与区别

    转自:http://blog.csdn.net/gyc1105/article/details/8063624 SQLServer中有三个关键字可以修改比较运算符:All.Any和Some,其中Som ...

  8. xcode7 怎样真机測试

    1. 下载xcode7 能够通过訪问 https://developer.apple.com/xcode/downloads/ 下载最新的xcode7的版本号 只是官网的下载速度太慢了,这个最好百度一 ...

  9. Linux文件系统与磁盘管理

    Linux文件系统与磁盘管理 有哪些文件系统: FAT:微软在Dos/Windows系列操作系统中共使用的一种文件系统的总称.       exFAT(Extended File Allocation ...

  10. js 二维码

    https://larsjung.de/jquery-qrcode/ 源码 <!DOCTYPE html> <html> <head> <title>j ...