标签:content std sim href max oid detail tail res
给定一张左部$N$个点,右部$M$个点,$E$条边的二分图,求二分图最大匹配的非可行边的条数
#include <bits/stdc++.h>
using namespace std;
#define _for(i,a,b) for(int i = (a);i <= (b);++i)
typedef long long ll;
const int maxn = 1e5+5;
const int mod = 1e9+7;
struct EDGE
{
int st,to,res,next;
}edge[maxn*4];
int tot,head[maxn],d[maxn],s,t;
void addedge(int st,int to,int c)
{
edge[++tot].st=st;
edge[tot].res=c;
edge[tot].to=to;
edge[tot].next=head[st];
head[st]=tot;
}
bool bfs()
{
queue<int>q;
q.push(s);
memset(d,0,sizeof(d));
d[s]=1;
while(!q.empty()){
int now = q.front();
q.pop();
for(int i = head[now];i;i = edge[i].next){
int to = edge[i].to;
if(edge[i].res&&!d[to]){
d[to]=d[now]+1;
q.push(to);
if(to==t)return 1;
}
}
}
return 0;
}
int dinic(int now,int flow)
{
if(now==t)return flow;
int res = flow;
for(int i = head[now];i&&res;i = edge[i].next){
int to = edge[i].to;
if(d[to]==d[now]+1&&edge[i].res){
int k = dinic(to,min(edge[i].res,res));
if(!k)d[to]=0;
res-=k;
edge[i].res-=k;
edge[i^1].res+=k;
}
}
return flow-res;
}
vector<int>v[maxn];
int dfn[maxn],low[maxn],cnt,num,top,stk[maxn],instk[maxn],belong[maxn];
void tarjan(int now)
{
low[now]=dfn[now]=++num;
stk[++top]=now;
instk[now]=1;
for(int i = 0;i < v[now].size();++i){
int to = v[now][i];
if(!dfn[to]){
tarjan(to);
low[now]=min(low[now],low[to]);
}
else if(instk[to])low[now]=min(low[now],dfn[to]);
}
if(dfn[now]==low[now]){
int x = stk[top];
top--;
cnt++;
while(x!=now){
instk[x]=0;
belong[x]=cnt;
x=stk[top];
top--;
}
instk[now]=0;
belong[now]=cnt;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("simple.in","r",stdin);
freopen("simple.out","w",stdout);
#endif
int n,m,e;
scanf("%d%d%d",&n,&m,&e);
tot=1;
s=0,t=n+m+1;
for(int i = 1;i <= e;++i)
{
int a,b;
scanf("%d%d",&a,&b);
b+=n;
addedge(a,b,1);
addedge(b,a,0);
}
for(int i = 1;i <= n;++i)
{
addedge(s,i,1);
addedge(i,s,0);
}
for(int i = n+1;i <= n+m;++i)
{
addedge(i,t,1);
addedge(t,i,0);
}
int maxflow=0;
while(bfs())
{
int flow ;
while(flow=dinic(s,1000000))maxflow+=flow;
}
int ti = 0;
for(int i = 2;i <= tot;++i)
{
if(edge[i].res){
ti++;
v[edge[i].st].push_back(edge[i].to);
}
}
for(int i = s;i <= t;++i)
{
if(!dfn[i])tarjan(i);
}
vector<int>ans;
for(int i = 1;i <= e;++i)
{
if(edge[i*2].res&&belong[edge[i*2].st]!=belong[edge[i*2].to])ans.push_back(i);
}
int count = ans.size();
printf("%d\n",count);
for(int i = 0;i < count;++i){
if(i==count-1)printf("%d",ans[i]);
else printf("%d ",ans[i]);
}
printf("\n");
return 0;
}
标签:content std sim href max oid detail tail res
原文地址:https://www.cnblogs.com/aaddvvaanntteezz/p/12811710.html