-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path218.cpp
66 lines (60 loc) · 1.17 KB
/
218.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
int a[510][510],last[510],p[510];
bool b[510];
int n,ans,l,r,mid,num;
void init()
{
scanf("%d",&n);
l=2147483647;
r=-2147483646;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
if (a[i][j]>r) r=a[i][j];
if (a[i][j]<l) l=a[i][j];
}
}
bool augment(int x)//二分图匹配
{
for (int i=1;i<=n;i++)
if (b[i]==false && a[x][i]<=mid)
{
b[i]=true;
if (last[i]==0 || augment(last[i]))
{
last[i]=x;
return true;
}
}
return false;
}
void Main()
{
while (l<=r)
{
mid=(l+r)/2;
memset(last,0,sizeof(last));
ans=0;
for (int i=1;i<=n;i++)
{
memset(b,false,sizeof(b));
if (augment(i)) ans++;
}
if (ans==n) r=mid-1;
else l=mid+1;
if (ans==n)
for (int j=1;j<=n;j++) p[last[j]]=j;
}
printf("%d\n",l);
for (int i=1;i<=n;i++) printf("%d %d\n",i,p[i]);
}
int main()
{
init();
Main();
}