10.20T5 二分图最大匹配
Description
Farmer John has a brilliant idea for the next great spectator sport: Cow Steeplechase! As everyone knows, regular steeplechase involves a group of horses that race around a course filled with obstacles they must jump over. FJ figures the same contest should work with highly-trained cows, as long as the obstacles are made short enough.
In order to design his course, FJ makes a diagram of all the N (1 <= N <= 250) possible obstacles he could potentially build. Each one is represented by a line segment in the 2D plane that is parallel to the horizontal or vertical axis. Obstacle i has distinct endpoints (X1_i, Y1_i) and (X2_i, Y2_i) (1 <= X1_i, Y1_i, X2_i, Y2_i <= 1,000,000,000). An example is as follows:
FJ would like to build as many of these obstacles as possible, subject to the constraint that no two of them intersect. Starting with the diagram above, FJ can build 7 obstacles:
Two segments are said to intersect if they share any point in common, even an endpoint of one or both of the segments. FJ is certain that no two horizontal segments in the original input diagram will intersect, and that similarly no two vertical segments in the input diagram will intersect.
Please help FJ determine the maximum number of obstacles he can build.
In order to design his course, FJ makes a diagram of all the N (1 <= N <= 250) possible obstacles he could potentially build. Each one is represented by a line segment in the 2D plane that is parallel to the horizontal or vertical axis. Obstacle i has distinct endpoints (X1_i, Y1_i) and (X2_i, Y2_i) (1 <= X1_i, Y1_i, X2_i, Y2_i <= 1,000,000,000). An example is as follows:
FJ would like to build as many of these obstacles as possible, subject to the constraint that no two of them intersect. Starting with the diagram above, FJ can build 7 obstacles:
Two segments are said to intersect if they share any point in common, even an endpoint of one or both of the segments. FJ is certain that no two horizontal segments in the original input diagram will intersect, and that similarly no two vertical segments in the input diagram will intersect.
Please help FJ determine the maximum number of obstacles he can build.
Input
* Line 1: A single integer: N.
* Lines 2..N+1: Line i+1 contains four space-separated integers representing an obstacle: X1_i, Y1_i, X2_i, and Y2_i.
* Lines 2..N+1: Line i+1 contains four space-separated integers representing an obstacle: X1_i, Y1_i, X2_i, and Y2_i.
Output
* Line 1: The maximum number of non-crossing segments FJ can choose.
Sample Input
3
4 5 10 5
6 2 6 12
8 3 8 5
INPUT DETAILS:
There are three potential obstacles. The first is a horizontal segment connecting (4, 5) to (10, 5); the second and third are vertical segments connecting (6, 2) to (6, 12) and (8, 3) to (8, 5).
Sample Output
2
OUTPUT DETAILS:
The optimal solution is to choose both vertical segments.
简述题意:
给出n条水平或者竖直的线段,保证同方向线段不相交,问最多能选出几条互不相交的线段。
如果相交就连接,然后跑二分图
code:
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 #define N 1000 6 struct node { 7 int left,right,y; 8 } heng[N]; 9 struct T { 10 int down,up,x; 11 } lie[N]; 12 int v[N],matching[N],g[N][N]; 13 int n; 14 bool match(int u){ 15 for(int i=1;i<=n;i++){ 16 if(v[i]||!g[u][i])continue; 17 v[i]=1; 18 if(!matching[i]||match(matching[i])){ 19 matching[i]=u; 20 return true; 21 } 22 } 23 return false; 24 } 25 int cnt1=0,cnt2=0; 26 int hun(){ 27 int ans=0; 28 for(int i=1+cnt1;i<=cnt1+cnt2;i++){ 29 memset(v,0,sizeof v); 30 if(match(i))ans++; 31 } 32 return ans; 33 } 34 int main() { 35 cin>>n; 36 for(int i=1; i<=n; i++) { 37 int x1,y1,x2,y2; 38 cin>>x1>>y1>>x2>>y2; 39 if(x1==x2) { 40 lie[++cnt2]=(T){min(y1,y2),max(y1,y2),x1}; 41 42 // cout<<"lie->down->"<<lie[cnt2].down<<" up->"<<lie[cnt2].up<<' '<<x1<<'\n'; 43 } else { 44 heng[++cnt1]=(node){min(x1,x2),max(x1,x2),y1}; 45 // cout<<"heng->left->"<<heng[cnt1].left<<" right->"<<heng[cnt1].right<<" "<<y1<<'\n'; 46 } 47 } 48 //cout<<cnt1<<" "<<cnt2<<'\n'; 49 for(int i=1; i<=cnt1; i++) { 50 for(int j=1; j<=cnt2; j++) { 51 // cout<<i<<" "<<j<<'\n'; 52 if(heng[i].left<=lie[j].x&&lie[j].x<=heng[i].right&&lie[j].down<=heng[i].y&&heng[i].y<=lie[j].up){ 53 g[i][j+cnt1]=g[j+cnt1][i]=1; 54 // cout<<"succ"<<i<<" "<<j<<endl; 55 } 56 } 57 } 58 cout<<n-hun(); 59 return 0; 60 }
over