QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#614707#9424. Stop the Castle 2kenkenkenWA 136ms7480kbC++235.4kb2024-10-05 16:50:312024-10-05 16:50:32

Judging History

This is the latest submission verdict.

  • [2024-10-05 16:50:32]
  • Judged
  • Verdict: WA
  • Time: 136ms
  • Memory: 7480kb
  • [2024-10-05 16:50:31]
  • Submitted

answer

#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define pob pop_back
#define SZ(x) (int)(x.size())
#define all(x) begin(x), end(x)
#ifdef LOCAL
#define HEHE freopen("in.txt", "r", stdin);
#define debug(...) {cout << #__VA_ARGS__ << " = "; dbg(__VA_ARGS__);}
#else
#define HEHE ios_base::sync_with_stdio(0), cin.tie(0);
#define debug(...) 7122;
#endif
using namespace std;

#define chmax(a, b) (a) = (a) > (b) ? (a) : (b)
#define chmin(a, b) (a) = (a) < (b) ? (a) : (b)

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
void dbg() { cerr << '\n'; }
template<typename T, typename ...U>
void dbg(T t, U ...u) { cerr << t << ' '; dbg(u...); }

#define int long long

struct Dinic {
    #define SZ(x) (int)(x.size())
    struct Edge {
        int to, w, rid;
    };
    vector<vector<Edge>> adj;
    void add_edge(int u, int v, int w) {
        adj[u].pb({v, w, SZ(adj[v])});
        adj[v].pb({u, 0, SZ(adj[u]) - 1});
    }
    int n;
    void init(int _n) {
        n = _n;
        adj.clear();
        adj.resize(n + 1);
    }
    vector<int> d;
    bool bfs(int s, int t) {
        queue<int> q; q.push(s);
        d.assign(n + 1, -1);
        d[s] = 1;
        while (q.size()) {
            int x = q.front(); q.pop();
            for (auto [y, w, rid] : adj[x]) {
                if (d[y] == -1 and w) {
                    d[y] = d[x] + 1;
                    q.push(y);
                }
            }
        }
        return d[t] != -1;
    }
    vector<int> it;
    int dfs(int x, int t, int limit) {
        if (x == t) return limit;
        for (int &i = it[x]; i < SZ(adj[x]); i++) {
            auto &[y, w, rid] = adj[x][i];
            if (!w or d[y] != d[x] + 1) continue;
            if (int r = dfs(y, t, min(limit, w))) {
                w -= r;
                adj[y][rid].w += r;
                return r;
            }
        }
        return 0;
    }
    int max_flow(int s, int t) {
        int flow = 0;
        while (bfs(s, t)) {
            it.assign(n + 1, 0);
            while (int v = dfs(s, t, 2e9)) {
                flow += v;
            }
        }
        return flow;
    }
} dinic;

