#include<stdio.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
//for 72 point, O(NK+NM lg K);
typedef pair<int,int> Pi;
#define X first
#define Y second
vector <int> E[2020];
int n, m, mat, ans, now, mx;
int xy[30030], yx[2020], prev[30030];
int chk[30030], in;
int iter[30030];
bool match(int x)
{
priority_queue <Pi, vector<Pi>, greater<Pi> >pq;
for(size_t i=iter[x];i<E[x].size();i++){
chk[E[x][i]] = in;
pq.push(Pi(E[x][i],x));
prev[E[x][i]] = -1;
}
while(!pq.empty()){
Pi tmp = pq.top();
pq.pop();
if(!xy[tmp.X]){
mx = max(mx, tmp.X);
int s = tmp.X, e = tmp.Y;
while(e!=-1){
int save = yx[e];
xy[s]=e, yx[e]=s;
e=prev[s], s=save;
}
return true;
}
int v = xy[tmp.X];
for(size_t i=iter[v];i<E[v].size();i++){
if(chk[E[v][i]] == in)continue;
chk[E[v][i]] = in;
pq.push(Pi(E[v][i],v));
prev[E[v][i]] = tmp.Y;
}
}
return false;
}
int main()
{
int k, i;
scanf("%d%d%d",&n,&m,&k);
for(i=1;i<=k;i++){
int x,y;
scanf("%d%d",&x,&y);
E[y].push_back(x);
}
for(i=1;i<=m;i++)sort(E[i].begin(), E[i].end());
for(i=1;i<=m;i++)if(match(in=i))mat++;
while(mat==m){
int t = min_element(yx+1,yx+1+m) - yx;
ans += (yx[t] - now) * (n - mx + 1);
now = yx[t];
for(i=1;i<=m;i++)while(iter[i] != (int)E[i].size() && E[i][iter[i]]<=now)iter[i]++;
xy[yx[t]] = 0, yx[t] = 0, mat--, in++;
if(match(t))mat++;
}
printf("%d",ans);
return 0;
}
Compilation message
marriage.cpp: In function 'int main()':
marriage.cpp:55:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d",&n,&m,&k);
^
marriage.cpp:58:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&x,&y);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1696 KB |
Output is correct |
2 |
Correct |
0 ms |
1696 KB |
Output is correct |
3 |
Correct |
0 ms |
1696 KB |
Output is correct |
4 |
Correct |
0 ms |
1696 KB |
Output is correct |
5 |
Correct |
0 ms |
1696 KB |
Output is correct |
6 |
Correct |
0 ms |
1696 KB |
Output is correct |
7 |
Correct |
0 ms |
1696 KB |
Output is correct |
8 |
Correct |
0 ms |
1696 KB |
Output is correct |
9 |
Correct |
0 ms |
1696 KB |
Output is correct |
10 |
Correct |
0 ms |
1696 KB |
Output is correct |
11 |
Correct |
0 ms |
1696 KB |
Output is correct |
12 |
Correct |
0 ms |
1696 KB |
Output is correct |
13 |
Correct |
0 ms |
1696 KB |
Output is correct |
14 |
Correct |
0 ms |
1696 KB |
Output is correct |
15 |
Correct |
0 ms |
1696 KB |
Output is correct |
16 |
Correct |
0 ms |
1696 KB |
Output is correct |
17 |
Correct |
0 ms |
1696 KB |
Output is correct |
18 |
Correct |
0 ms |
1696 KB |
Output is correct |
19 |
Correct |
0 ms |
1696 KB |
Output is correct |
20 |
Correct |
0 ms |
1696 KB |
Output is correct |
21 |
Correct |
0 ms |
1696 KB |
Output is correct |
22 |
Correct |
0 ms |
1696 KB |
Output is correct |
23 |
Correct |
0 ms |
1696 KB |
Output is correct |
24 |
Correct |
0 ms |
1696 KB |
Output is correct |
25 |
Correct |
16 ms |
1828 KB |
Output is correct |
26 |
Correct |
6 ms |
1696 KB |
Output is correct |
27 |
Correct |
0 ms |
1696 KB |
Output is correct |
28 |
Correct |
0 ms |
1696 KB |
Output is correct |
29 |
Correct |
3 ms |
1828 KB |
Output is correct |
30 |
Correct |
0 ms |
1828 KB |
Output is correct |
31 |
Correct |
136 ms |
1960 KB |
Output is correct |
32 |
Correct |
29 ms |
1828 KB |
Output is correct |
33 |
Correct |
0 ms |
1696 KB |
Output is correct |
34 |
Correct |
3 ms |
1696 KB |
Output is correct |
35 |
Correct |
66 ms |
2224 KB |
Output is correct |
36 |
Correct |
53 ms |
2224 KB |
Output is correct |
37 |
Correct |
586 ms |
2096 KB |
Output is correct |
38 |
Correct |
33 ms |
2224 KB |
Output is correct |
39 |
Correct |
66 ms |
1828 KB |
Output is correct |
40 |
Correct |
6 ms |
1828 KB |
Output is correct |
41 |
Correct |
66 ms |
1960 KB |
Output is correct |
42 |
Correct |
9 ms |
1828 KB |
Output is correct |
43 |
Correct |
13 ms |
1960 KB |
Output is correct |
44 |
Correct |
33 ms |
2224 KB |
Output is correct |
45 |
Correct |
173 ms |
1960 KB |
Output is correct |
46 |
Execution timed out |
1500 ms |
2872 KB |
Execution timed out |
47 |
Correct |
26 ms |
2224 KB |
Output is correct |
48 |
Correct |
29 ms |
2224 KB |
Output is correct |
49 |
Execution timed out |
1500 ms |
2896 KB |
Execution timed out |
50 |
Correct |
63 ms |
1828 KB |
Output is correct |