Submission #95442

# Submission time Handle Problem Language Result Execution time Memory
95442 2019-02-01T08:23:13 Z choikiwon Marriage questions (IZhO14_marriage) C++17
54 / 100
1500 ms 5864 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 && !chk[u]) {
        chk[u] = 1;
        return true;
    }

    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(vis[v]) continue;
        if(matched[e] == t && dfs(v, t ^ 1, lb, ub)) {
            if(!t) {
                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 < N + M; k++) vis[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;
            matched[ who[i] ] = 0;
            chk[v] = 0;
            for(int k = 0; k < N + M; k++) vis[k] = 0;
            if(dfs(v, 0, i + 1, j)) {
                matching++;
                chk[v] = 1;
            }
        }
    }
    printf("%lld", ans);
}

Compilation message

marriage.cpp: In function 'bool dfs(int, int, int, int)':
marriage.cpp:19:22: 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:37: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:40: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 4 ms 2680 KB Output isn't correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Correct 3 ms 2680 KB Output is correct
6 Correct 4 ms 2680 KB Output is correct
7 Incorrect 4 ms 2680 KB Output isn't 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 Incorrect 3 ms 2684 KB Output isn't correct
14 Incorrect 5 ms 2684 KB Output isn't correct
15 Incorrect 3 ms 2680 KB Output isn't correct
16 Correct 3 ms 2680 KB Output is correct
17 Incorrect 3 ms 2680 KB Output isn't correct
18 Incorrect 3 ms 2680 KB Output isn't correct
19 Incorrect 6 ms 2808 KB Output isn't correct
20 Incorrect 4 ms 2680 KB Output isn't correct
21 Correct 4 ms 2680 KB Output is correct
22 Correct 4 ms 2680 KB Output is correct
23 Incorrect 4 ms 2680 KB Output isn't correct
24 Incorrect 4 ms 2680 KB Output isn't correct
25 Incorrect 22 ms 3064 KB Output isn't correct
26 Incorrect 14 ms 2808 KB Output isn't correct
27 Correct 4 ms 2680 KB Output is correct
28 Correct 4 ms 2680 KB Output is correct
29 Correct 17 ms 2936 KB Output is correct
30 Correct 15 ms 2936 KB Output is correct
31 Incorrect 373 ms 3984 KB Output isn't correct
32 Correct 50 ms 2856 KB Output is correct
33 Correct 4 ms 2808 KB Output is correct
34 Correct 9 ms 2812 KB Output is correct
35 Incorrect 253 ms 4980 KB Output isn't correct
36 Incorrect 226 ms 4840 KB Output isn't correct
37 Incorrect 1174 ms 4336 KB Output isn't correct
38 Correct 435 ms 5300 KB Output is correct
39 Correct 135 ms 3320 KB Output is correct
40 Correct 58 ms 3192 KB Output is correct
41 Correct 548 ms 3836 KB Output is correct
42 Correct 1329 ms 4216 KB Output is correct
43 Execution timed out 1563 ms 4332 KB Time limit exceeded
44 Execution timed out 1571 ms 5224 KB Time limit exceeded
45 Correct 521 ms 4344 KB Output is correct
46 Execution timed out 1570 ms 5580 KB Time limit exceeded
47 Execution timed out 1546 ms 5512 KB Time limit exceeded
48 Execution timed out 1567 ms 5528 KB Time limit exceeded
49 Execution timed out 1575 ms 5864 KB Time limit exceeded
50 Correct 482 ms 3448 KB Output is correct