Submission #95462

# Submission time Handle Problem Language Result Execution time Memory
95462 2019-02-01T11:36:34 Z choikiwon Marriage questions (IZhO14_marriage) C++17
58 / 100
373 ms 23828 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MN = 100010;

int V, E, src, snk;
vector<int> la, nxt, oppo, capa;
void init() {
    E = 0;
    la.clear(); nxt.clear(); oppo.clear(); capa.clear();
    la = vector<int>(V, -1);
}
void add(int u, int v, int c) {
    nxt.push_back(la[u]);
    la[u] = E++;
    oppo.push_back(v);
    capa.push_back(c);
}
vector<int> dist;
queue<int> q;
bool bfs() {
    dist = vector<int>(V, -1);
    q.push(src);
    dist[src] = 0;
    while(!q.empty()) {
        int u = q.front(); q.pop();

        for(int i = la[u]; i != -1; i = nxt[i]) {
            int v = oppo[i];
            if(capa[i] && dist[v] == -1) {
                dist[v] = dist[u] + 1;
                q.push(v);
            }
        }
    }
    return dist[snk] != -1;
}
vector<int> laa;
int dfs(int u, int f) {
    if(u == snk) return f;
    for(int i = laa[u]; i != -1; i = nxt[i]) {
        laa[u] = i;
        int v = oppo[i];
        if(capa[i] && dist[v] == dist[u] + 1) {
            if(int tmp = dfs(v, min(f, capa[i]))) {
                capa[i] -= tmp;
                capa[i ^ 1] += tmp;
                return tmp;
            }
        }
    }
    return 0;
}
int dinic() {
    int tf = 0;
    while(bfs()) {
        laa = la;
        while(int tmp = dfs(src, 1e9)) tf += tmp;
    }
    return tf;
}

int N, M, K;
vector<int> adj[MN];
unordered_map<int, int> chk[MN];
int who[MN];

void con(int l, int r) {
    V = N + M + 2, src = V - 2, snk = V - 1;
    init();
    for(int i = l; i <= r; i++) {
        add(src, i, 1);
        add(i, src, 0);
    }
    for(int i = 0; i < M; i++) {
        add(N + i, snk, 1);
        add(snk, N + i, 0);
    }
    for(int u = l; u <= r; u++) {
        for(int i = 0; i < adj[u].size(); i++) {
            int v = adj[u][i];

            add(u, v, 1);
            add(v, u, 0);
        }
    }
}

int main() {
    scanf("%d %d %d", &N, &M, &K);

    for(int i = 0; i < K; i++) {
        int a, b; scanf("%d %d", &a, &b);
        a--; b--;

        adj[a].push_back(N + b);
        adj[N + b].push_back(a);
        chk[a][N + b] = 1;
        chk[N + b][a] = 1;
    }
    for(int i = 0; i < M; i++) {
        sort(adj[N + i].begin(), adj[N + i].end());
        reverse(adj[N + i].begin(), adj[N + i].end());
    }

    int s = 0, e = N - 1, p = -1, q = -1;
    while(s <= e) {
        int m = (s + e)>>1;

        con(0, m);
        if(dinic() == M) {
            p = m;
            e = m - 1;
        }
        else s = m + 1;
    }
    if(p == -1) {
        printf("0");
        return 0;
    }

    con(0, p);
    dinic();

    s = 0, e = p;
    while(s <= e) {
        int m = (s + e)>>1;

        con(m, p);
        if(dinic() == M) {
            q = m;
            s = m + 1;
        }
        else e = m - 1;
    }
    con(q, p);
    dinic();

    memset(who, -1, sizeof(who));
    for(int u = q; u <= p; u++) {
        for(int i = la[u]; i != -1; i = nxt[i]) {
            int v = oppo[i];
            if(v != src && !capa[i]) {
                who[u] = v;
                who[v] = u;
                break;
            }
        }
    }

    ll ans = N - p;
    for(int i = 1; i < N; i++) {
        if(who[i - 1] != -1) {
            int u = who[i - 1];

            bool found = false;
            while(p + 1 < N) {
                ++p;

                bool ok = false;
                for(int j = 0; j < adj[p].size(); j++) {
                    int v = adj[p][j];
                    if(v == u) {
                        who[u] = p;
                        who[p] = u;
                        ok = true;
                        break;
                    }
                    int x = who[v];
                    if(chk[x][u]) {
                        who[u] = x;
                        who[x] = u;
                        who[v] = p;
                        who[p] = v;
                        ok = true;
                        break;
                    }
                }
                if(ok) {
                    found = true;
                    break;
                }
            }
            if(!found) break;
        }
        ans += N - p;
    }
    printf("%lld", ans);
}

