QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#603683#9424. Stop the Castle 2SorahISAWA 125ms8760kbC++2314.9kb2024-10-01 18:18:512024-10-01 18:18:52

Judging History

This is the latest submission verdict.

  • [2024-10-01 18:18:52]
  • Judged
  • Verdict: WA
  • Time: 125ms
  • Memory: 8760kb
  • [2024-10-01 18:18:51]
  • Submitted

answer

#ifndef SorahISA
#define SorahISA
#include SorahISA __FILE__ SorahISA

// https://github.com/OmeletWithoutEgg/ckiseki/blob/fe9bac856a384aa36252244a31b73b73e0955d5c/codes/FlowAndMatching/Dinic.cpp

template <typename Cap = int64_t> struct Dinic {
  struct E { int to, rev; Cap cap; int comment; }; int n, st, ed;
  vector<vector<E>> G; vector<size_t> lv, idx;
  bool BFS(int k) {
    lv.assign(n, 0); idx.assign(n, 0);
    queue<int> bfs; bfs.push(st); lv[st] = 1;
    while (not bfs.empty() and not lv[ed]) {
      int u = bfs.front(); bfs.pop();
      for (auto e : G[u]) if (e.cap >> k and !lv[e.to])
        bfs.push(e.to), lv[e.to] = lv[u] + 1;
    }
    return lv[ed];
  }
  Cap DFS(int u, Cap f = numeric_limits<Cap>::max()) {
    if (u == ed) return f;
    Cap ret = 0;
    for (auto &i = idx[u]; i < G[u].size(); ++i) {
      auto &[to, rev, cap, _comment] = G[u][i];
      if (cap <= 0 or lv[to] != lv[u] + 1) continue;
      Cap nf = DFS(to, min(f, cap));
      ret += nf; cap -= nf; f -= nf;
      G[to][rev].cap += nf;
      if (f == 0) return ret;
    }
    if (ret == 0) lv[u] = 0;
    return ret;
  }
  void init(int n_) { G.assign(n = n_, vector<E>()); }
  void add_edge(int u, int v, Cap c, int comment) {
    G[u].eb(v, ssize(G[v]), c, comment);
    G[v].eb(u, ssize(G[u])-1, 0, -1);
  }
  Cap max_flow(int st_, int ed_) {
    st = st_, ed = ed_; Cap ret = 0;
    for (int i = 31; i >= 0; --i) { /// log2(max cap)
      if (i == 0) assert(ret == 0);
      while (BFS(i)) ret += DFS(st);
    }
    return ret;
  }
}; // test @ luogu P3376

