답안 #91515

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
91515 2018-12-28T03:21:13 Z mirbek01 결혼 문제 (IZhO14_marriage) C++11
100 / 100
271 ms 4120 KB
# include <bits/stdc++.h>

using namespace std;

const int N = 33002;

int n, m, k, mt[N], cnt, used[N], ans, L, R, pt = 1;
vector <int> g[N], gr[N], mr;

bool dfs(int v){
      if(used[v] == cnt)
            return 0;
      used[v] = cnt;
      for(int i = (int)g[v].size() - 1; i >= 0; i --){
            int to = g[v][i];
            if(to < L) break;
            if(mt[to] == -1 || dfs(mt[to])){
                  mt[to] = v;
                  mt[v] = to;
                  return true;
            }
      }
      return false;
}

bool check(){
      while(!mr.empty()){
            int v = mr.back();
            if(mt[v] == -1){
                  cnt ++;
                  if(!dfs(v))
                        return false;
            }
            mr.pop_back();
      }
      return mr.empty();
}

int main(){
      scanf("%d %d %d", &n, &m, &k);

      for(int i = 1; i <= k; i ++){
            int a, b;
            scanf("%d %d", &a, &b);
            gr[a].push_back(b + n);
      }

      memset(mt, -1, sizeof(mt));

      for(int i = n + 1; i <= n + m; i ++)
            mr.push_back(i);

      for(int j = 0; j < gr[1].size(); j ++){
            int v = gr[1][j];
            g[v].push_back(1);
      }

      for(int i = 1; i <= n; i ++){
            L = i, R = pt;
            while(pt <= n && !check()){
                  pt ++, R = pt;
                  for(int j = 0; j < gr[pt].size(); j ++){
                        int v = gr[pt][j];
                        g[v].push_back(pt);
                  }
            }
            ans += n - pt + 1;
            if(mt[L] != -1){
                  mr.push_back(mt[L]);
                  mt[mt[L]] = -1;
            }
      }

      printf("%d", ans);
}

Compilation message

marriage.cpp: In function 'int main()':
marriage.cpp:53:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int j = 0; j < gr[1].size(); j ++){
                      ~~^~~~~~~~~~~~~~
marriage.cpp:62:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                   for(int j = 0; j < gr[pt].size(); j ++){
                                  ~~^~~~~~~~~~~~~~~
marriage.cpp:40:12: 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:44:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %d", &a, &b);
             ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2040 KB Output is correct
2 Correct 3 ms 2164 KB Output is correct
3 Correct 3 ms 2164 KB Output is correct
4 Correct 3 ms 2164 KB Output is correct
5 Correct 3 ms 2276 KB Output is correct
6 Correct 3 ms 2276 KB Output is correct
7 Correct 3 ms 2276 KB Output is correct
8 Correct 3 ms 2276 KB Output is correct
9 Correct 3 ms 2276 KB Output is correct
10 Correct 3 ms 2276 KB Output is correct
11 Correct 3 ms 2276 KB Output is correct
12 Correct 3 ms 2276 KB Output is correct
13 Correct 3 ms 2276 KB Output is correct
14 Correct 3 ms 2276 KB Output is correct
15 Correct 3 ms 2276 KB Output is correct
16 Correct 3 ms 2280 KB Output is correct
17 Correct 3 ms 2280 KB Output is correct
18 Correct 3 ms 2280 KB Output is correct
19 Correct 4 ms 2388 KB Output is correct
20 Correct 3 ms 2412 KB Output is correct
21 Correct 3 ms 2412 KB Output is correct
22 Correct 3 ms 2412 KB Output is correct
23 Correct 3 ms 2412 KB Output is correct
24 Correct 3 ms 2412 KB Output is correct
25 Correct 11 ms 2428 KB Output is correct
26 Correct 5 ms 2428 KB Output is correct
27 Correct 3 ms 2428 KB Output is correct
28 Correct 3 ms 2428 KB Output is correct
29 Correct 5 ms 2428 KB Output is correct
30 Correct 5 ms 2428 KB Output is correct
31 Correct 93 ms 2940 KB Output is correct
32 Correct 5 ms 2940 KB Output is correct
33 Correct 3 ms 2940 KB Output is correct
34 Correct 5 ms 2940 KB Output is correct
35 Correct 153 ms 3420 KB Output is correct
36 Correct 125 ms 3436 KB Output is correct
37 Correct 252 ms 3436 KB Output is correct
38 Correct 25 ms 3580 KB Output is correct
39 Correct 90 ms 3580 KB Output is correct
40 Correct 7 ms 3580 KB Output is correct
41 Correct 10 ms 3580 KB Output is correct
42 Correct 11 ms 3580 KB Output is correct
43 Correct 15 ms 3580 KB Output is correct
44 Correct 24 ms 3580 KB Output is correct
45 Correct 16 ms 3580 KB Output is correct
46 Correct 34 ms 3964 KB Output is correct
47 Correct 27 ms 3964 KB Output is correct
48 Correct 24 ms 3964 KB Output is correct
49 Correct 42 ms 4120 KB Output is correct
50 Correct 271 ms 4120 KB Output is correct