Kindergarten(网络流解法)

Deadly 提交于 2019-12-03 20:54:14

题意:http://acm.hdu.edu.cn/showproblem.php?pid=2458

问你二分图的最大团是多大。

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 #include <cassert>
 21 #include <iomanip>
 22 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 23 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 24 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 25 //******************
 26 int lowbit(int n);
 27 int Del_bit_1(int n);
 28 clock_t __STRAT,__END;
 29 double __TOTALTIME;
 30 void _MS(){__STRAT=clock();}
 31 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
 32 //***********************
 33 #define rint register int
 34 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 35 #define fr(a,b,c) for(rint a=b;a>=c;--a)
 36 #define mem(a,b) memset(a,b,sizeof(a))
 37 #define pr printf
 38 #define sc scanf
 39 #define ls rt<<1
 40 #define rs rt<<1|1
 41 typedef pair<int,int> PII;
 42 typedef vector<int> VI;
 43 typedef long long ll;
 44 const double E=2.718281828;
 45 const double PI=acos(-1.0);
 46 const ll INF=(1LL<<60);
 47 const int inf=(1<<30);
 48 const double ESP=1e-9;
 49 const int mod=(int)1e9+7;
 50 const int N=(int)4e2+10;
 51 const int M=40010;
 52 
 53 class DINIC
 54 {
 55 public:
 56 //    const int MAXN=10004,MAXWAY=100005;
 57     int n,way,max_flow,deep[N];
 58     int tot,head[N],cur[N];
 59     struct EDGE{
 60         int to,next;
 61         int dis;
 62     }edge[M];
 63     void Init(int n_)
 64     {
 65         tot=-1;//因为加反向边要^1,所以要从0开始;
 66         n=n_;
 67         max_flow=0;
 68         for(int i=0;i<=n_;++i)
 69             head[i]=-1;
 70     }
 71     void add(int from,int to,int V)
 72     {
 73         //正向
 74         ++tot;
 75         edge[tot].to=to;
 76         edge[tot].dis=V;
 77         edge[tot].next=head[from];
 78         head[from]=tot;
 79         //反向
 80         swap(from,to);
 81         ++tot;
 82         edge[tot].to=to;
 83         edge[tot].dis=0;
 84         edge[tot].next=head[from];
 85         head[from]=tot;
 86     }
 87     queue<int>q;
 88     bool bfs(int s,int t)
 89     {
 90         for(int i=1;i<=n;++i)
 91             deep[i]=inf;
 92         while(!q.empty())q.pop();
 93         for(int i=1;i<=n;++i)cur[i]=head[i];
 94         deep[s]=0;
 95         q.push(s);
 96 
 97         while(!q.empty())
 98         {
 99             int now=q.front();q.pop();
100             for(int i=head[now];i!=-1;i=edge[i].next)
101             {
102                 if(deep[edge[i].to]==inf&&edge[i].dis)
103                 {
104                     deep[edge[i].to]=deep[now]+1;
105                     q.push(edge[i].to);
106                 }
107             }
108         }
109         return deep[t]<inf;
110     }
111     int dfs(int now,int t,int limit)
112     {
113         if(!limit||now==t)return limit;
114         int flow=0,f;
115         for(int i=cur[now];i!=-1;i=edge[i].next)
116         {
117             cur[now]=i;
118             if(deep[edge[i].to]==deep[now]+1&&(f=dfs(edge[i].to,t,min(limit,edge[i].dis))))
119             {
120                 flow+=f;
121                 limit-=f;
122                 edge[i].dis-=f;
123                 edge[i^1].dis+=f;
124                 if(!limit)break;
125             }
126         }
127         return flow;
128     }
129     void Dinic(int s,int t)
130     {
131         while(bfs(s,t))
132             max_flow+=dfs(s,t,inf);
133     }
134 }G;
135 
136 int mp[N][N];
137 
138 int main()
139 {
140     int boy,girl,m;
141     int cnt=0;
142     while(~sc("%d%d%d",&boy,&girl,&m),boy||girl||m)
143     {
144         mem(mp,1);
145         G.Init(boy+girl+2);
146         for(int i=1;i<=m;++i)
147         {
148             int u,v;
149             sc("%d%d",&u,&v);
150             mp[u][v]=0;
151         }
152         int S=boy+girl+1,T=boy+girl+2;
153         for(int i=1;i<=boy;++i)
154             G.add(S,i,1);
155         for(int i=boy+1;i<=boy+girl;++i)
156             G.add(i,T,1);
157         for(int i=1;i<=boy;++i)
158             for(int j=1;j<=girl;++j)
159                 if(mp[i][j])
160                     G.add(i,j+boy,inf);
161         G.Dinic(S,T);
162         pr("Case %d: %d\n",++cnt,boy+girl-G.max_flow);
163     }
164     return 0;
165 }
166 
167 /**************************************************************************************/
168 
169 int lowbit(int n)
170 {
171     return n&(-n);
172 }
173 
174 int Del_bit_1(int n)
175 {
176     return n&(n-1);
177 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!