結果

問題 No.1640 簡単な色塗り
ユーザー sash0sash0
提出日時 2021-08-22 23:27:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,268 ms / 2,000 ms
コード長 7,306 bytes
コンパイル時間 3,474 ms
コンパイル使用メモリ 237,956 KB
実行使用メモリ 50,940 KB
最終ジャッジ日時 2024-06-29 17:09:11
合計ジャッジ時間 21,523 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 83 ms
34,744 KB
testcase_05 AC 81 ms
35,088 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 227 ms
21,468 KB
testcase_11 AC 182 ms
19,500 KB
testcase_12 AC 148 ms
19,176 KB
testcase_13 AC 467 ms
34,804 KB
testcase_14 AC 479 ms
33,916 KB
testcase_15 AC 91 ms
12,748 KB
testcase_16 AC 126 ms
18,812 KB
testcase_17 AC 352 ms
35,600 KB
testcase_18 AC 15 ms
6,944 KB
testcase_19 AC 156 ms
18,644 KB
testcase_20 AC 232 ms
21,708 KB
testcase_21 AC 164 ms
19,720 KB
testcase_22 AC 9 ms
6,940 KB
testcase_23 AC 246 ms
23,256 KB
testcase_24 AC 46 ms
10,508 KB
testcase_25 AC 157 ms
18,216 KB
testcase_26 AC 300 ms
32,216 KB
testcase_27 AC 83 ms
12,492 KB
testcase_28 AC 434 ms
32,652 KB
testcase_29 AC 294 ms
33,480 KB
testcase_30 AC 15 ms
7,724 KB
testcase_31 AC 157 ms
48,784 KB
testcase_32 AC 104 ms
33,020 KB
testcase_33 AC 61 ms
20,436 KB
testcase_34 AC 106 ms
35,756 KB
testcase_35 AC 65 ms
21,712 KB
testcase_36 AC 14 ms
7,464 KB
testcase_37 AC 20 ms
9,100 KB
testcase_38 AC 116 ms
36,084 KB
testcase_39 AC 51 ms
20,480 KB
testcase_40 AC 41 ms
15,708 KB
testcase_41 AC 75 ms
26,044 KB
testcase_42 AC 50 ms
19,208 KB
testcase_43 AC 64 ms
23,860 KB
testcase_44 AC 47 ms
17,056 KB
testcase_45 AC 36 ms
14,680 KB
testcase_46 AC 17 ms
8,184 KB
testcase_47 AC 11 ms
6,944 KB
testcase_48 AC 114 ms
35,160 KB
testcase_49 AC 6 ms
6,944 KB
testcase_50 AC 2 ms
6,944 KB
testcase_51 AC 2 ms
6,944 KB
testcase_52 AC 215 ms
50,940 KB
testcase_53 AC 1,268 ms
46,368 KB
07_evil_01.txt AC 1,213 ms
67,956 KB
07_evil_02.txt TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

// clang-format off
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#include <algorithm>
#include <cassert>
#include <limits>
#include <queue>
#include <vector>


#include <vector>

namespace atcoder {

namespace internal {

template <class T> struct simple_queue {
    std::vector<T> payload;
    int pos = 0;
    void reserve(int n) { payload.reserve(n); }
    int size() const { return int(payload.size()) - pos; }
    bool empty() const { return pos == int(payload.size()); }
    void push(const T& t) { payload.push_back(t); }
    T& front() { return payload[pos]; }
    void clear() {
        payload.clear();
        pos = 0;
    }
    void pop() { pos++; }
};

}  // namespace internal

}  // namespace atcoder


namespace atcoder {

template <class Cap> struct mf_graph {
  public:
    mf_graph() : _n(0) {}
    mf_graph(int n) : _n(n), g(n) {}

    int add_edge(int from, int to, Cap cap) {
        assert(0 <= from && from < _n);
        assert(0 <= to && to < _n);
        assert(0 <= cap);
        int m = int(pos.size());
        pos.push_back({from, int(g[from].size())});
        int from_id = int(g[from].size());
        int to_id = int(g[to].size());
        if (from == to) to_id++;
        g[from].push_back(_edge{to, to_id, cap});
        g[to].push_back(_edge{from, from_id, 0});
        return m;
    }

    struct edge {
        int from, to;
        Cap cap, flow;
    };

    edge get_edge(int i) {
        int m = int(pos.size());
        assert(0 <= i && i < m);
        auto _e = g[pos[i].first][pos[i].second];
        auto _re = g[_e.to][_e.rev];
        return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};
    }
    std::vector<edge> edges() {
        int m = int(pos.size());
        std::vector<edge> result;
        for (int i = 0; i < m; i++) {
            result.push_back(get_edge(i));
        }
        return result;
    }
    void change_edge(int i, Cap new_cap, Cap new_flow) {
        int m = int(pos.size());
        assert(0 <= i && i < m);
        assert(0 <= new_flow && new_flow <= new_cap);
        auto& _e = g[pos[i].first][pos[i].second];
        auto& _re = g[_e.to][_e.rev];
        _e.cap = new_cap - new_flow;
        _re.cap = new_flow;
    }