void solve() {
    int N, M, K; cin >> N >> M >> K;
    
    vector<pii> castles(N), obstacles(M);
    for (auto &[r, c] : castles) cin >> r >> c;
    for (auto &[r, c] : obstacles) cin >> r >> c;
    
    map<int, set<pii>> row, col;
    for (int i = 0; i < N; ++i) {
        auto [r, c] = castles[i];
        row[r].ee(c, i), col[c].ee(r, i);
    }
    
    Dinic<int> dinic;
    dinic.init(2 + 2*N);
    
    int tok_conflict = 0;
    map<pii, int> id_conflict;
    for (const auto &[r, st] : row) {
        int id_lst = -1;
        for (const auto &[c, id] : st) {
            if (id_lst != -1) {
                id_conflict[{id_lst, id}] = tok_conflict;
                dinic.add_edge(2*N, tok_conflict, 1, -1);
                ++tok_conflict;
            }
            id_lst = id;
        }
    }
    for (const auto &[c, st] : col) {
        int id_lst = -1;
        for (const auto &[r, id] : st) {
            if (id_lst != -1) {
                id_conflict[{id_lst, id}] = tok_conflict;
                dinic.add_edge(tok_conflict, 2*N+1, 1, -1);
                ++tok_conflict;
            }
            id_lst = id;
        }
    }
    
    vector<int> conflict_to_any_obstacle(tok_conflict, -1);
    for (int i = 0; i < M; ++i) {
        auto [r, c] = obstacles[i];
        if (not row.contains(r) or not col.contains(c)) continue;
        
        auto it_r = row[r].lower_bound({c, -1});
        if (it_r == begin(row[r]) or it_r == end(row[r])) continue;
        int id_r = id_conflict[{prev(it_r)->second, it_r->second}];
        conflict_to_any_obstacle[id_r] = i;
        
        auto it_c = col[c].lower_bound({r, -1});
        if (it_c == begin(col[c]) or it_c == end(col[c])) continue;
        int id_c = id_conflict[{prev(it_c)->second, it_c->second}];
        conflict_to_any_obstacle[id_c] = i;
        
        dinic.add_edge(id_r, id_c, 1, i);
    }
    
    int maxflow = dinic.max_flow(2*N, 2*N+1);
    
    int n_pick = 0;
    vector<int> ans(M, 0), alive(tok_conflict, 1);
    for (int i = 0; i < tok_conflict and n_pick < M - K; ++i) {
        for (auto &e : dinic.G[i]) {
            if (e.cap == 0 and e.comment != -1) {
                assert(ans[e.comment] == 0 and alive[i] == 1 and alive[e.to] == 1);
                ++n_pick, ans[e.comment] = 1, alive[i] = 0, alive[e.to] = 0;
                break;
            }
        }
    }
    for (int i = 0; i < tok_conflict and n_pick < M - K; ++i) {
        if (!alive[i] or conflict_to_any_obstacle[i] == -1) continue;
        assert(ans[conflict_to_any_obstacle[i]] == 0 and alive[i] == 1);
        ++n_pick, ans[conflict_to_any_obstacle[i]] = 1, alive[i] = 0;
    }
    for (int i = 0; i < M and n_pick < M - K; ++i) {
        if (ans[i] == 0) ++n_pick, ans[i] = 1;
    }
    
    // debug(tok_conflict, maxflow);
    
    cout << accumulate(ALL(alive), int(0)) << "\n";
    for (int i = 0; i < M; ++i) { if (ans[i] == 0) cout << i + 1 << " "; }
    cout << "\n";
}

int32_t main() {
    fastIO();
    
    int t = 1; cin >> t;
    for (int _ = 1; _ <= t; ++_) {
        // cout << "Case #" << _ << ": ";
        solve();
    }
    
    return 0;
}

#else

#ifdef local
#define _GLIBCXX_DEBUG 1
#endif
#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
using namespace std;
// #include <bits/extc++.h>
// #include <tr2/dynamic_bitset>

using i64 = long long;
using i128 = __int128;
// #define int i64
using f80 = long double;
using f128 = __float128;
#define double f80
using pii = pair<int, int>;
template <typename T> using Prior = std::priority_queue<T>;
template <typename T> using prior = std::priority_queue<T, vector<T>, greater<T>>;

// #define X first
// #define Y second
#define eb emplace_back
#define ef emplace_front
#define ee emplace
#define pb pop_back
#define pf pop_front
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)
#define SZ(x) ((int)(x).size())

// template <size_t D, typename T> struct Vec : vector<Vec<D-1, T>> {
//     static_assert(D >= 1, "Vector dimension must be greater than zero!");
//     template <typename... Args> Vec(int n = 0, Args... args) : vector<Vec<D-1, T>>(n, Vec<D-1, T>(args...)) {}
// };

// template <typename T> struct Vec<1, T> : vector<T> {
//     Vec(int n = 0, const T& val = T()) : vector<T>(n, val) {}
// };

