Submission #95440

# Submission time Handle Problem Language Result Execution time Memory
95440 2019-02-01T08:16:24 Z choikiwon Marriage questions (IZhO14_marriage) C++17
46 / 100
1500 ms 6808 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(dfs(v, t ^ 1, lb, ub)) {
            if(!t) {
                who[u] = v;
                who[v] = u;
            }
            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--;
            chk[ who[i] ] = 0;
            for(int k = 0; k < N + M; k++) vis[k] = 0;
            if(dfs(who[i], 0, i + 1, j)) {
                matching++;
                chk[ who[i] ] = 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:36: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:39: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 4 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 4 ms 2684 KB Output isn't correct
14 Incorrect 4 ms 2680 KB Output isn't correct
15 Incorrect 4 ms 2680 KB Output isn't correct
16 Correct 4 ms 2680 KB Output is correct
17 Incorrect 4 ms 2680 KB Output isn't correct
18 Incorrect 4 ms 2684 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 2684 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 2684 KB Output isn't correct
25 Incorrect 22 ms 3064 KB Output isn't correct
26 Incorrect 13 ms 2936 KB Output isn't correct
27 Correct 5 ms 2680 KB Output is correct
28 Correct 4 ms 2680 KB Output is correct
29 Correct 21 ms 2936 KB Output is correct
30 Correct 16 ms 3064 KB Output is correct
31 Incorrect 354 ms 4208 KB Output isn't correct
32 Incorrect 61 ms 3064 KB Output isn't correct
33 Correct 5 ms 2680 KB Output is correct
34 Correct 8 ms 2808 KB Output is correct
35 Incorrect 274 ms 5352 KB Output isn't correct
36 Incorrect 209 ms 5096 KB Output isn't correct
37 Incorrect 1155 ms 4456 KB Output isn't correct
38 Correct 517 ms 5860 KB Output is correct
39 Correct 124 ms 3320 KB Output is correct
40 Correct 1445 ms 3676 KB Output is correct
41 Execution timed out 1573 ms 4088 KB Time limit exceeded
42 Execution timed out 1569 ms 4344 KB Time limit exceeded
43 Execution timed out 1562 ms 4736 KB Time limit exceeded
44 Execution timed out 1555 ms 5944 KB Time limit exceeded
45 Execution timed out 1547 ms 4472 KB Time limit exceeded
46 Execution timed out 1567 ms 6464 KB Time limit exceeded
47 Execution timed out 1560 ms 6376 KB Time limit exceeded
48 Execution timed out 1567 ms 6324 KB Time limit exceeded
49 Execution timed out 1571 ms 6808 KB Time limit exceeded
50 Correct 458 ms 3328 KB Output is correct