题目链接:https://www.luogu.org/problemnew/show/P2764

把每个点在左边建一遍右边建一遍,再加上源点汇点,跑最大流,n-最大流就是答案。

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int inf = 1e9;
const int maxn = ;
int n, m, s, t, deep[maxn], maxflow, pre[maxn], st;
struct EDG{
int next, to, flow;
}edge[maxn];
int cnt = -, head[maxn], cur[maxn];
queue<int> q; void add(int u, int v, int w)
{
edge[++cnt].next = head[u];
edge[cnt].to = v;
edge[cnt].flow = w;
head[u] = cnt; edge[++cnt].next = head[v];
edge[cnt].to = u;
edge[cnt].flow = ;
head[v] = cnt;
} bool bfs(int s, int t)
{
for(int i = s; i <= t; i++)
{
cur[i] = head[i];
deep[i] = ;
}
deep[s] = ;
queue<int> q;
q.push(s);
while(!q.empty())
{
int now = q.front(); q.pop();
for(int i = head[now]; i != -; i = edge[i].next)
{
if(!deep[edge[i].to] && edge[i].flow)
{
q.push(edge[i].to);
deep[edge[i].to] = deep[now]+;
}
}
}
if(deep[t]) return true;
return false;
}
int dfs(int now, int t, int limit)
{
if(!limit || now == t) return limit;
int flow = , f;
for(int i = cur[now]; i != -; i = edge[i].next)
{
cur[now] = i;
if(deep[edge[i].to] == deep[now]+ && (f = dfs(edge[i].to, t, min(limit, edge[i].flow))))
{
flow += f;
limit -= f;
edge[i].flow -= f;
edge[i^].flow += f;
if(!limit) break;
}
}
return flow;
}
void Dinic(int s, int t)
{
while(bfs(s,t))
maxflow += dfs(s,t,inf);
}
void output(int x)
{
printf("%d ",x);
for(int i = head[x]; i != -; i = edge[i].next)
{
if(edge[i].flow == && edge[i].to - n > && edge[i].to - n <= n)
{
output(edge[i].to - n);
break;
}
}
return ;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i = ; i <= *n+; i++)
head[i] = -;
s = , t = *n+;
for(int i = ; i <= m; i++)
{
int u, v;
scanf("%d%d",&u,&v);
add(u,v+n,);
}
for(int i = ; i <= n; i++)
{
add(,i,);
add(i+n,*n+,);
}
Dinic(s,t);
for(int i=head[*n+];i!=-;i=edge[i].next)
{
if(edge[i^].flow==)
pre[++st]=edge[i].to;
}
for(int i=;i<=st;i++)
{
output(pre[i]-n);
printf("\n");
}
printf("%d",n - maxflow);
return ;
}

【luogu P2764 最小路径覆盖问题】 模板的更多相关文章

  1. Luogu P2764 最小路径覆盖问题(二分图匹配)

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

  2. luogu P2764 最小路径覆盖问题

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

  3. LUOGU P2764 最小路径覆盖问题 (最小路径点覆盖)

    解题思路 有向图最小路径点覆盖问题,有这样的结论就是有向图最小路径点覆盖等于n-拆点二分图中最大匹配.具体怎么证明不太知道..输出方案时找到所有左部未匹配的点一直走$match​$就行了. #incl ...

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

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

  5. Luogu 2764 最小路径覆盖问题 / Libre 6002 「网络流 24 题」最小路径覆盖 (网络流,最大流)

    Luogu 2764 最小路径覆盖问题 / Libre 6002 「网络流 24 题」最小路径覆盖 (网络流,最大流) Description 给定有向图G=(V,E).设P是G的一个简单路(顶点不相 ...

  6. P2764 最小路径覆盖问题 网络流重温

    P2764 最小路径覆盖问题 这个题目之前第一次做的时候感觉很难,现在好多了,主要是二分图定理不太记得了,二分图定理 知道这个之后就很好写了,首先我们对每一个点进行拆点,拆完点之后就是跑最大流,求出最 ...

  7. 【Luogu】P2764最小路径覆盖(拆点求最大匹配)

    题目链接 这个……学了一条定理 最小路径覆盖=原图总点数-对应二分图最大匹配数 这个对应二分图……是什么呢? 就是这样 这是原图 这是拆点之后对应的二分图. 然后咱们的目标就是从这张图上跑出个最大流来 ...

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

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

  9. P2764 最小路径覆盖问题

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

随机推荐

  1. BNU27932——Triangle——————【数学计算面积】

    Triangle Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class nam ...

  2. jqgrid 上移下移单元格

    在表格中常常需要调整表格中数据的显示顺序,我用的是jqgrid,实现原理就是将表中的行数保存到数据库中,取数据时按行进行排序 1.上移,下移按钮 <a href="javascript ...

  3. Object 公共方法详解

    在C#中,所有类型最终都从System.Object派生,所以每个类型的每个对象都保证了一组最基本的方法.具体地说,System.Object提供了一组公共实例方法. 一.Equals 如果两个对象具 ...

  4. dpkg: error: dpkg status database is locked by another process 解决方法

    使用dpkg -i/apt命令安装,报错: ------------------------------------------------------------- dpkg: error: dpk ...

  5. node.js服务器端下载、上传文件

    使用request 下载文件: 安装依赖: npm i requestsourceUrl下载源,targetUrl保存路径 async function downLoadFile(sourceUrl, ...

  6. js控制字符处理

    使用js在对json字符串转json对象时,如果遇到一些控制(特殊)字符会出现转化失败的情况 处理方法:通常我们可以把这些控制字符替换成空 function character(str) { retu ...

  7. C#程序执行时间

    Stopwatch类 using System.Diagnostics; static void Main(string[] args) { Stopwatch stopWatch = new Sto ...

  8. HTML标签 链接 随笔3

    4-1 <a>标签  网页链接 使用<a>标签可实现超链接,它在网页制作中可以说是无处不在,只要有链接的地方,就会有这个标签. 语法: <a href="目标网 ...

  9. Java将科学计数法数据转为字符串

    如果Excel单元格数据类型为数值,数字太长会变成科学计数法,Java读取的时候使用如下方法可将其转为字符串 BigDecimal bd = new BigDecimal("3.000085 ...

  10. Java设计模式—责任链模式

    责任链模式的定义: 使多个对象都有机会处理请求,从而避免了请求的发送者和接受者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有对象处理它为止. 责任链模式的重点是在"链& ...