题目链接: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. 15019:Only the instance admin may alter the PermSize attribute

    15019:Only the instance admin may alter the PermSize attribute TimesTen提示空间不足,增加空间重启后提示15019:Only th ...

  2. Servlet3.0的文件上传功能

    在Servlet3.0之前,文件上传需要借助于第三方插件,在Servlet3.0之后,Servlet本身开始支持文件上传功能. 获取上传的文件可以通过HTTPServletRequest的getPar ...

  3. CSS+DIV进度条

    <style type="text/css"> .Bar { position: relative; width: 200px; /* 宽度 */ border: 1p ...

  4. Base class for cloning an object in C#

    Base class for cloning an object in C# /// <summary> /// BaseObject class is an abstract class ...

  5. Spring @Resource, @Autowired and @Inject 注入

    Overview I’ve been asked several times to explain the difference between injecting Spring beans with ...

  6. Android StickHeaderRecyclerView - 让recyclerview头部固定

    介绍在项目中有时会需要recyclerview滑动式时某个view滑出后会固定在头部显示,比较常用的比如手机联系人界面.地区选择界面等. StickHeaderRecyclerView就是实现这个功能 ...

  7. 任务四:CSS定位和居中问题

    任务目标 实践HTML/CSS布局方式 深入了解position等CSS属性 任务描述 实现如 示例图(点击打开) 的效果 灰色元素水平垂直居中,有两个四分之一圆位于其左上角和右下角. 任务注意事项 ...

  8. Unity 多个Android sdk 插件如何组织目录

    一般Android 插件放在 Assets/Plugins/Android/ 下, 但是一个项目可能要用到多个sdk , 比如既要用 阿里九游的sdk 又要用 share sdk 怎么办呢, 难道要只 ...

  9. Siebel 集成中的“发布-订阅”与“阅读”

    将 Siebel 应用程序中存储的数据提供给企业中的其他应用程序时,通常需要遵循以下两种基本模式之一: 发布-订阅 阅读 “发布-订阅”是一种机制,根据该机制,一个系统(发布者)将更改或更新的数据提供 ...

  10. C# 生成CODE128条码

    using System; using System.Collections.Generic; using System.Data; using System.Drawing;    namespac ...