[USACO5.4]奶牛的电信Telecowmunication
思路:
水题;
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 205
#define maxm 20005
#define INF 0x3f3f3f3f
int head[maxn],cnt=,n,m,E[maxm],V[maxm],F[maxm],s,t;
int que[maxm<<],deep[maxn],c1,c2;
inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
}
inline void edge_add(int u,int v,int f)
{
E[++cnt]=head[u],V[cnt]=v,F[cnt]=f,head[u]=cnt;
E[++cnt]=head[v],V[cnt]=u,F[cnt]=,head[v]=cnt;
}
bool bfs()
{
memset(deep,-,sizeof(deep));
int h=,tail=,now;que[]=s,deep[s]=;
while(h<tail)
{
now=que[h++];
for(int i=head[now];i;i=E[i])
{
if(deep[V[i]]<&&F[i])
{
deep[V[i]]=deep[now]+;
if(V[i]==t) return true;
que[tail++]=V[i];
}
}
}
return false;
}
int flowing(int now,int flow)
{
if(t==now||flow<=) return flow;
int oldflow=,pos;
for(int i=head[now];i;i=E[i])
{
if(!F[i]||deep[V[i]]!=deep[now]+) continue;
pos=flowing(V[i],min(flow,F[i]));
flow-=pos,oldflow+=pos,F[i]-=pos,F[i^]+=pos;
if(!flow) return oldflow;
}
if(!oldflow) deep[now]=-;
return oldflow;
}
int main()
{
in(n),in(m),in(c1),in(c2),s=c1+n,t=c2;
for(int i=;i<=n;i++) edge_add(i,i+n,);
while(m--)
{
in(c1),in(c2);
edge_add(c1+n,c2,INF);
edge_add(c2+n,c1,INF);
}
int ans=;
while(bfs()) ans+=flowing(s,INF);
cout<<ans;
return ;
}