Submission #91403

# Submission time Handle Problem Language Result Execution time Memory
91403 2018-12-27T11:17:25 Z mirbek01 Marriage questions (IZhO14_marriage) C++11
94 / 100
1500 ms 20204 KB
# include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 2;

int n, m, k, mt[N], used[N], ans, L, R;
vector <int> g[N], vr, mr;

bool dfs(int v){
      if(used[v])
            return 0;
      vr.push_back(v);
      used[v] = 1;
      for(int to : g[v]){
            if(to < L) break;
            if(to <= n && to > R)
                  continue;
            if(mt[to] == -1 || dfs(mt[to])){
                  mt[to] = v;
                  mt[v] = to;
                  return true;
            }
      }
      return false;
}

bool check(){
      if(mr.empty())
            return true;
      while(!mr.empty()){
            int v = mr.back();
            if(mt[v] == -1){
                  for(int i = 0; i < vr.size(); i ++){
                        used[vr[i]] = 0;
                  }
                  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);
            g[a].push_back(b + n);
            g[b + n].push_back(a);
      }

      for(int i = 1; i <= n + m; i ++){
            sort(g[i].rbegin(), g[i].rend());
      }

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

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

      int pt = 1;

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

      printf("%d", ans);
}

Compilation message

marriage.cpp: In function 'bool check()':
marriage.cpp:34:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                   for(int i = 0; i < vr.size(); i ++){
                                  ~~^~~~~~~~~~~
marriage.cpp: In function 'int main()':
marriage.cpp:46: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:50:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %d", &a, &b);
             ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3064 KB Output is correct
2 Correct 3 ms 3248 KB Output is correct
3 Correct 4 ms 3248 KB Output is correct
4 Correct 3 ms 3248 KB Output is correct
5 Correct 3 ms 3248 KB Output is correct
6 Correct 3 ms 3248 KB Output is correct
7 Correct 4 ms 3248 KB Output is correct
8 Correct 3 ms 3248 KB Output is correct
9 Correct 6 ms 3248 KB Output is correct
10 Correct 4 ms 3248 KB Output is correct
11 Correct 3 ms 3324 KB Output is correct
12 Correct 3 ms 3324 KB Output is correct
13 Correct 4 ms 3324 KB Output is correct
14 Correct 4 ms 3324 KB Output is correct
15 Correct 4 ms 3324 KB Output is correct
16 Correct 4 ms 3324 KB Output is correct
17 Correct 4 ms 3324 KB Output is correct
18 Correct 4 ms 3324 KB Output is correct
19 Correct 5 ms 3324 KB Output is correct
20 Correct 4 ms 3324 KB Output is correct
21 Correct 4 ms 3324 KB Output is correct
22 Correct 4 ms 3324 KB Output is correct
23 Correct 4 ms 3324 KB Output is correct
24 Correct 4 ms 3324 KB Output is correct
25 Correct 29 ms 3896 KB Output is correct
26 Correct 11 ms 3896 KB Output is correct
27 Correct 5 ms 3896 KB Output is correct
28 Correct 5 ms 3896 KB Output is correct
29 Correct 6 ms 3896 KB Output is correct
30 Correct 6 ms 3896 KB Output is correct
31 Correct 301 ms 6020 KB Output is correct
32 Correct 8 ms 6020 KB Output is correct
33 Correct 4 ms 6020 KB Output is correct
34 Correct 27 ms 6020 KB Output is correct
35 Correct 244 ms 6020 KB Output is correct
36 Correct 210 ms 6020 KB Output is correct
37 Execution timed out 1569 ms 12468 KB Time limit exceeded
38 Correct 50 ms 12468 KB Output is correct
39 Execution timed out 1583 ms 20028 KB Time limit exceeded
40 Correct 36 ms 20028 KB Output is correct
41 Correct 54 ms 20028 KB Output is correct
42 Correct 81 ms 20028 KB Output is correct
43 Correct 84 ms 20028 KB Output is correct
44 Correct 102 ms 20028 KB Output is correct
45 Correct 372 ms 20028 KB Output is correct
46 Correct 1047 ms 20028 KB Output is correct
47 Correct 297 ms 20028 KB Output is correct
48 Correct 298 ms 20028 KB Output is correct
49 Correct 945 ms 20028 KB Output is correct
50 Execution timed out 1580 ms 20204 KB Time limit exceeded