luogu2764最小路径覆盖问题

1
2
3
4
n个点就是n条路径
s-1-x- [有边]*1 -y - 1 -t
每次将两个点连在一起就减少1条边,增加1点流量
所以最小路径数=n-最大流
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
inline LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return x*f;
}
const int N=308,M=6008;
int n,m,u,v,s,t,ans;
int nume,head[N];
struct node{
int to,nxt,f;
}e[M*2+N*4];
inline void addedge(int x,int y,int z)
{
e[++nume]=(node){y,head[x],z};head[x]=nume;
e[++nume]=(node){x,head[y],0};head[y]=nume;
}
int cur[N],dis[N];
int he,ta,q[N];
inline bool bfs()
{
memset(dis,0,sizeof(dis));
he=1;ta=2;
q[1]=s;
dis[s]=1;
while(he!=ta){
int x=q[he];
++he;
for(int i=head[x];i;i=e[i].nxt){
if(e[i].f&&!dis[e[i].to]){
dis[e[i].to]=dis[x]+1;
if(e[i].to==t) return 1;
q[ta]=e[i].to;
++ta;
}
}
}
return 0;
}
inline int dfs(int x,int low)
{
if(x==t||!low) return low;
int flow=0,tmp;
for(int &i=cur[x];i;i=e[i].nxt){
if(dis[e[i].to]==dis[x]+1&&(tmp=dfs(e[i].to,min(low,e[i].f)))){
flow+=tmp;
low-=tmp;
e[i].f-=tmp;
e[i^1].f+=tmp;//´ò´í+-
if(!low) return flow;
}
}
if(low) dis[x]=0;
return flow;
}
inline int dinic()
{
int maxflow=0;
while(bfs()){
for(int i=0;i<=t;++i){
cur[i]=head[i];
}
maxflow+=dfs(s,INT_MAX);
}
return maxflow;
}
int nxt[N],ind[N];
inline void print()
{
/*for(u=0;u<=t;++u){
cout<<"!!"<<u<<'\n';
for(int i=head[u];i;i=e[i].nxt){
cout<<e[i].to<<' '<<e[i].f<<'\n';
}
}*/
for(u=1;u<=n;++u){
for(int i=head[u];i;i=e[i].nxt){
if(i%2==0&&e[i].to>n&&e[i].f==0){
nxt[u]=e[i].to-n;
++ind[e[i].to-n];
}
}
}
for(u=1;u<=n;++u){
if(!ind[u]){
v=u;
while(v){
printf("%d ",v);
v=nxt[v];
}
puts("");
}
}
}
int main()
{
nume=1;
n=read();m=read();
s=0;t=n+n+1;
for(int i=1;i<=m;++i){
u=read();v=read();
addedge(u,n+v,1);
}
for(int i=1;i<=n;++i){
addedge(s,i,1);
addedge(n+i,t,1);
}
ans=dinic();
print();
printf("%d",n-ans);
return 0;
}