제출 #95440

#제출 시각아이디문제언어결과실행 시간메모리
95440choikiwon결혼 문제 (IZhO14_marriage)C++17
46 / 100
1573 ms6808 KiB
#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);
}

컴파일 시 표준 에러 (stderr) 메시지

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 timeMemoryGrader output
Fetching results...