void solve() {
    int n, m, k;
    cin >> n >> m >> k;
    map<int, vector<int>> r, c;
    FOR (i, 1, n) {
        int x, y; cin >> x >> y;
        r[x].pb(y); c[y].pb(x);
    }
    int atk = 0;
    for (auto &[x, y] : r) {
        sort(all(y));
        atk += y.size() - 1;
    }
    for (auto &[x, y] : c) {
        sort(all(y));
        atk += y.size() - 1;
    }
    vector<int> x(m + 1), y(m + 1);
    vector<int> ocp(m + 1);
    FOR (i, 1, m) {
        cin >> x[i] >> y[i];
        int j = lower_bound(all(r[x[i]]), y[i]) - r[x[i]].begin();
        if (j != r[x[i]].size() and j - 1 >= 0) {
            ocp[i]++;
        }
        j = lower_bound(all(c[y[i]]), x[i]) - c[y[i]].begin();
        if (j != c[y[i]].size() and j - 1 >= 0) {
            ocp[i]++;
        }
    }
    map<tuple<int, int, int, int>, int> mp;
    vector<int> ls, rs; // left side, right side
    vector<pair<int, int>> e;
    map<pair<int, int>, int> eid;
    int cnt = 0;
    FOR (i, 1, m) {
        if (ocp[i] < 2) continue;
        int j1 = lower_bound(all(r[x[i]]), y[i]) - r[x[i]].begin();
        int j2 = lower_bound(all(c[y[i]]), x[i]) - c[y[i]].begin();
        tuple<int, int, int, int> p1 = {x[i], r[x[i]][j1 - 1], x[i], r[x[i]][j1]};
        tuple<int, int, int, int> p2 = {c[y[i]][j2 - 1], y[i], c[y[i]][j2], y[i]};
        if (!mp[p1]) {
            mp[p1] = ++cnt; ls.pb(cnt);
        }
        if (!mp[p2]) {
            mp[p2] = ++cnt; rs.pb(cnt);
        }
        e.pb({mp[p1], mp[p2]});
        eid[{mp[p1], mp[p2]}] = i;
    }
    dinic.init(cnt + 1);
    for (int i : ls) dinic.add_edge(0, i, 1);
    for (int i : rs) dinic.add_edge(i, cnt + 1, 1);
    for (auto [i, j] : e) dinic.add_edge(i, j, 1);
    int flow = dinic.max_flow(0, cnt + 1);
    vector<int> f(m + 1, 0);
    int re = m - k;
    debug(flow, atk, re);
    for (int i : ls) {
        int flag = 0;
        for (auto [j, w, rid] : dinic.adj[i]) {
            if (re > 0 and j != 0 and w == 0) {
                f[eid[{i, j}]] = 1;
                re--;
                atk -= 2;
                flag = 1;
            }
        }
        if (flag) {
            for (auto [j, w, rid] : dinic.adj[i]) {
                if (j != 0 and w == 1) {
                    ocp[eid[{i, j}]]--;
                }
            }
        }
    }
    for (int i : rs) {
        int flag = 1;
        for (auto [j, w, rid] : dinic.adj[i]) {
            if (j != cnt + 1 and w > 0) {
                flag = 1;
            }
        }
        if (flag) {
            for (auto [j, w, rid] : dinic.adj[i]) {
                if (j != cnt + 1 and w == 0) {
                    ocp[eid[{j, i}]]--;
                }
            }
        }
    }
    FOR (i, 1, m) {
        if (!re) break;
        if (ocp[i] == 0 or f[i]) continue;
        f[i] = 1;
        re--; atk--;
    }
    FOR (i, 1, m) {
        if (!re) break;
        if (f[i]) continue;
        f[i] = 1; re--;
    }
    vector<int> an;
    FOR (i, 1, m) {
        if (!f[i]) an.pb(i);
    }
    // assert(k == an.size());
    cout << atk << '\n';
    for (int i : an) cout << i << ' ';
    cout << '\n';
}
signed main() {
    HEHE
    int t; cin >> t;
    while (t--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3648kb

input:

3
8 6 4
1 3
2 1
2 6
4 1
4 7
6 1
6 3
6 6
2 3
3 1
4 3
4 6
5 2
6 4
3 2 1
10 12
10 10
10 11
1 4
1 5
1 3 2
1 1
2 1
2 2
2 3

output:

4
2 3 5 6 
2
2 
0
2 3 

result:

ok ok 3 cases (3 test cases)

Test #2:

score: -100
Wrong Answer
time: 136ms
memory: 7480kb

input:

1224
11 17 14
7 3
4 2
8 13
3 15
3 4
5 11
10 2
3 3
8 6
7 11
2 3
10 4
1 3
12 1
2 5
11 9
11 6
11 10
8 15
1 5
9 14
4 11
1 6
10 7
7 6
11 4
8 4
1 11
18 3 2
14 8
2 14
13 13
9 12
14 12
5 6
8 1
10 5
8 6
8 9
6 6
7 5
12 11
6 11
13 5
1 10
7 6
14 5
6 15
2 4
11 1
1 6 4
14 14
13 9
9 3
10 12
7 5
8 13
9 14
1 9 8
4 9...

output:

7
3 4 5 6 7 8 9 10 11 12 13 15 16 17 
15
2 3 
0
3 4 5 6 
0
2 3 4 5 6 7 8 9 
11
1 3 
8
1 2 3 
0
1 2 3 4 5 6 7 8 9 10 11 12 
1
5 6 7 9 10 11 12 
8
17 18 19 
1
1 2 3 4 5 6 7 8 
7
6 8 
10
13 14 15 
1
10 11 12 13 14 15 16 17 18 19 20 
0
1 
1
2 3 
0
5 6 7 
6
7 8 12 13 14 
1
10 11 12 13 14 
4
3 4 5 6 7 8 
...

result:

wrong answer Participant states the answer to be 6 but is actually 7 (test case 17)