답안 #90680

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
90680 2018-12-23T13:43:08 Z popovicirobert 결혼 문제 (IZhO14_marriage) C++14
96 / 100
1500 ms 7008 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define ld long double
// 217
// 44

using namespace std;

const int MAXN = 30000;
const int MAXM = 2000;

struct Match {
    unordered_set <int> g[MAXM + 1];
    vector <int> vis, l, r;
    int n, now;
    int ans;
    inline void init(int _n) {
        n = _n;
        l.resize(n + 1);
        r.resize(n + 1);
        vis.resize(n + 1);
        ans = now = 0;
    }
    inline void addEdge(int x, int y) {
        g[x].insert(y);
    }
    inline void delEdge(int x, int y) {
        g[x].erase(g[x].find(y));
        if(l[x] == y) {
            l[x] = r[y] = 0;
            ans--;
        }
    }
    bool match(int nod) {
        for(auto it : g[nod]) {
            if(r[it] == 0) {
                l[nod] = it;
                r[it] = nod;
                return 1;
            }
        }
        vis[nod] = now;
        for(auto it : g[nod]) {
            if(vis[r[it]] != now && match(r[it])) {
                l[nod] = it;
                r[it] = nod;
                return 1;
            }
        }
        return 0;
    }
    inline int solve(int m) {
        bool flag = 1;
        int i;
        while(flag && ans < m) {
            flag = 0;
            now++;
            for(i = 1; i <= m; i++) {
                if(l[i] == 0) {
                    flag |= match(i);
                    ans += (l[i] > 0);
                }
            }
        }
        return ans;
    }
}M;

vector <int> g[MAXN + 1];

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i, n, m, k;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n >> m >> k;
    for(i = 1; i <= k; i++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
    }
    M.init(n + m);
    int pos = 1, mx = 0;
    int ans = 0;
    for(i = 1; i <= n; i++) {
        bool ok = 0;
        while(M.solve(m) < m && pos <= n) {
            if(mx < pos) {
                for(auto it : g[pos]) {
                    M.addEdge(it, pos + m);
                }
            }
            mx = max(mx, pos);
            pos++;
            ok = 1;
        }
        pos -= ok;
        if(M.ans == m) {
            ans += n - pos + 1;
        }
        for(auto it : g[i]) {
            M.delEdge(it, i + m);
        }
    }
    cout << ans;
    //cin.close();
    //cout.close();
    return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1144 KB Output is correct
2 Correct 2 ms 1276 KB Output is correct
3 Correct 2 ms 1276 KB Output is correct
4 Correct 2 ms 1276 KB Output is correct
5 Correct 2 ms 1316 KB Output is correct
6 Correct 2 ms 1316 KB Output is correct
7 Correct 2 ms 1320 KB Output is correct
8 Correct 2 ms 1332 KB Output is correct
9 Correct 2 ms 1348 KB Output is correct
10 Correct 2 ms 1348 KB Output is correct
11 Correct 2 ms 1348 KB Output is correct
12 Correct 2 ms 1348 KB Output is correct
13 Correct 2 ms 1348 KB Output is correct
14 Correct 2 ms 1348 KB Output is correct
15 Correct 2 ms 1348 KB Output is correct
16 Correct 2 ms 1348 KB Output is correct
17 Correct 2 ms 1348 KB Output is correct
18 Correct 2 ms 1348 KB Output is correct
19 Correct 5 ms 1476 KB Output is correct
20 Correct 3 ms 1476 KB Output is correct
21 Correct 2 ms 1476 KB Output is correct
22 Correct 3 ms 1476 KB Output is correct
23 Correct 3 ms 1476 KB Output is correct
24 Correct 3 ms 1476 KB Output is correct
25 Correct 31 ms 1632 KB Output is correct
26 Correct 6 ms 1632 KB Output is correct
27 Correct 3 ms 1632 KB Output is correct
28 Correct 3 ms 1632 KB Output is correct
29 Correct 5 ms 1888 KB Output is correct
30 Correct 5 ms 1888 KB Output is correct
31 Execution timed out 1579 ms 2912 KB Time limit exceeded
32 Correct 31 ms 2912 KB Output is correct
33 Correct 7 ms 2912 KB Output is correct
34 Correct 12 ms 2912 KB Output is correct
35 Correct 925 ms 6268 KB Output is correct
36 Correct 688 ms 6268 KB Output is correct
37 Execution timed out 1580 ms 6268 KB Time limit exceeded
38 Correct 54 ms 6496 KB Output is correct
39 Correct 262 ms 6496 KB Output is correct
40 Correct 157 ms 6496 KB Output is correct
41 Correct 184 ms 6496 KB Output is correct
42 Correct 91 ms 6496 KB Output is correct
43 Correct 98 ms 6496 KB Output is correct
44 Correct 190 ms 6496 KB Output is correct
45 Correct 230 ms 6496 KB Output is correct
46 Correct 473 ms 6496 KB Output is correct
47 Correct 324 ms 7008 KB Output is correct
48 Correct 339 ms 7008 KB Output is correct
49 Correct 591 ms 7008 KB Output is correct
50 Correct 681 ms 7008 KB Output is correct