hdu3081 Marriage Match II(最大流)

轉載請注明出處:?http://www.cnblogs.com/fraud/?????????? ——by fraud

?

Marriage Match II

Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2410????Accepted Submission(s): 820


Problem Description
Presumably, you all have known the question of stable marriage match. A girl will choose a boy; it is similar as the game of playing house we used to play when we are kids. What a happy time as so many friends playing together. And it is normal that a fight or a quarrel breaks out, but we will still play together after that, because we are kids.?
Now, there are 2n kids, n boys numbered from 1 to n, and n girls numbered from 1 to n. you know, ladies first. So, every girl can choose a boy first, with whom she has not quarreled, to make up a family. Besides, the girl X can also choose boy Z to be her boyfriend when her friend, girl Y has not quarreled with him. Furthermore, the friendship is mutual, which means a and c are friends provided that a and b are friends and b and c are friend.?
Once every girl finds their boyfriends they will start a new round of this game—marriage match. At the end of each round, every girl will start to find a new boyfriend, who she has not chosen before. So the game goes on and on.
Now, here is the question for you, how many rounds can these 2n kids totally play this game?


Input
There are several test cases. First is a integer T, means the number of test cases.?
Each test case starts with three integer n, m and f in a line (3<=n<=100,0<m<n*n,0<=f<n). n means there are 2*n children, n girls(number from 1 to n) and n boys(number from 1 to n).
Then m lines follow. Each line contains two numbers a and b, means girl a and boy b had never quarreled with each other.?
Then f lines follow. Each line contains two numbers c and d, means girl c and girl d are good friends.


Output
For each case, output a number in one line. The maximal number of Marriage Match the children can play.


Sample Input
1 4 5 2 1 1 2 3 3 2 4 2 4 4 1 4 2 3


Sample Output
2


Author
starvae


Source
HDU 2nd “Vegetable-Birds Cup” Programming Open Contest

?

題意:

有n個女孩和n個男孩,有m個女孩與男孩的關系,代表女孩喜歡男孩,有f個女孩與女孩的關系,代表女孩與女孩是好朋友。

若女孩i與女孩j是好朋友,而女孩i喜歡男孩k,則女孩j也可以和男孩匹配。即女孩之間的關系是可以傳遞的,而男孩之間的不可以。

每對男孩與女孩之間只能匹配一次,問可以進行幾輪配對。

