ACM 二分图匹配 匈牙利匹配模板 URAL 1997

引用:趣写算法系列之--匈牙利算法

模板

最坏时间复杂度:

G=(V,E),V为点数,E为边数

每次增广时间为O(E),最多进行O(V)次迭代,时间复杂度为O(VE).


//Bipartite -_-| by Ck0123 
#include <iostream>
#include <cstdio> 
#include <cstring>
using namespace std; 
int R[40100],pre[40100],last[40100],match[40100];
bool flag[40100];
int eNum,n,m; 

void add(int x,int y){
	eNum ++;
	R[eNum] = y;
	pre[eNum] = last[x];
	last[x] = eNum;
}

bool find(int k){
   for(int x = last[k]; x; x = pre[x])
   	  if(!flag[R[x]]){
		flag[R[x]] = 1; 
			if(!match[R[x]] || find(match[R[x]])){
				match[R[x]] = k;
				return 1;
			}
	  }
	return 0;
}
void Bipartite(){
	
	int ret = 0; 
	for(int k = 1; k <= n; k++){
    	memset(flag, 0, sizeof(flag));
    	if(find(k))
    	ret++;
	}
		eNum = 0;  
		memset(match,0,sizeof(match));
		memset(last,0,sizeof(last));
		memset(pre,0,sizeof(pre));
		memset(R,0,sizeof(R)); 




例题:


Those are not the droids you're looking for
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Bar owner: Hey. We don’t serve their kind here. 
Luke: What? 
Bar owner: Your droids – they’ll have to wait outside. We don’t want them here.
Planet Tatooine is quite far from the center of the Galaxy. It is at the intersection of many hyperspace paths and it hosts smugglers and hoods of all sorts. The pilots who visited external territories have been to the space port bar called Mos Eisley for a drink at least once.
In this bar you can find a bunch of rascals and scoundrels from all over the Galaxy. The bar owner is ready to make drinks for any client except for, perhaps, a droid. Usually the bar has a lot of smugglers hanging out there. Each smuggler spends at least  a minutes inside hoping to meet a good client. Cargo owners show up quite often as well. They usually find a dealer quickly, so they never spend more than b minutes in the bar.
The imperial stormtroopers are searching through Tatooine for the escaped droids. The bar owner said that no droids had ever been on his territory. He also said that nobody except for smugglers and cargo owners had been in the place recently.
Help the stormtroopers find out if the owner is a liar. For that, you are going to need the daily records from the sensor on the entrance door. The sensor keeps record of the time when somebody entered the bar or left it. The stormtroopers got the records after the bar had been closed, so there was nobody in the bar before or after the sensor took the records. You can assume that the sensor is working properly. That is, if somebody went through the bar door, the sensor made a record of that. You can also assume that the bar clients go in and out only through the door with the sensor. But the bar owner and the staff use the ‘staff only’ door.

Input

The first line of the input contains integers  a and  b (1 ≤  ab ≤ 10  9b + 1 <  a). The second line contains integer  n — the number of records from the sensor (2 ≤  n ≤ 1000). The  i-th of the next  n lines contains two integers  t i and  d i (1 ≤  t i ≤ 10  9d i ∈ {0,1}) — the time of the  i-th record and direction (0 — in the bar, 1 — out of the bar). The records in the input are listed in the increasing order of  t i.

Output

If there is no doubt that somebody who was neither a smuggler nor a cargo owner visited the bar, print "Liar" on a single line. Otherwise, print a line "No reason". And in the following lines list information on all visitors of the bar. The information about a visitor consists of two space-separated numbers — the time this visitor entered the bar and the time he left the bar. If there are multiple solutions that correspond to the sensor records, print any of them.

Sample Input

input output
6 3
4
1 0
2 0
5 1
10 1
No reason
1 10
2 5
6 3
4
1 0
2 0
6 1
10 1
Liar




Memory: 10181 KB Time: 46 MS
Language: G++ 4.7.2 Result: Accepted

#include <stdio.h>
#include <queue>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string.h>
#define FOR(i,a,b) for (int i = a; i <=b; i ++)
#define FORD(i,a,b) for (int i = a; i >=b; i --)
#define mem(a, b) memset(a, b, sizeof(a))
#define Sqr(x) ((x) * (x))
typedef long long LL;
using namespace std;

int a,b;
int l[1011],r[1011];
int R[500100],pre[500100],last[500100],match[500100],L[500100];
bool flag[1011];
int eNum,n,m,num1,num2; 

void add(int x,int y){
	eNum ++;
	R[eNum] = y;
	pre[eNum] = last[x];
	last[x] = eNum;
}

bool find(int k){
	for (int x = last[k]; x;x = pre[x]){
		if (!flag[R[x]]){
			flag[R[x]] = 1;
			if (!match[R[x]]||find(match[R[x]])){
				match[R[x]] = k;
				L[k] = R[x];
				return 1;
			}
		}
	}
	return 0;
}

void answer(){
	cout<<"No reason"<<endl;
	FOR(i,1,num1){
		cout<<l[i]<<" "<<r[L[i]]<<endl;
	}
}

void Bipartite(){
	int ret = 0;
	for (int k = 1; k<= num1; k ++){
		memset(flag,0,sizeof(flag));
		if (find(k)) ret ++;
	}
	if (ret == num1){
		answer();
	} else  cout<<"Liar"<<endl;
	
}

int main(){
	cin>>a>>b;
	cin>>n;
	num1 = 0;
	num2 = 0;
	eNum = 0;
	memset(l,0,sizeof(l));
	memset(r,0,sizeof(r));
	memset(match,0,sizeof(match));
	memset(last,0,sizeof(last));
	memset(pre,0,sizeof(pre));
	memset(R,0,sizeof(R));
	memset(L,0,sizeof(L));
	
	FOR(i,1,n) {
		int x,tim;
		cin>>x>>tim;
		if (tim==0){
			num1 ++;
			l[num1] = x;
		} else {
			num2 ++;
			r[num2] = x;
		}
	}
	
	if (n%2==1) {
		cout<<"Liar"<<endl;
		//system("pause");
		return 0;
	}
	
	if (num1!=num2) {
		cout<<"Liar"<<endl;
		//system("pause");
		return 0;
	}
	
	FOR(i,1,num1)
	FOR(j,1,num2)
	   if ((r[j]-l[i]>=a)||((0<=r[j]-l[i])&&(r[j]-l[i]<=b))){
	   	  add(i,j);
	   }
	
	Bipartite();
	//system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值