Submission #95449

# Submission time Handle Problem Language Result Execution time Memory
95449 2019-02-01T08:43:31 Z choikiwon Marriage questions (IZhO14_marriage) C++17
92 / 100
1500 ms 6048 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MN = 100010;

int N, M, K;
vector<int> adj[MN], U, V;
int matched[MN], chk[MN], who[MN], vis[MN];

bool dfs(int u, int t, int lb, int ub) {
    vis[u] = 1;
    if(t) {
        if(!chk[u]) {
            chk[u] = 1;
            return true;
        }

        int e = who[u];
        int v = U[e] + V[e] - u;

        if(dfs(v, 0, lb, ub)) {
            matched[e] = 0;
            return true;
        }
        else return false;
    }
    else {
        for(int i = 0; i < adj[u].size(); i++) {
            int e = adj[u][i];
            int v = U[e] + V[e] - u;
            if(v < N && (v < lb || ub < v)) continue;
            if(chk[v] && vis[v]) continue;
            if(!matched[e] && dfs(v, 1, lb, ub)) {
                who[u] = e;
                who[v] = e;
                matched[e] = 1;
                return true;
            }
        }
        return false;
    }
}

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(i);
        adj[b + N].push_back(i);
        U.push_back(a);
        V.push_back(b + N);
    }

    ll ans = 0;
    int matching = 0;
    int j = 0;
    for(int i = 0; i < N; i++) {
        while(j < N && matching < M) {
            for(int k = 0; k < M; k++) vis[N + k] = 0;
            if(dfs(j, 0, i, j)) {
                matching++;
                chk[j] = 1;
            }
            j++;
        }
        if(matching < M) break;
        ans += N - j + 1;

        if(chk[i]) {
            matching--;
            int v = U[ who[i] ] + V[ who[i] ] - i;
            chk[v] = 0;
            for(int k = 0; k < M; k++) if(chk[N + k]) {
                int e = who[N + k];
                int x = U[e] + V[e] - (N + k);
                vis[x] = 0;
            }
            if(dfs(v, 0, i + 1, j - 1)) {
                matching++;
                chk[v] = 1;
            }
        }
    }
    printf("%lld", ans);
}

Compilation message

marriage.cpp: In function 'bool dfs(int, int, int, int)':
marriage.cpp:30: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:47: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:50: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 Correct 3 ms 2680 KB Output is correct
2 Correct 3 ms 2680 KB Output is correct
3 Correct 3 ms 2680 KB Output is correct
4 Correct 3 ms 2680 KB Output is correct
5 Correct 3 ms 2680 KB Output is correct
6 Correct 3 ms 2680 KB Output is correct
7 Correct 4 ms 2680 KB Output is correct
8 Correct 4 ms 2680 KB Output is correct
9 Correct 4 ms 2680 KB Output is correct
10 Correct 4 ms 2680 KB Output is correct
11 Correct 4 ms 2680 KB Output is correct
12 Correct 4 ms 2680 KB Output is correct
13 Correct 4 ms 2680 KB Output is correct
14 Correct 4 ms 2680 KB Output is correct
15 Correct 4 ms 2680 KB Output is correct
16 Correct 4 ms 2680 KB Output is correct
17 Correct 4 ms 2680 KB Output is correct
18 Correct 4 ms 2636 KB Output is correct
19 Correct 5 ms 2680 KB Output is correct
20 Correct 4 ms 2680 KB Output is correct
21 Correct 4 ms 2680 KB Output is correct
22 Correct 4 ms 2680 KB Output is correct
23 Correct 4 ms 2680 KB Output is correct
24 Correct 4 ms 2680 KB Output is correct
25 Correct 49 ms 3064 KB Output is correct
26 Correct 9 ms 2808 KB Output is correct
27 Correct 4 ms 2680 KB Output is correct
28 Correct 4 ms 2680 KB Output is correct
29 Correct 9 ms 2936 KB Output is correct
30 Correct 8 ms 2936 KB Output is correct
31 Correct 339 ms 4084 KB Output is correct
32 Correct 22 ms 2936 KB Output is correct
33 Correct 4 ms 2808 KB Output is correct
34 Correct 7 ms 2808 KB Output is correct
35 Correct 157 ms 5148 KB Output is correct
36 Correct 121 ms 4816 KB Output is correct
37 Correct 1189 ms 4144 KB Output is correct
38 Correct 222 ms 5348 KB Output is correct
39 Correct 60 ms 3196 KB Output is correct
40 Correct 16 ms 3320 KB Output is correct
41 Correct 184 ms 3548 KB Output is correct
42 Correct 413 ms 4056 KB Output is correct
43 Correct 487 ms 4308 KB Output is correct
44 Correct 952 ms 5232 KB Output is correct
45 Correct 83 ms 4228 KB Output is correct
46 Execution timed out 1564 ms 5852 KB Time limit exceeded
47 Execution timed out 1584 ms 5880 KB Time limit exceeded
48 Execution timed out 1576 ms 5712 KB Time limit exceeded
49 Execution timed out 1574 ms 6048 KB Time limit exceeded
50 Correct 78 ms 3236 KB Output is correct