分析:
先floyd搞好女孩之間的傳遞關系,然后對于可以配對的女孩與男孩之間連一條容量為1的邊,然后二分答案,每次二分之后從源點向各個女孩連容量為二分值的邊,從各個男孩向匯點連容量為二分值的邊。

  1 //#####################
  2 //Author:fraud
  3 //Blog: http://www.cnblogs.com/fraud/
  4 //#####################
  5 #include <iostream>
  6 #include <sstream>
  7 #include <ios>
  8 #include <iomanip>
  9 #include <functional>
 10 #include <algorithm>
 11 #include <vector>
 12 #include <string>
 13 #include <list>
 14 #include <queue>
 15 #include <deque>
 16 #include <stack>
 17 #include <set>
 18 #include <map>
 19 #include <cstdio>
 20 #include <cstdlib>
 21 #include <cmath>
 22 #include <cstring>
 23 #include <climits>
 24 #include <cctype>
 25 using namespace std;
 26 #define XINF INT_MAX
 27 #define INF 0x3FFFFFFF
 28 #define MP(X,Y) make_pair(X,Y)
 29 #define PB(X) push_back(X)
 30 #define REP(X,N) for(int X=0;X<N;X++)
 31 #define REP2(X,L,R) for(int X=L;X<=R;X++)
 32 #define DEP(X,R,L) for(int X=R;X>=L;X--)
 33 #define CLR(A,X) memset(A,X,sizeof(A))
 34 #define IT iterator
 35 typedef long long ll;
 36 typedef pair<int,int> PII;
 37 typedef vector<PII> VII;
 38 typedef vector<int> VI;
 39 #define MAXN 1010
 40 struct edge{
 41     int to,cap,rev;
 42     edge(int _to,int _cap,int _rev)
 43     {
 44         to=_to;
 45         cap=_cap;
 46         rev=_rev;
 47     }
 48 };
 49 const int MAX_V=5020;
 50 vector<edge>G[MAX_V];
 51 int iter[MAX_V];
 52 int head[MAXN];
 53 int _to[510*510];
 54 int _flow[510*510];
 55 int _next[510*510];
 56 int level[MAX_V];
 57 int tot=0;
 58 void add_edge(int from,int to,int cap)
 59 {
 60     G[from].PB(edge(to,cap,G[to].size()));
 61     G[to].PB(edge(from,0,G[from].size()-1));
 62 }
 63 void Add(int u,int v,int f){
 64     _to[tot]=v;
 65     _flow[tot]=f;
 66     _next[tot]=head[u];
 67     head[u]=tot++;
 68 }
 69 void bfs(int s,int t)
 70 {
 71     CLR(level,-1);
 72     queue<int>q;
 73     level[s]=0;
 74     q.push(s);
 75     while(!q.empty())
 76     {
 77         int u=q.front();
 78         q.pop();
 79         for(int i=0;i<G[u].size();i++)
 80         {
 81             edge &e=G[u][i];
 82             if(e.cap>0&&level[e.to]<0)
 83             {
 84                 level[e.to]=level[u]+1;
 85                 q.push(e.to);
 86             }
 87         }
 88     }
 89 }
 90 int dfs(int v,int t,int f)
 91 {
 92     if(v==t)return f;
 93     for(int &i=iter[v];i<G[v].size();i++)
 94     {
 95         edge &e=G[v][i];
 96         if(e.cap>0&&level[v]<level[e.to])
 97         {
 98             int d=dfs(e.to,t,min(f,e.cap));
 99             if(d>0)
100             {
101                 e.cap-=d;;
102                 G[e.to][e.rev].cap+=d;
103                 return d;
104             }
105         }
106     }
107     return 0;
108 }
109 int Dinic(int s,int t)
110 {
111     int flow=0;
112     for(;;)
113     {
114         bfs(s,t);
115         if(level[t]<0)return flow;
116         memset(iter,0,sizeof(iter));
117         int f;
118         while((f=dfs(s,t,INF))>0)
119         {
120             flow+=f;
121         }
122     }
123 }
124 
125 int a[210][210];
126 
127 int main()
128 {
129     ios::sync_with_stdio(false);
130     int t;
131     scanf("%d",&t);
132     while(t--){
133         int n,m,f;
134         scanf("%d%d%d",&n,&m,&f);
135         tot=0;
136         for(int i=0;i<MAXN;i++)head[i]=-1;
137         int u,v;
138         memset(a,0,sizeof(a));
139         for(int i=0;i<m;i++){
140             scanf("%d%d",&u,&v);
141             a[u][v+n]=1;
142         }
143         for(int i=0;i<f;i++){
144             scanf("%d%d",&u,&v);
145             a[u][v]=1;
146             a[v][u]=1;
147         }
148         for(int i=1;i<=n+n;i++)a[i][i]=1;
149         for(int k=1;k<=n+n;k++){
150             for(int i=1;i<=n+n;i++){
151                 for(int j=1;j<=n+n;j++){
152                     a[i][j]=max(a[i][j],a[i][k]&a[k][j]);
153                 }
154             }
155         }
156         for(int i=1;i<=n;i++){
157             for(int j=1+n;j<=n+n;j++){
158                 if(a[i][j])Add(i,j,1);
159             }
160         }
161         int l=0,r=n;
162         int s=0,t=2*n+1;
163         int ans=0;
164         while(l<=r){
165             int mid=(l+r)>>1;
166             for(int i=0;i<=2*n+1;i++)G[i].clear();
167             for(int i=1;i<=2*n;i++){
168                 int now=head[i];
169                 while(now!=-1){
170                     add_edge(i,_to[now],_flow[now]);
171                     now=_next[now];
172                 }
173             }
174             for(int i=1;i<=n;i++){
175                 add_edge(s,i,mid);
176                 add_edge(n+i,t,mid);
177             }
178             if(Dinic(s,t)==mid*n){
179                 ans=mid;
180                 l=mid+1;
181             }
182             else r=mid-1;
183         }
184         printf("%d\n",ans);
185     }
186         
187                     
188             
189     return 0;
190 }
代碼君

