标签:
题目链接:传送门
题目大意:给你一副无向图,求解图的顶点连通度
题目思路:模板(图论算法理论,实现及应用 P396)
Menger定理:无向图G的顶点连通度k(G)和顶点间最大独立轨数目之间存在如下关系:
1.若G是完全图,k(G)=|V(G)|-1
2.若G不是完全图,k(G)=min{P(A,B)} 其中A,B不直接相连
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <cstring> #include <stack> #include <cctype> #include <queue> #include <string> #include <vector> #include <set> #include <map> #include <climits> #define lson root<<1,l,mid #define rson root<<1|1,mid+1,r #define fi first #define se second #define ping(x,y) ((x-y)*(x-y)) #define mst(x,y) memset(x,y,sizeof(x)) #define mcp(x,y) memcpy(x,y,sizeof(y)) #define Min(x,y) (x<y?x:y) #define Max(x,y) (x>y?x:y) using namespace std; #define gamma 0.5772156649015328606065120 #define MOD 1000000007 #define inf 0x3f3f3f3f #define N 10005 #define maxn 1000050 typedef long long LL; typedef pair<int,int> PII; int n,m; int lel[305],head[305]; struct Node{ int to,next,v; Node(){} Node(int a,int b,int c):to(a),next(b),v(c){} }node[N];int hcnt; inline void init(){ hcnt=0; mst(head,-1); } int bfs(int s,int t){ int i; queue<int>q; mst(lel,-1); lel[s]=0; q.push(s); while(!q.empty()){ int x=q.front();q.pop(); if(x==t)return 1; for(i=head[x];~i;i=node[i].next){ int e=node[i].to; if(lel[e]==-1&&node[i].v){ lel[e]=lel[x]+1; q.push(e); } } } return 0; } inline void init_flow(){ for(int i=0;i<hcnt;i+=2){ node[i].v+=node[i^1].v; node[i^1].v=0; } } int dfs(int s,int t,int v){ if(s==t) return v; int flow=0; for(int i=head[s];~i;i=node[i].next){ int e=node[i].to,f=node[i].v; if(lel[e]==lel[s]+1&&f){ int al=Min(v-flow,f); al=dfs(e,t,al); node[i].v-=al; node[i^1].v+=al; flow+=al; if(flow==v)return flow; } } return flow; } int Dinic(int s,int t){ int res=0; while(bfs(s,t))res+=dfs(s,t,inf); return res; } inline void add(int x,int y,int v){ node[hcnt]=Node(y,head[x],v); head[x]=hcnt++; node[hcnt]=Node(x,head[y],0); head[y]=hcnt++; } int main(){ int i,j,group,Case=0,x,y; while(scanf("%d%d",&n,&m)!=EOF){ init(); for(i=0;i<n;++i)add(i,i+n,1); while(m--){ scanf(" (%d,%d)",&x,&y); add(x+n,y,inf); add(y+n,x,inf); } int ans=inf; for(i=1;i<n;++i){ ans=min(ans,Dinic(0+n,i)); init_flow(); } if(ans==inf)ans=n; printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/Kurokey/p/5519792.html