답안 #5031

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
5031 2014-01-28T15:10:08 Z cki86201 결혼 문제 (IZhO14_marriage) C++
96 / 100
1500 ms 2780 KB
#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;

bool match(int x)
{
	priority_queue <Pi, vector<Pi>, greater<Pi> >pq;
	for(size_t i=0;i<E[x].size();i++){
		if(E[x][i] > now){
			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=0;i<E[v].size();i++){
			if(chk[E[v][i]] == in || E[v][i] <= now)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++)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];
		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:56: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:59: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 1580 KB Output is correct
2 Correct 0 ms 1580 KB Output is correct
3 Correct 0 ms 1580 KB Output is correct
4 Correct 0 ms 1580 KB Output is correct
5 Correct 0 ms 1580 KB Output is correct
6 Correct 0 ms 1580 KB Output is correct
7 Correct 0 ms 1580 KB Output is correct
8 Correct 0 ms 1580 KB Output is correct
9 Correct 0 ms 1580 KB Output is correct
10 Correct 0 ms 1580 KB Output is correct
11 Correct 0 ms 1580 KB Output is correct
12 Correct 0 ms 1580 KB Output is correct
13 Correct 0 ms 1580 KB Output is correct
14 Correct 0 ms 1580 KB Output is correct
15 Correct 0 ms 1580 KB Output is correct
16 Correct 0 ms 1580 KB Output is correct
17 Correct 0 ms 1580 KB Output is correct
18 Correct 0 ms 1580 KB Output is correct
19 Correct 3 ms 1580 KB Output is correct
20 Correct 0 ms 1580 KB Output is correct
21 Correct 0 ms 1580 KB Output is correct
22 Correct 0 ms 1580 KB Output is correct
23 Correct 0 ms 1580 KB Output is correct
24 Correct 0 ms 1580 KB Output is correct
25 Correct 43 ms 1712 KB Output is correct
26 Correct 13 ms 1580 KB Output is correct
27 Correct 0 ms 1580 KB Output is correct
28 Correct 0 ms 1580 KB Output is correct
29 Correct 6 ms 1712 KB Output is correct
30 Correct 3 ms 1712 KB Output is correct
31 Correct 266 ms 1844 KB Output is correct
32 Correct 36 ms 1712 KB Output is correct
33 Correct 0 ms 1580 KB Output is correct
34 Correct 3 ms 1580 KB Output is correct
35 Correct 89 ms 2108 KB Output is correct
36 Correct 79 ms 2108 KB Output is correct
37 Correct 1008 ms 1980 KB Output is correct
38 Correct 36 ms 2108 KB Output is correct
39 Correct 63 ms 1712 KB Output is correct
40 Correct 6 ms 1712 KB Output is correct
41 Correct 53 ms 1844 KB Output is correct
42 Correct 6 ms 1712 KB Output is correct
43 Correct 13 ms 1844 KB Output is correct
44 Correct 26 ms 2108 KB Output is correct
45 Correct 99 ms 1844 KB Output is correct
46 Execution timed out 1500 ms 2756 KB Execution timed out
47 Correct 29 ms 2108 KB Output is correct
48 Correct 39 ms 2108 KB Output is correct
49 Execution timed out 1500 ms 2780 KB Execution timed out
50 Correct 66 ms 1712 KB Output is correct