    Cap flow(int s, int t) {
        return flow(s, t, std::numeric_limits<Cap>::max());
    }
    Cap flow(int s, int t, Cap flow_limit) {
        assert(0 <= s && s < _n);
        assert(0 <= t && t < _n);
        assert(s != t);

        std::vector<int> level(_n), iter(_n);
        internal::simple_queue<int> que;

        auto bfs = [&]() {
            std::fill(level.begin(), level.end(), -1);
            level[s] = 0;
            que.clear();
            que.push(s);
            while (!que.empty()) {
                int v = que.front();
                que.pop();
                for (auto e : g[v]) {
                    if (e.cap == 0 || level[e.to] >= 0) continue;
                    level[e.to] = level[v] + 1;
                    if (e.to == t) return;
                    que.push(e.to);
                }
            }
        };
        auto dfs = [&](auto self, int v, Cap up) {
            if (v == s) return up;
            Cap res = 0;
            int level_v = level[v];
            for (int& i = iter[v]; i < int(g[v].size()); i++) {
                _edge& e = g[v][i];
                if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;
                Cap d =
                    self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));
                if (d <= 0) continue;
                g[v][i].cap += d;
                g[e.to][e.rev].cap -= d;
                res += d;
                if (res == up) return res;
            }
            level[v] = _n;
            return res;
        };

        Cap flow = 0;
        while (flow < flow_limit) {
            bfs();
            if (level[t] == -1) break;
            std::fill(iter.begin(), iter.end(), 0);
            Cap f = dfs(dfs, t, flow_limit - flow);
            if (!f) break;
            flow += f;
        }
        return flow;
    }

    std::vector<bool> min_cut(int s) {
        std::vector<bool> visited(_n);
        internal::simple_queue<int> que;
        que.push(s);
        while (!que.empty()) {
            int p = que.front();
            que.pop();
            visited[p] = true;
            for (auto e : g[p]) {
                if (e.cap && !visited[e.to]) {
                    visited[e.to] = true;
                    que.push(e.to);
                }
            }
        }
        return visited;
    }

  private:
    int _n;
    struct _edge {
        int to, rev;
        Cap cap;
    };
    std::vector<std::pair<int, int>> pos;
    std::vector<std::vector<_edge>> g;
};

}  // namespace atcoder


#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forn1(i,n) for (int i = 1; i <= int(n); i++)
#define popcnt __builtin_popcountll
#define ffs __builtin_ffsll
#define ctz __builtin_ctzll
#define clz __builtin_clz
#define clzll __builtin_clzll
#define all(a) (a).begin(), (a).end()

using namespace std;
using namespace __gnu_pbds;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pil = pair<int,ll>;
using pll = pair<ll,ll>;
template <typename T> using vec = vector<T>;
using vi = vec<int>;
using vl = vec<ll>;
template <typename T> using que = queue<T>;
template <typename T> using deq = deque<T>;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename K, typename V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename T> T id(T b) {return b;};
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); }
template <typename T> bool getf(T flag, size_t i) { return (flag>>i) & 1; }
template <typename T> T setf(T flag, size_t i) { return flag | (T(1)<<i); }
template <typename T> T unsetf(T flag, size_t i) { return flag & ~(T(1)<<i); }
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); }
// clang-format on

int main() {
    fastio();

    int n;
    cin >> n;

    auto ball = [](int i) {
        return i;
    };

    auto rule = [&](int i) {
        return i + n;
    };

    int s = 2 * n;
    int t = s + 1;

    atcoder::mf_graph<int> g(t + 1);

    forn(i, n) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        g.add_edge(ball(a), rule(i), 1);
        g.add_edge(ball(b), rule(i), 1);
    }

    forn(i, n) {
        g.add_edge(s, ball(i), 1);
        g.add_edge(rule(i), t, 1);
    }

    if (g.flow(s, t) < n) {
        cout << "No\n";
        return 0;
    }

    vi ans(n);
    for (auto e : g.edges()) {
        if (e.flow == 1 and n <= e.to and e.to < 2 * n) {
            ans[e.to - n] = e.from + 1;
        }
    }

    cout << "Yes\n";
    for (auto x : ans) {
        cout << x << "\n";
    }

    return 0;
}
0