Compilation message

marriage.cpp: In function 'void con(int, int)':
marriage.cpp:82:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0; i < adj[u].size(); i++) {
                        ~~^~~~~~~~~~~~~~~
marriage.cpp: In function 'int main()':
marriage.cpp:163:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 for(int j = 0; j < adj[p].size(); j++) {
                                ~~^~~~~~~~~~~~~~~
marriage.cpp:92:10: 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:95:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         int a, b; scanf("%d %d", &a, &b);
                   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 8696 KB Output isn't correct
2 Incorrect 9 ms 8568 KB Output isn't correct
3 Correct 9 ms 8568 KB Output is correct
4 Correct 9 ms 8568 KB Output is correct
5 Correct 10 ms 8568 KB Output is correct
6 Correct 11 ms 8568 KB Output is correct
7 Incorrect 9 ms 8568 KB Output isn't correct
8 Correct 9 ms 8568 KB Output is correct
9 Correct 8 ms 8188 KB Output is correct
10 Correct 8 ms 8184 KB Output is correct
11 Correct 9 ms 8568 KB Output is correct
12 Correct 9 ms 8184 KB Output is correct
13 Correct 9 ms 8564 KB Output is correct
14 Incorrect 9 ms 8568 KB Output isn't correct
15 Incorrect 9 ms 8568 KB Output isn't correct
16 Correct 9 ms 8568 KB Output is correct
17 Incorrect 9 ms 8568 KB Output isn't correct
18 Incorrect 9 ms 8568 KB Output isn't correct
19 Correct 12 ms 8824 KB Output is correct
20 Incorrect 10 ms 8568 KB Output isn't correct
21 Correct 9 ms 8184 KB Output is correct
22 Correct 9 ms 8568 KB Output is correct
23 Incorrect 10 ms 8696 KB Output isn't correct
24 Incorrect 11 ms 8572 KB Output isn't correct
25 Incorrect 23 ms 9992 KB Output isn't correct
26 Incorrect 13 ms 8952 KB Output isn't correct
27 Correct 8 ms 8312 KB Output is correct
28 Correct 10 ms 8568 KB Output is correct
29 Correct 23 ms 9976 KB Output is correct
30 Correct 18 ms 9848 KB Output is correct
31 Incorrect 84 ms 14396 KB Output isn't correct
32 Incorrect 22 ms 9336 KB Output isn't correct
33 Correct 9 ms 8312 KB Output is correct
34 Correct 10 ms 8824 KB Output is correct
35 Incorrect 111 ms 20696 KB Output isn't correct
36 Incorrect 106 ms 18348 KB Output isn't correct
37 Incorrect 117 ms 15348 KB Output isn't correct
38 Correct 178 ms 21176 KB Output is correct
39 Correct 21 ms 9564 KB Output is correct
40 Correct 52 ms 10284 KB Output is correct
41 Incorrect 116 ms 11764 KB Output isn't correct
42 Correct 87 ms 13176 KB Output is correct
43 Correct 107 ms 15840 KB Output is correct
44 Correct 172 ms 21204 KB Output is correct
45 Incorrect 154 ms 14268 KB Output isn't correct
46 Incorrect 259 ms 22372 KB Output isn't correct
47 Correct 242 ms 23128 KB Output is correct
48 Correct 217 ms 22100 KB Output is correct
49 Incorrect 373 ms 23828 KB Output isn't correct
50 Correct 31 ms 10364 KB Output is correct