首页 技术 正文
技术 2022年11月11日
0 收藏 589 点赞 4,614 浏览 1651 个字

题目链接: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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,905
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,430
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727