标签:
Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1971 Accepted Submission(s): 583
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; const int MAXNODE = 805; const int MAXEDGE = MAXNODE*MAXNODE; typedef int Type; const Type INF = 0x3f3f3f3f; struct Edge { int u, v; Type cap, flow; Edge() {} Edge(int u, int v, Type cap, Type flow) { this->u = u; this->v = v; this->cap = cap; this->flow = flow; } }; struct Dinic { int n, m, s, t; Edge edges[MAXEDGE]; int first[MAXNODE]; int next[MAXEDGE]; bool vis[MAXNODE]; Type d[MAXNODE]; int cur[MAXNODE]; vector<int> cut; void init(int n) { this->n = n; memset(first, -1, sizeof(first)); m = 0; } void add_Edge(int u, int v, Type cap) { edges[m] = Edge(u, v, cap, 0); next[m] = first[u]; first[u] = m++; edges[m] = Edge(v, u, 0, 0); next[m] = first[v]; first[v] = m++; } bool bfs() { memset(vis, false, sizeof(vis)); queue<int> Q; Q.push(s); d[s] = 0; vis[s] = true; while (!Q.empty()) { int u = Q.front(); Q.pop(); for (int i = first[u]; i != -1; i = next[i]) { Edge& e = edges[i]; if (!vis[e.v] && e.cap > e.flow) { vis[e.v] = true; d[e.v] = d[u] + 1; Q.push(e.v); } } } return vis[t]; } Type dfs(int u, Type a) { if (u == t || a == 0) return a; Type flow = 0, f; for (int &i = cur[u]; i != -1; i = next[i]) { Edge& e = edges[i]; if (d[u] + 1 == d[e.v] && (f = dfs(e.v, min(a, e.cap - e.flow))) > 0) { e.flow += f; edges[i^1].flow -= f; flow += f; a -= f; if (a == 0) break; } } return flow; } Type Maxflow(int s, int t) { this->s = s; this->t = t; Type flow = 0; while (bfs()) { for (int i = 0; i < n; i++) cur[i] = first[i]; flow += dfs(s, INF); } return flow; } void MinCut() { cut.clear(); for (int i = 0; i < m; i += 2) { if (vis[edges[i].u] && !vis[edges[i].v]) cut.push_back(i); } } } gao; const int N = 251; bool vis[N][N]; int father[N]; int girl[N*N],boy[N*N]; ///这里m是属于 (0,n*n]的 int n,m,k,f,src,des; int _find(int x) { if(father[x]!=x) { father[x] = _find(father[x]); } return father[x]; } void build(int c) { gao.init(3*n+2); for(int i=1; i<=n; i++) { gao.add_Edge(src,i,c); gao.add_Edge(i,n+i,k); gao.add_Edge(2*n+i,des,c); } for(int i=1; i<=n; i++) { for(int j=2*n+1; j<=3*n; j++) { if(vis[i][j]) gao.add_Edge(i,j,1); else gao.add_Edge(n+i,j,1); } } } int main() { int tcase; scanf("%d",&tcase); while(tcase--) { scanf("%d%d%d%d",&n,&m,&k,&f); memset(vis,false,sizeof(vis)); src = 0,des = 3*n+1; for(int i=1; i<=n; i++) father[i] = i; /* for(int i=1;i<=m;i++){ //TLE int u,v; scanf("%d%d",&u,&v); v+=2*n; vis[u][v] = true; }*/ for(int i=1; i<=m; i++) { scanf("%d%d",&girl[i],&boy[i]); boy[i]+=2*n; } for(int i=1; i<=f; i++) { int u,v; scanf("%d%d",&u,&v); int a = _find(u),b = _find(v); if(a!=b) father[a] = b; } for(int i=1; i<=m; i++) { vis[_find(girl[i])][boy[i]] = true; } for(int i=1; i<=n; i++) ///预处理所有关系 { for(int j=2*n+1; j<=3*n; j++) { if(vis[i][j]) continue; if(vis[_find(i)][j]) vis[i][j] = true; } } /*for(int i=1;i<=n;i++){ //TLE for(int j=1;j<=n;j++){ if(_find(i)==_find(j)){ for(int k=2*n+1;k<=3*n;k++){ if(vis[i][k]||vis[j][k]) vis[i][k] = vis[j][k] = 1; } } } }*/ int l = 0,r = n,ans = 0; while(l<=r) { int mid = (l+r)>>1; build(mid); if(gao.Maxflow(src,des)==mid*n) { ans = mid; l = mid+1; } else r = mid-1; } printf("%d\n",ans); } return 0; }
hdu 3277(二分+最大流+拆点+离线处理+模板问题...)
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5725102.html