답안 #95441

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
95441 2019-02-01T08:22:06 Z choikiwon 결혼 문제 (IZhO14_marriage) C++17
48 / 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) {
                matched[e] = 1;
                who[u] = e;
                who[v] = e;
            }
            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);
                   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 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 3 ms 2552 KB Output is correct
12 Correct 4 ms 2680 KB Output is correct
13 Incorrect 3 ms 2680 KB Output isn't correct
14 Incorrect 3 ms 2680 KB Output isn't correct
15 Incorrect 3 ms 2684 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 5 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 3 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 30 ms 3112 KB Output isn't correct
26 Incorrect 13 ms 2940 KB Output isn't correct
27 Correct 4 ms 2680 KB Output is correct
28 Correct 3 ms 2680 KB Output is correct
29 Correct 17 ms 3064 KB Output is correct
30 Correct 14 ms 2936 KB Output is correct
31 Incorrect 577 ms 3952 KB Output isn't correct
32 Incorrect 39 ms 2936 KB Output isn't correct
33 Correct 4 ms 2684 KB Output is correct
34 Correct 6 ms 2808 KB Output is correct
35 Incorrect 298 ms 5192 KB Output isn't correct
36 Incorrect 235 ms 4968 KB Output isn't correct
37 Execution timed out 1520 ms 4208 KB Time limit exceeded
38 Correct 450 ms 5480 KB Output is correct
39 Correct 77 ms 3192 KB Output is correct
40 Correct 62 ms 3448 KB Output is correct
41 Incorrect 421 ms 3704 KB Output isn't correct
42 Correct 1264 ms 4100 KB Output is correct
43 Execution timed out 1566 ms 4212 KB Time limit exceeded
44 Execution timed out 1561 ms 5260 KB Time limit exceeded
45 Incorrect 498 ms 4472 KB Output isn't correct
46 Execution timed out 1554 ms 5608 KB Time limit exceeded
47 Execution timed out 1561 ms 5508 KB Time limit exceeded
48 Execution timed out 1565 ms 5436 KB Time limit exceeded
49 Execution timed out 1569 ms 5864 KB Time limit exceeded
50 Correct 403 ms 3192 KB Output is correct