https://www.luogu.org/problem/P1345 题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流。这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,…,a©,且a1与a2相连,a2与a3相连,等等,那么电脑a1和a©就可以互发电邮。 很不幸,有时候奶牛会不小心踩到电脑上,农夫约翰的车也可能碾过电脑,这台倒霉的电脑就会坏掉。这意味着这台电脑不能再发送电邮了,于是与这台电脑相关的连接也就不可用了。 有两头奶牛就想:如果我们两个不能互发电邮,至少需要坏掉多少台电脑呢?请编写一个程序为她们计算这个最小值。 以如下网络为例: 1* / 3 - 2* 这张图画的是有2条连接的3台电脑。我们想要在电脑1和2之间传送信息。电脑1与3、2与3直接连通。如果电脑3坏了,电脑1与2便不能互发信息了。 输入格式 第一行 四个由空格分隔的整数:N,M,c1,c2.N是电脑总数(1<=N<=100),电脑由1到N编号。M是电脑之间连接的总数(1<=M<=600)。最后的两个整数c1和c2是上述两头奶牛使用的电脑编号。连接没有重复且均为双向的(即如果c1与c2相连,那么c2与c1也相连)。两台电脑之间至多有一条连接。电脑c1和c2不会直接相连。 第2到M+1行 接下来的M行中,每行包含两台直接相连的电脑的编号。 输出格式 一个整数表示使电脑c1和c2不能互相通信需要坏掉的电脑数目的最小值。
思路:思路转自洛谷某位 d a l a o dalao dalao
#include<bits/stdc++.h> #define INF 0x3f3f3f3f using namespace std; const int maxn=205; const int maxm=1e4+5; struct Edge { int to,nxt,f; }; Edge edge[maxm]; int head[maxn],cur[maxn]; int tot=1; int depth[maxn]; int n,m,s,t; inline void addedge(int u,int v,int dis) { edge[++tot].to=v,edge[tot].f=dis; edge[tot].nxt=head[u]; head[u]=tot; edge[++tot].to=u,edge[tot].f=0; edge[tot].nxt=head[v]; head[v]=tot; } bool bfs() { memcpy(cur,head,sizeof(cur)); memset(depth,0,sizeof(depth)); queue<int> q; depth[s]=1;//源点 q.push(s); int fir,to; while(!q.empty()) { fir=q.front(); q.pop(); for(int i=head[fir];i;i=edge[i].nxt) { to=edge[i].to; if(edge[i].f&&!depth[to]) { depth[to]=depth[fir]+1; q.push(to); } } } return depth[t]; } int dfs(int u,int lim)//当前节点 当前流量 { if(u==t)//汇点 return lim; int v,temp,ans=0; for(int i=cur[u];i;i=edge[i].nxt) { cur[u]=i; //当前弧优化 v=edge[i].to; if(depth[v]==depth[u]+1&&edge[i].f) { temp=dfs(v,min(lim,edge[i].f)); edge[i].f-=temp; edge[i^1].f+=temp; ans+=temp; lim-=temp; if(!lim) break; } } if(!ans) depth[u]=0; return ans; } int dinic() { int ans=0; while(bfs()) ans+=dfs(s,INF); return ans; } int main() { while(~scanf("%d %d %d %d",&n,&m,&s,&t)) { memset(head,0,sizeof(head)); tot=1; int u,v; for(int i=1;i<=n;i++) addedge(i,i+n,1); for(int i=0;i<m;i++) { //一个点拆成两个点 scanf("%d%d",&u,&v); //进入的边都连到u上 出去的边都从u+n出去 addedge(u+n,v,INF); addedge(v+n,u,INF); } s+=n; //记得修改源点 printf("%d\n",dinic()); } return 0; }