ACM 二分图匹配 匈牙利匹配模板 URAL 1997
2014-08-13 17:27
323 查看
引用:趣写算法系列之--匈牙利算法
模板
最坏时间复杂度:
G=(V,E),V为点数,E为边数
每次增广时间为O(E),最多进行O(V)次迭代,时间复杂度为O(VE).
例题:
Those are not the droids you're looking for
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
URAL
1997
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 thanb 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 ≤ a, b ≤ 10 9, b + 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 ti and di (1 ≤ ti ≤ 10 9, di ∈
{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 ti.
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
模板
最坏时间复杂度:
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
URAL
1997
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 thanb 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 ≤ a, b ≤ 10 9, b + 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 ti and di (1 ≤ ti ≤ 10 9, di ∈
{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 ti.
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; }
相关文章推荐
- 【模板】二分图匹配/一般图匹配——匈牙利算法/随机匈牙利算法
- ACM_模板_二分图匹配(匈牙利算法)-DFS
- 【二分图匹配入门专题1】F - COURSES poj1469【最大匹配--匈牙利算法模板题】
- [ACM]二分图 最大匹配 最大权匹配 匈牙利 Hopcroft–Karp Kuhn-Munkres
- 匈牙利算法(二分图匹配)模板
- 匈牙利算法模板 二分图最大匹配
- 二分图最大匹配之匈牙利算法模板
- hihocoder #1122 二分图二•二分图最大匹配之匈牙利算法(*【模板】应用 )
- 二分图匹配算法之匈牙利算法模板 hdoj1083 nyoj月老的难题
- HDU 2444 The Accomodation of Students(最大二分匹配(匈牙利算法)+二分图判断->模板题目)
- 最大匹配之匈牙利算法模板。。
- 【二分图匹配】最大匹配-匈牙利算法BFS && DFS写法
- 最大二分匹配 匈牙利算法模板&&POJ 1469 COURSES
- 二分图匹配 匈牙利算法 模板
- 最大匹配之匈牙利算法模板。。
- 二分图匹配之最大匹配——匈牙利算法
- 二分图匹配(匈牙利算法模板)
- 【模板】匈牙利算法——二分图最大匹配
- Pku acm 1466 Girls and Boys数据结构题目解题报告(十七)---- 匈牙利算法求二分图的最大匹配
- 二分图最大匹配 匈牙利算法模板