#ifdef local
#define fastIO() void()
#define debug(...) \
    _color.emplace_back("\u001b[31m"), \
    fprintf(stderr, "%sAt [%s], line %d: (%s) = ", _color.back().c_str(), __FUNCTION__, __LINE__, #__VA_ARGS__), \
    _do(__VA_ARGS__), _color.pop_back(), \
    fprintf(stderr, "%s", _color.back().c_str())
#define print(...) \
    fprintf(stdout, "%s", "\u001b[36m"), \
    _P(__VA_ARGS__), \
    fprintf(stdout, "%s", "\u001b[0m")

deque<string> _color{"\u001b[0m"};

template <typename T> concept is_string = is_same_v<T, string&> or is_same_v<T, const string&>;
template <typename T> concept is_iterable = requires (T _t) { begin(_t); };

template <typename T> inline void _print_err(T &&_t);
template <typename T> inline void _print_err(T &&_t) requires is_iterable<T> and (not is_string<T>);
template <size_t I, typename ...U> inline typename enable_if<I == sizeof...(U), void>::type _print_err(const tuple<U...> &);
template <size_t I, typename ...U> inline typename enable_if<I <  sizeof...(U), void>::type _print_err(const tuple<U...> &_t);
template <size_t I, typename ...U> inline typename enable_if<I == sizeof...(U), void>::type _print_err(tuple<U...> &);
template <size_t I, typename ...U> inline typename enable_if<I <  sizeof...(U), void>::type _print_err(tuple<U...> &_t);
template <typename T, typename U> ostream& operator << (ostream &os, const pair<T, U> &_tu);

inline void _do() { cerr << "\n"; }
template <typename T> inline void _do(T &&_t) { _print_err(_t), cerr << "\n"; }
template <typename T, typename ...U> inline void _do(T &&_t, U &&..._u) { _print_err(_t), cerr << ", ", _do(_u...); }
#else
#define fastIO() ios_base::sync_with_stdio(0), cin.tie(0)
#define debug(...) void()
#define print(...) _P(__VA_ARGS__)
#endif

inline void _P() { cout << "\n"; }
template <typename T> inline void _P(T &&_t) { cout << _t << "\n"; }
template <typename T, typename ...U> inline void _P(T &&_t, U &&..._u) { cout << _t << " ", _P(_u...); }

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

inline int getRand(int L, int R) {
    if (L > R) swap(L, R);
    return (int)(rng() % ((uint64_t)R - L + 1) + L);
}

template <typename T, typename U> bool chmin(T &lhs, U rhs) { return lhs > rhs ? lhs = rhs, 1 : 0; }
template <typename T, typename U> bool chmax(T &lhs, U rhs) { return lhs < rhs ? lhs = rhs, 1 : 0; }

/// below are Fast I/O and _print_err templates ///

/*
/// Fast I/O by FHVirus ///
/// https://fhvirus.github.io/blog/2020/fhvirus-io/ ///

#include <unistd.h>

const int S = 65536;

int OP = 0;
char OB[S];

inline char RC() {
    static char buf[S], *p = buf, *q = buf;
    return p == q and (q = (p = buf) + read(0, buf, S)) == buf ? -1 : *p++;
}

inline int RI() {
    static char c;
    int a;
    while (((c = RC()) < '0' or c > '9') and c != '-' and c != -1);
    if (c == '-') {
        a = 0;
        while ((c = RC()) >= '0' and c <= '9') a *= 10, a -= c ^ '0';
    }
    else {
        a = c ^ '0';
        while ((c = RC()) >= '0' and c <= '9') a *= 10, a += c ^ '0';
    }
    return a;
}

inline void WI(int n, char c = '\n') {
    static char buf[20], p;
    if (n == 0) OB[OP++] = '0';
    p = 0;
    if (n < 0) {
        OB[OP++] = '-';
        while (n) buf[p++] = '0' - (n % 10), n /= 10;
    }
    else {
        while (n) buf[p++] = '0' + (n % 10), n /= 10;
    }
    for (--p; p >= 0; --p) OB[OP++] = buf[p];
    OB[OP++] = c;
    if (OP > S-20) write(1, OB, OP), OP = 0;
}

/// Fast I/O by FHVirus ///
/// https://fhvirus.github.io/blog/2020/fhvirus-io/ ///
*/

#ifdef local

template <typename T> inline void _print_err(T &&_t) { cerr << _t; }

template <typename T> inline void _print_err(T &&_t) requires is_iterable<T> and (not is_string<T>) {
    string _tmp_color = _color.back();
    ++_tmp_color[3], _color.emplace_back(_tmp_color);
    cerr << _color.back() << "[";
    for (bool _first = true; auto &_x : _t) {
        if (!_first) cerr << ", ";
        _print_err(_x), _first = false;
    }
    cerr << "]" << (_color.pop_back(), _color.back());
}

template <typename T, typename U> ostream& operator << (ostream &os, const pair<T, U> &_tu) {
    string _tmp_color = _color.back();
    ++_tmp_color[3], _color.emplace_back(_tmp_color);
    cerr << _color.back() << "(";
    _print_err(_tu.first), cerr << ", ", _print_err(_tu.second);
    cerr << ")" << (_color.pop_back(), _color.back());
    return os;
}

template <size_t I = 0, typename ...U> inline typename enable_if<I == sizeof...(U), void>::type _print_err(const tuple<U...> &) {
    cerr << ")" << (_color.pop_back(), _color.back());
}

template <size_t I = 0, typename ...U> inline typename enable_if<I <  sizeof...(U), void>::type _print_err(const tuple<U...> &_t) {
    if (!I) {
        string _tmp_color = _color.back();
        ++_tmp_color[3], _color.emplace_back(_tmp_color);
        cerr << _color.back();
    }
    cerr << (I ? ", " : "("), _print_err(get<I>(_t)), _print_err<I+1, U...>(_t);
}

template <size_t I = 0, typename ...U> inline typename enable_if<I == sizeof...(U), void>::type _print_err(tuple<U...> &) {
    cerr << ")" << (_color.pop_back(), _color.back());
}

template <size_t I = 0, typename ...U> inline typename enable_if<I <  sizeof...(U), void>::type _print_err(tuple<U...> &_t) {
    if (!I) {
        string _tmp_color = _color.back();
        ++_tmp_color[3], _color.emplace_back(_tmp_color);
        cerr << _color.back();
    }
    cerr << (I ? ", " : "("), _print_err(get<I>(_t)), _print_err<I+1, U...>(_t);
}

#endif

#endif

/**
 *                                                                                                                 
 *                                                                                                                 
 *                                                                                                                 
 *                            iiiiii         iiiiiiiiii       iiiiiiiiiiiiii                                       
 *                       iiiiiiiiiiiii   iiiiiii    iiii    iiiiiiiiiiiiiii                          ii   iiii     
 *                    iiiiiiii     iiiiiiiii         iiii       iiii iii              iii          iiiiiiiiii      
 *                 iiiiiii          iiiiii           iiii    iiii   ii           iiiiiiiiii      iiii iiii         
 *               iiiiii            iiiii             iiii iiii        iii      iiii    iiiiiiiiiiiiiiiii  ii       
 *             iiiiii            iiiiiii            iiiiiii       iiiiiiii   iii    iiiiiiiiiiiiii iii  iiii       
 *           iiiiii             iiiiiii            iiiii   ii   iiii       iiiiiiiiiii iiii  iii iiii iiii      iii
 *          iiiii              iiiiiiii       ii        iiiii iiii    iiiiiiiii        iii iii  iii  iii  ii  iiii 
 *        iiiiii              iiiiiiii      iiiii     iiiii iiiiiiiiiiiiiiii         iii  iii  ii  iii  iii iiii   
 *       iiiii                 iiiiii     iiii     iiiiii iiiiiii    iii iii       iiii  ii   i   ii  iii  iii     
 *     iiiiii                            iiii  iiiiiiiiiiiiiii       iii iiii   iiiii  iii  ii  iii  iii  ii       
 *    iiiii                              iiiiiiii iiiiiiiiii       iiii   iiiiiiiii            ii  iii  ii         
 *   iiiii                                     iiiiii  iiii      iiiii              iii      ii   ii  i            
 * iiiiii                                  iiiiiiii   iiiii    iiiii                        ii  ii   ii            
 * iiiii                                iiii  iiii    iiiiiiiiiiii                             ii                  
 *  iii                              iiii   iiii       iiiiiiii                                                    
 *                                iiiii   iiii                                                                     
 *                              iiii     iiii                                                                      
 *                            iiii    iiiii                                                                        
 *                          iii     iiiii                                                                          
 *                        iii     iiiii                                                                            
 *                       iii   iiiiii                                                                              
 *                       iiiiiiiii                                                                                 
 *                       iiiiii                                                                                    
 *                                                                                                                 
 *                                                                                                                 
 *                                                                                                                 
**/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3812kb

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: 125ms
memory: 8760kb

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 
2
6 7 8 9 10 11 12 
8
17 18 19 
1
1 2 3 4 5 6 7 8 
8
7 8 
10
13 14 15 
2
10 11 12 13 14 15 16 17 18 19 20 
0
1 
1
2 3 
0
5 6 7 
10
11 12 13 14 15 
3
10 11 12 13 14 
4
3 4 5 6 7 ...

result:

wrong answer Jury has better answer. Participant's answer is 2 while jury's answer is 1 (test case 8)