?

轉載于:https://www.cnblogs.com/fraud/p/4354766.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/273353.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/273353.shtml
英文地址,請注明出處:http://en.pswp.cn/news/273353.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

CentOS6安裝tomcat6

首先我們要下載一個tomcat的安裝包 http://ftp.riken.jp/net/apache/ wget http://ftp.riken.jp/net/apache/tomcat/tomcat-6/v6.0.41/src/apache-tomcat-6.0.41.tar.gz 下載好后解壓到一個以目錄&#xff0c;我的是放在了/usr/apache-tomcat-6.0.41 tar –zxvf apache-t…

修復 XE7 , XE8 Frame 內 PopupMenu 快捷鍵失效問題

問題&#xff1a;將 Frame 含 PopupMenu 放置 Form 后&#xff0c;在 Frame 里的 PopupMenu 失效&#xff0c;無法按快捷鍵。 適用&#xff1a;(XE7 update 1 / XE8) for Windows 平臺 修正方法&#xff1a; 請將源碼 FMX.Forms.pas 復制到自己的工程目錄里&#xff0c;再進行修…

Vmware Centos中安裝vmtools工具

在Vmware安裝虛擬機是很好玩的&#xff0c;可是有時候在虛擬機與本地主機之間相互傳遞文件時卻是一件比較麻煩的事情&#xff0c;這時候我們安裝一個vmtools的工具這樣我們就可以隨意的在虛擬機與主機之間相互拖拽文件&#xff0c;下面我們就來說說如何安裝vmtools 點擊虛擬機會…

關于Dapper - 能否不創建定義表對應類使用

1.是可以的&#xff0c;而且支持的很棒 1 /*2 lcg3 * 1.看看能不能用4 * 2.怎么用 - 引哪個文件即可&#xff1f;5 */6 7 //數據庫連接參數8 private const string strConn "Data SourceAlen;Initial Catal…

動態規劃 背包九講的實現。

最近在學習動態規劃&#xff0c;會了不少基礎的之后就開始挑戰比較困難的背包問題了&#xff0c;我這里自己寫了每一講的問題&#xff0c;解析&#xff0c;代碼&#xff0c;注釋。如果dp還沒入門的孩紙就去看看我的另一篇文章http://www.cnblogs.com/luyi14/p/4344946.html …

Linux中查看負載

行車過橋 一只單核的處理器可以形象得比喻成一條單車道。設想下&#xff0c;你現在需要收取這條道路的過橋 費 — 忙于處理那些將要過橋的車輛。你首先當然需要了解些信息&#xff0c;例如車輛的載重、以及 還有多少車輛正在等待過橋。如果前面沒有車輛在等待&#xff0c;那么你…

flask小demo-數據查詢

mysqlconn-flask.py 1 # -*- coding: utf-8 -*-2 #codingutf-83 4 import os5 import mysql.connector6 from flask import Flask, request, render_template7 8 app Flask(__name__)9 10 def db(): 11 # 注意把password設為你的root口令: 12 conn mysql.connect…

js實現的文件下載

