Submission #95443

# Submission time Handle Problem Language Result Execution time Memory
95443 2019-02-01T08:26:33 Z choikiwon Marriage questions (IZhO14_marriage) C++17
86 / 100
1500 ms 5952 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;
            chk[v] = 0;
            for(int k = 0; k < N + M; k++) vis[k] = 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: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 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2684 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 Correct 3 ms 2680 KB Output is correct
8 Correct 3 ms 2680 KB Output is correct
9 Correct 3 ms 2680 KB Output is correct
10 Correct 3 ms 2652 KB Output is correct
11 Correct 3 ms 2684 KB Output is correct
12 Correct 3 ms 2680 KB Output is correct
13 Correct 3 ms 2680 KB Output is correct
14 Correct 3 ms 2680 KB Output is correct
15 Correct 4 ms 2680 KB Output is correct
16 Correct 10 ms 2684 KB Output is correct
17 Correct 4 ms 2680 KB Output is correct
18 Correct 4 ms 2680 KB Output is correct
19 Correct 6 ms 2808 KB Output is correct
20 Correct 4 ms 2708 KB Output is correct
21 Correct 3 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 57 ms 3128 KB Output is correct
26 Correct 17 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 18 ms 2936 KB Output is correct
30 Correct 15 ms 2936 KB Output is correct
31 Correct 423 ms 3952 KB Output is correct
32 Correct 51 ms 2936 KB Output is correct
33 Correct 4 ms 2808 KB Output is correct
34 Correct 8 ms 2808 KB Output is correct
35 Correct 260 ms 5092 KB Output is correct
36 Correct 202 ms 4968 KB Output is correct
37 Execution timed out 1519 ms 4356 KB Time limit exceeded
38 Correct 432 ms 5288 KB Output is correct
39 Correct 127 ms 3268 KB Output is correct
40 Correct 60 ms 3192 KB Output is correct
41 Correct 566 ms 3704 KB Output is correct
42 Correct 1163 ms 4088 KB Output is correct
43 Execution timed out 1579 ms 4524 KB Time limit exceeded
44 Execution timed out 1590 ms 5192 KB Time limit exceeded
45 Correct 522 ms 4344 KB Output is correct
46 Execution timed out 1570 ms 5708 KB Time limit exceeded
47 Execution timed out 1566 ms 5708 KB Time limit exceeded
48 Execution timed out 1572 ms 5476 KB Time limit exceeded
49 Execution timed out 1567 ms 5952 KB Time limit exceeded
50 Correct 474 ms 3464 KB Output is correct