답안 #171832

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
171832 2019-12-30T13:10:21 Z VEGAnn 결혼 문제 (IZhO14_marriage) C++14
64 / 100
1500 ms 2932 KB
#include <bits/stdc++.h>
#define sz(x) ((us)x.size())
#define PB push_back
#define MP make_pair
#define ft first
#define sd second
#define pii pair<us, us>
#define pi3 pair<pii, us>
#define MP3(a,b,c) MP(MP(a, b), c)
using namespace std;
typedef unsigned short us;
const us oo = 32000;
const us N = 32100;
const us K = 110;
const us PW = 20;

struct edge{
    us y;
    bool psh;

    edge(us _y, bool _p): y(_y), psh(_p) {}
};

vector<edge> edges;
queue<us> q;
vector<us> g[N];
us ans = 0, s, t, n, m, k, dst[N], ptr[N];

void add_edge(us x, us y){

    g[x].PB(sz(edges));
    edges.PB(edge(y, 0));

    g[y].PB(sz(edges));
    edges.PB(edge(x, 1));

}

bool bad(us l, us r, us v){
    if (v == s || v == t || v < m) return 0;
    return !(m + l <= v && v <= m + r);
}

bool bfs(us l, us r){
    fill(dst, dst + t + 1, oo);

    dst[s] = 0;
    while (sz(q)) q.pop();
    q.push(s);

    while (sz(q)){
        us v = q.front(); q.pop();

        for (us nm : g[v]){
            edge u = edges[nm];
            if (!bad(l, r, u.y) && !u.psh && dst[u.y] > dst[v] + 1){
                dst[u.y] = dst[v] + 1;
                q.push(u.y);
            }
        }
    }

    return (dst[t] < oo);
}

bool dfs(us v, bool pshd){
    if (pshd == 0) return 0;
    if (v == t) return pshd;
    for (; ptr[v] < sz(g[v]); ptr[v]++){
        us nm = g[v][ptr[v]];
        edge u = edges[nm];
        if (dst[u.y] != dst[v] + 1) continue;
        us nw = dfs(u.y, min(pshd, !u.psh));
        if (nw){
            edges[nm].psh = 1;
            edges[nm ^ 1].psh = 0;
            return nw;
        }
    }
    return 0;
}

bool ok(us l, us r){

    if (r - l + 1 < m) return 0;

    us flow = 0;

    for (us i = 0; i < m; i++) {
        us nm = g[s][i];
        flow += edges[nm].psh;
    }

    while (bfs(l, r)){
        fill(ptr, ptr + t + 1, 0);
        us pshd = dfs(s, oo);
        while (pshd > 0){
            flow += pshd;
            pshd = dfs(s, oo);
        }
    }

    return (flow == m);
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

    cin >> n >> m >> k;

    for (us i = 0; i < k; i++){
        us x, y; cin >> y >> x;
        x--; y--;
        add_edge(x, m + y);
    }

    s = n + m;
    t = n + m + 1;

    for (us i = 0; i < m; i++)
        add_edge(s, i);

    for (us i = 0; i < n; i++)
        add_edge(i + m, t);

    for (us l = 0, r = 0; l < n; l++){

        if (l + m - 1 >= n) break;

        while (r < n && !ok(l, r))
            r++;
        if (r >= n) break;
        ans += n - r;

        us nm = g[t][l];

        if (edges[nm].psh) continue;

        us pr = -1, cr = m + l;
        for (us it = 0; it < sz(g[cr]); it++){
            us nm = g[cr][it];
            edge ed = edges[nm];

            if (ed.y != t && !ed.psh){
                pr = ed.y;
                break;
            }
        }

        nm = g[s][pr];
        edges[nm].psh = 0;
        edges[nm ^ 1].psh = 1;
    }

    cout << ans;

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1016 KB Output is correct
2 Correct 3 ms 1144 KB Output is correct
3 Correct 2 ms 1016 KB Output is correct
4 Correct 3 ms 1016 KB Output is correct
5 Correct 3 ms 1016 KB Output is correct
6 Correct 3 ms 1144 KB Output is correct
7 Correct 3 ms 1016 KB Output is correct
8 Correct 3 ms 1020 KB Output is correct
9 Correct 3 ms 888 KB Output is correct
10 Correct 3 ms 1144 KB Output is correct
11 Correct 3 ms 1144 KB Output is correct
12 Correct 3 ms 1144 KB Output is correct
13 Correct 2 ms 1016 KB Output is correct
14 Correct 3 ms 1144 KB Output is correct
15 Correct 3 ms 1144 KB Output is correct
16 Correct 3 ms 1144 KB Output is correct
17 Correct 3 ms 1144 KB Output is correct
18 Correct 3 ms 1144 KB Output is correct
19 Correct 5 ms 1144 KB Output is correct
20 Correct 3 ms 1144 KB Output is correct
21 Correct 3 ms 1144 KB Output is correct
22 Correct 3 ms 1144 KB Output is correct
23 Correct 3 ms 1148 KB Output is correct
24 Correct 3 ms 1144 KB Output is correct
25 Incorrect 72 ms 1400 KB Output isn't correct
26 Incorrect 20 ms 1220 KB Output isn't correct
27 Correct 4 ms 1144 KB Output is correct
28 Correct 5 ms 1144 KB Output is correct
29 Correct 22 ms 1404 KB Output is correct
30 Correct 18 ms 1272 KB Output is correct
31 Incorrect 71 ms 1908 KB Output isn't correct
32 Correct 25 ms 1272 KB Output is correct
33 Correct 12 ms 1144 KB Output is correct
34 Correct 17 ms 1272 KB Output is correct
35 Runtime error 6 ms 2168 KB Execution killed with signal 11 (could be triggered by violating memory limits)
36 Runtime error 6 ms 2128 KB Execution killed with signal 11 (could be triggered by violating memory limits)
37 Incorrect 224 ms 1908 KB Output isn't correct
38 Runtime error 6 ms 2296 KB Execution killed with signal 11 (could be triggered by violating memory limits)
39 Incorrect 705 ms 2036 KB Output isn't correct
40 Correct 671 ms 1788 KB Output is correct
41 Execution timed out 1545 ms 1908 KB Time limit exceeded
42 Incorrect 1245 ms 2288 KB Output isn't correct
43 Incorrect 296 ms 2800 KB Output isn't correct
44 Runtime error 6 ms 2168 KB Execution killed with signal 11 (could be triggered by violating memory limits)
45 Execution timed out 1580 ms 2932 KB Time limit exceeded
46 Runtime error 6 ms 2296 KB Execution killed with signal 11 (could be triggered by violating memory limits)
47 Runtime error 6 ms 2296 KB Execution killed with signal 11 (could be triggered by violating memory limits)
48 Runtime error 6 ms 2168 KB Execution killed with signal 11 (could be triggered by violating memory limits)
49 Runtime error 6 ms 2168 KB Execution killed with signal 11 (could be triggered by violating memory limits)
50 Execution timed out 1564 ms 2752 KB Time limit exceeded