[bzoj1143][CTSC2008]祭祀

时间:2023-03-09 17:12:03
[bzoj1143][CTSC2008]祭祀

题意:给定一个n个点m条边的有向无环图,你要选出最多的点,并且满足任意两点之间都不存在通路。2)输出每个点选了它之后还是否有最优解。   n<=100 m<=1000

题解:每个点拆两个点,把每个点向它能走到点连边,然后最小割/二分图匹配。

这题想了好久,后来想出这个模型感觉没问题.....

第二个问貌似把每个点都强行不割跑一遍应该不会T吧.....

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#define S 0
#define T 201
#define INF 2000000000
using namespace std;
inline int read()
{
int x = , f = ; char ch = getchar();
while(ch < '' || ch > ''){ if(ch == '-') f = -; ch = getchar();}
while(ch >= '' && ch <= ''){x = x * + ch - '';ch = getchar();}
return x * f;
} int mark[T+];
int head[T+],cnt=,n,m,ans,cc=,thead[T+],q[T+],top,d[T+];
struct edge{
int to,next,w;
}e[T*T+];
struct tedge{
int to,next;
}e2[]; inline void ins(int f,int t){e2[++cc]=(tedge){t,thead[f]};thead[f]=cc;}
inline void ins(int f,int t,int w)
{
e[++cnt]=(edge){t,head[f],w};head[f]=cnt;
e[++cnt]=(edge){f,head[t],};head[t]=cnt;
} void build(int x,int from)
{
if(mark[x]!=from&&x!=from) ins(from,x+n,INF),mark[x]=from;
for(int i=thead[x];i;i=e2[i].next)
if(mark[e2[i].to]!=from)
build(e2[i].to,from);
} int dfs(int x,int f)
{
if(x==T)return f;
int used=;
for(int i=head[x];i;i=e[i].next)
if(e[i].w&&d[e[i].to]==d[x]+)
{
int w=dfs(e[i].to,min(f-used,e[i].w));
used+=w;e[i].w-=w;e[i^].w+=w;
if(used==f)return f;
}
return used;
} bool bfs()
{
memset(d,,sizeof(d));int i,j;
for(d[q[i=top=]=S]=;i<=top;++i)
for(int j=head[q[i]];j;j=e[j].next)
if(e[j].w&&!d[e[j].to])
d[q[++top]=e[j].to]=d[q[i]]+;
return d[T];
} int main()
{
ans=n=read();m=read();
for(int i=;i<=m;i++)
{
int x=read(),y=read();
ins(y,x);
}
for(int i=;i<=n;i++)build(i,i);
for(int i=;i<=n;i++)ins(S,i,),ins(i+n,T,);
while(bfs())ans-=dfs(S,INF);
cout<<ans;
return ;
}