/** * Javascript 多文件下載 * author Barret Lee * email barret.chinagmail.com */var Downer (function(files) { var h5Down !/Trident|MSIE/.test(navigator.userAgent); // try{ // h5Down document.createElement("a").hasOwnProperty("download&quo…

Jersey注解詳解

REST 在 2000 年由 Roy Fielding 在博士論文中提出&#xff0c;他是 HTTP 規范 1.0 和 1.1 版的首席作者之一。 REST 中最重要的概念是資源&#xff08;resources&#xff09;&#xff0c;使用全球 ID&#xff08;通常使用 URI&#xff09;標識。客戶端應用程序使用 HTTP 方法&…

Struts2配置文件詳解

解決在斷網環境下,配置文件無提示的問題我們可以看到Struts.xml在斷網的情況下,前面有一個嘆號,這時,我們按alt/ 沒有提示,這是因為” http://struts.apache.org/dtds/struts-2.0.dtd”是一個網絡地址,如果上網的話,IDE會自動幫我們下載此文件,如果斷網就沒有辦法了,但是我們還…

mysql插入圖片數據

import java.sql.*; import java.util.Scanner; import java.io.*; public class mysql插入圖片 { private static final File File null;private static String String;public static Connection getConn() { Connection conn null; try { Class.forName("com.…

mybatis插入圖片處理--mysql

1. 數據庫Scheme 1.數據庫SchemeDROP TABLE IF EXISTS user_graphic_t; /*!40101 SET saved_cs_client character_set_client */; /*!40101 SET character_set_client utf8 */; CREATE TABLE user_graphic_t ( id int(11) NOT NULL AUTO_INCREMENT, graph…

careercup-高等難度 18.6

18.6 設計一個算法&#xff0c;給定10億個數字&#xff0c;找出最小的100萬個數字。假定計算機內存足以容納全部10億個數字。 解法&#xff1a; 方法1&#xff1a;排序 按升序排序所有的元素&#xff0c;然后取出前100萬個數&#xff0c;時間復雜度為O(nlog(n)) 方法2&#xff…

不浮躁的社會是什么樣的?

不浮躁就是該吃飯吃飯&#xff0c;該睡覺睡覺。該看書看書&#xff0c;該洗澡洗澡。聊事時聊事&#xff0c;陪朋友時陪朋友。萬事各得其所&#xff0c;各安其分&#xff0c;專心在此時此刻&#xff0c;做每一件事。而不是吃飯時想著別人的魚翅海參&#xff0c;睡覺時想著發票報…

java jre 中導入導出證書

導入證書&#xff1a; 將所要導入的證書放到Javahome的jre/lib/security文件夾中 運行命令jre/bin/keytool-import -alias cacerts -keystore cacerts -file 證書名稱 輸入默認密碼&#xff1a;changeit 導入過程中會交互詢問是否信任該證書&#xff0c;輸入 yes 導出證書 …

各種類庫網址學習

http://shouce.jb51.net/net/index.html轉載于:https://www.cnblogs.com/chenls/p/4362730.html

圖片的base64編碼實現以及網頁上顯示

生成、解析base64編碼的圖片 //圖片轉化成base64字符串 public static String GetImageStr(<span style"font-family: Arial, Helvetica, sans-serif;">String imgFile</span><span style"font-family: Arial, Helvetica, sans-serif;">…

nginx windows 下安裝和配置

去nginx官網下載相應的版本 下載地址&#xff1a;http://nginx.org/download/nginx-1.6.2.zip 下載完成解壓放到你喜歡的目錄下&#xff1b;樓主的放到了F:\nginx 進入windows的cmd窗口&#xff0c;輸入如下所示的命令&#xff1a; C:\Users\YiXian>F: F:\>cd nginx s…

c/c++學習書籍

一、c Primer . 目錄內容關鍵字第一章 面向過程編程&#xff0c;面向對象編程&#xff0c;泛型 轉載于:https://www.cnblogs.com/bzsh/p/4362930.html

applicationContext.xml 配置文件的存放位置

web.xml中classpath:和classpath*: 有什么區別? classpath&#xff1a;只會到你的class路徑中查找找文件; classpath*&#xff1a;不僅包含class路徑&#xff0c;還包括jar文件中(class路徑)進行查找. 存放位置&#xff1a; 1&#xff1a;src下面 需要在web.xml中定義如下&…