Submission #91384

# Submission time Handle Problem Language Result Execution time Memory
91384 2018-12-27T09:45:30 Z mirbek01 Marriage questions (IZhO14_marriage) C++11
34 / 100
1500 ms 12424 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 > R || to < L) && to <= n)
                  continue;
            if(mt[to] == -1 || dfs(mt[to])){
                  mt[to] = v;
                  mt[v] = to;
                  return true;
            }
      }
      return false;
}

bool check(){
      if(mt[L] == -1)
            return true;
      while(!mr.empty()){
            int v = mr.back();
            for(int i = 0; i < vr.size(); i ++){
                  used[vr[i]] = 0;
            }
            if(!dfs(v)){
                  break;
            }
            mr.pop_back();
      }
      return mr.empty();
}

bool ok(){
      for(int i = n + 1; i <= n + m; i ++){
            if(mt[i] != -1){
                  mt[ mt[i] ] = -1;
                  mt[i] = -1;
            }
      }
      for(int i = n + 1; i <= n + m; i ++){
            for(int j = 0; j < (int)vr.size(); j ++){
                  used[ vr[j] ] = 0;
            }
            vr.clear();
            dfs(i);
      }
      for(int i = n + 1; i <= n + m; i ++){
            if(mt[i] == -1)
                  return false;
      }
      return true;
}

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].begin(), g[i].end());
      }

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

      int lo = 1, hi = n, pos = -1;

      L = 1;

      while(hi - lo > 1){
            int md = (lo + hi) >> 1;
            R = md;
            if(ok())
                  hi = md - 1, pos = md;
            else
                  lo = md + 1;
      }

      if(pos == -1){
            cout << 0 << endl;
            return 0;
      }

      ans += n - pos + 1;

      if(mt[L] != -1){
            mt[mt[L]] = -1;
            mr.push_back(mt[L]);
      }

      int pt = pos;

      for(int i = 2; i <= n - m + 1; i ++){
            int lo = 1, hi = 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:32:30: 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:108:17: warning: unused variable 'lo' [-Wunused-variable]
             int lo = 1, hi = i;
                 ^~
marriage.cpp:108:25: warning: unused variable 'hi' [-Wunused-variable]
             int lo = 1, hi = i;
                         ^~
marriage.cpp:65: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:69: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 Incorrect 4 ms 3064 KB Output isn't correct
2 Incorrect 4 ms 3248 KB Output isn't correct
3 Correct 4 ms 3248 KB Output is correct
4 Correct 4 ms 3248 KB Output is correct
5 Correct 4 ms 3248 KB Output is correct
6 Correct 3 ms 3248 KB Output is correct
7 Incorrect 4 ms 3248 KB Output isn't correct
8 Incorrect 3 ms 3248 KB Output isn't correct
9 Correct 4 ms 3376 KB Output is correct
10 Correct 4 ms 3376 KB Output is correct
11 Incorrect 4 ms 3376 KB Output isn't correct
12 Correct 3 ms 3376 KB Output is correct
13 Incorrect 3 ms 3376 KB Output isn't correct
14 Incorrect 4 ms 3376 KB Output isn't correct
15 Incorrect 4 ms 3376 KB Output isn't correct
16 Incorrect 4 ms 3376 KB Output isn't correct
17 Incorrect 3 ms 3376 KB Output isn't correct
18 Incorrect 3 ms 3376 KB Output isn't correct
19 Incorrect 5 ms 3396 KB Output isn't correct
20 Incorrect 4 ms 3396 KB Output isn't correct
21 Correct 4 ms 3396 KB Output is correct
22 Incorrect 4 ms 3396 KB Output isn't correct
23 Incorrect 4 ms 3396 KB Output isn't correct
24 Incorrect 4 ms 3396 KB Output isn't correct
25 Incorrect 43 ms 4060 KB Output isn't correct
26 Incorrect 14 ms 4060 KB Output isn't correct
27 Correct 4 ms 4060 KB Output is correct
28 Incorrect 4 ms 4060 KB Output isn't correct
29 Correct 6 ms 4060 KB Output is correct
30 Correct 6 ms 4060 KB Output is correct
31 Incorrect 374 ms 6168 KB Output isn't correct
32 Correct 13 ms 6168 KB Output is correct
33 Correct 4 ms 6168 KB Output is correct
34 Incorrect 19 ms 6168 KB Output isn't correct
35 Incorrect 176 ms 6168 KB Output isn't correct
36 Incorrect 140 ms 6168 KB Output isn't correct
37 Execution timed out 1561 ms 12424 KB Time limit exceeded
38 Correct 31 ms 12424 KB Output is correct
39 Incorrect 377 ms 12424 KB Output isn't correct
40 Correct 8 ms 12424 KB Output is correct
41 Incorrect 147 ms 12424 KB Output isn't correct
42 Correct 12 ms 12424 KB Output is correct
43 Correct 17 ms 12424 KB Output is correct
44 Incorrect 27 ms 12424 KB Output isn't correct
45 Incorrect 211 ms 12424 KB Output isn't correct
46 Execution timed out 1565 ms 12424 KB Time limit exceeded
47 Incorrect 31 ms 12424 KB Output isn't correct
48 Incorrect 29 ms 12424 KB Output isn't correct
49 Execution timed out 1570 ms 12424 KB Time limit exceeded
50 Execution timed out 1563 ms 12424 KB Time limit exceeded