Submission #37928

# Submission time Handle Problem Language Result Execution time Memory
37928 2017-12-29T06:12:01 Z mirbek01 Marriage questions (IZhO14_marriage) C++14
58 / 100
1500 ms 34980 KB
# include <bits/stdc++.h>

# define pb push_back
# define fr first
# define sc second
# define mk make_pair

using namespace std;

const int inf = 1e9 + 7;
const int N = 1e6 + 5;

typedef long long ll;

int n, m, k, used[N], mt[N], lo, ri;
ll ans;
vector <int> g[N];

bool try_kuhn(int v)
{
      if(used[v]) return false;
      used[v] = 1;
      for(int to : g[v])
      {
            if((mt[to] == -1 || try_kuhn(mt[to])) && to >= lo && to <= ri)
            {
                  mt[to] = v;
                  return true;
            }
      }
      return false;
}

bool check(int l, int r)
{
      for(int i = 1; i < l; i ++)
            used[i] = 1;
      for(int i = r + 1; i <= n; i ++)
            used[i] = 1;
      for(int i = 1; i <= n + m; i ++)
            mt[i] = -1;
      lo = l, ri = r;
      for(int i = 1; i <= m; i ++)
      {
            for(int j = l; j <= r; j ++)
                  used[j] = 0;
            for(int j = 1; j <= m; j ++)
                  used[j + n] = 0;
            try_kuhn(i + n);
      }

      for(int i = l; i <= r; i ++)
      {
            if(mt[i] == -1) continue;
            mt[mt[i]] = 1;
      }

      for(int i = 1; i <= m; i ++)
            if(mt[i + n] == -1) return false;

      return true;
}

int main()
{
      cin >> n >> m >> k;

      for(int i = 1; i <= k; i ++)
      {
            int a, b;
            cin >> a >> b;
            g[a].pb(n + b);
            g[n + b].pb(a);
      }

      for(int i = 1; i <= n; i ++)
      {
            int l = i, r = n;
            while(r - l > 1)
            {
                  int md = (l + r) >> 1;
                  if(check(i, md))
                        r = md;
                  else
                        l = md;
            }

            if(check(i, l))
                  r = l;
            if(check(i, r))
                  ans += (n - r + 1);
      }

      cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 33264 KB Output is correct
2 Correct 13 ms 33264 KB Output is correct
3 Correct 16 ms 33264 KB Output is correct
4 Correct 9 ms 33264 KB Output is correct
5 Correct 3 ms 33264 KB Output is correct
6 Correct 6 ms 33264 KB Output is correct
7 Correct 9 ms 33264 KB Output is correct
8 Correct 3 ms 33264 KB Output is correct
9 Correct 3 ms 33264 KB Output is correct
10 Correct 9 ms 33264 KB Output is correct
11 Correct 9 ms 33264 KB Output is correct
12 Correct 3 ms 33264 KB Output is correct
13 Correct 3 ms 33264 KB Output is correct
14 Correct 3 ms 33264 KB Output is correct
15 Correct 6 ms 33264 KB Output is correct
16 Correct 6 ms 33264 KB Output is correct
17 Correct 3 ms 33264 KB Output is correct
18 Correct 3 ms 33264 KB Output is correct
19 Correct 79 ms 33264 KB Output is correct
20 Correct 26 ms 33264 KB Output is correct
21 Correct 9 ms 33264 KB Output is correct
22 Correct 16 ms 33264 KB Output is correct
23 Correct 49 ms 33264 KB Output is correct
24 Correct 33 ms 33264 KB Output is correct
25 Execution timed out 1500 ms 33396 KB Execution timed out
26 Correct 436 ms 33264 KB Output is correct
27 Correct 143 ms 33264 KB Output is correct
28 Correct 249 ms 33264 KB Output is correct
29 Correct 503 ms 33396 KB Output is correct
30 Correct 356 ms 33396 KB Output is correct
31 Execution timed out 1500 ms 33792 KB Execution timed out
32 Execution timed out 1500 ms 33396 KB Execution timed out
33 Execution timed out 1500 ms 33264 KB Execution timed out
34 Execution timed out 1500 ms 33264 KB Execution timed out
35 Execution timed out 1500 ms 34320 KB Execution timed out
36 Execution timed out 1500 ms 34320 KB Execution timed out
37 Execution timed out 1500 ms 33924 KB Execution timed out
38 Execution timed out 1500 ms 34584 KB Execution timed out
39 Execution timed out 1500 ms 33396 KB Execution timed out
40 Execution timed out 1500 ms 33528 KB Execution timed out
41 Execution timed out 1500 ms 33660 KB Execution timed out
42 Execution timed out 1500 ms 33792 KB Execution timed out
43 Execution timed out 1500 ms 34056 KB Execution timed out
44 Execution timed out 1500 ms 34452 KB Execution timed out
45 Execution timed out 1500 ms 34056 KB Execution timed out
46 Execution timed out 1500 ms 34848 KB Execution timed out
47 Execution timed out 1500 ms 34716 KB Execution timed out
48 Execution timed out 1500 ms 34716 KB Execution timed out
49 Execution timed out 1500 ms 34980 KB Execution timed out
50 Execution timed out 1500 ms 33396 KB Execution timed out