結果

問題 No.1640 簡単な色塗り
ユーザー Ricky_ponRicky_pon
提出日時 2021-08-06 22:56:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,933 bytes
コンパイル時間 2,742 ms
コンパイル使用メモリ 213,600 KB
実行使用メモリ 22,952 KB
最終ジャッジ日時 2023-09-12 03:00:38
合計ジャッジ時間 12,032 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 WA -
testcase_05 AC 61 ms
22,952 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 32 ms
9,084 KB
testcase_13 WA -
testcase_14 AC 65 ms
15,680 KB
testcase_15 AC 22 ms
7,580 KB
testcase_16 AC 29 ms
8,460 KB
testcase_17 AC 58 ms
14,572 KB
testcase_18 AC 6 ms
4,380 KB
testcase_19 AC 33 ms
9,236 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 5 ms
4,376 KB
testcase_23 AC 45 ms
11,724 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 51 ms
12,768 KB
testcase_30 AC 9 ms
5,268 KB
testcase_31 AC 59 ms
16,668 KB
testcase_32 AC 51 ms
15,092 KB
testcase_33 AC 34 ms
11,412 KB
testcase_34 AC 41 ms
13,452 KB
testcase_35 AC 33 ms
11,084 KB
testcase_36 AC 10 ms
5,340 KB
testcase_37 AC 12 ms
6,048 KB
testcase_38 AC 70 ms
15,424 KB
testcase_39 AC 22 ms
8,676 KB
testcase_40 AC 23 ms
8,584 KB
testcase_41 AC 44 ms
13,604 KB
testcase_42 AC 26 ms
9,324 KB
testcase_43 AC 26 ms
9,976 KB
testcase_44 AC 26 ms
9,320 KB
testcase_45 AC 21 ms
8,052 KB
testcase_46 AC 10 ms
5,420 KB
testcase_47 AC 7 ms
4,756 KB
testcase_48 AC 57 ms
15,636 KB
testcase_49 AC 4 ms
4,380 KB
testcase_50 WA -
testcase_51 WA -
testcase_52 AC 100 ms
22,144 KB
testcase_53 AC 98 ms
19,812 KB
07_evil_01.txt WA -
07_evil_02.txt WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <atcoder/dsu>

#include <vector>

template <class T = int>
struct Edge {
    int from, to;
    T cost;
    int idx;

    Edge() = default;

    Edge(int from, int to, T cost = 1, int idx = 0)
        : from(from), to(to), cost(cost), idx(idx) {}
};

template <class T = int>
struct Graph {
    std::vector<std::vector<Edge<T>>> es;
    int edge_num;

    Graph(int n) : edge_num(0) { es.resize(n); }

    int size() { return es.size(); }

    void add_edge(int from, int to, T cost = 1, int idx = 0) {
        es[from].emplace_back(from, to, cost, idx);
        ++edge_num;
    }

    std::vector<Edge<T>> edges() {
        std::vector<Edge<T>> tmp;
        for (int v = 0; v < (int)es.size(); ++v) {
            for (auto& e : es[v]) {
                tmp.push_back(e);
            }
        }
        if (tmp.size() >= 2 && tmp[0].idx == tmp[1].idx) {
            return tmp;
        }
        std::vector<Edge<T>> res(edge_num);
        for (auto& e : tmp) res[e.idx] = e;
        return res;
    }

    std::vector<Edge<T>>& operator[](int i) { return es[i]; }
};


#include <algorithm>
#include <cassert>
#include <vector>

template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
T div_floor(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a >= 0 ? a / b : (a + 1) / b - 1;
}

template <class T>
T div_ceil(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a > 0 ? (a - 1) / b + 1 : a / b;
}

template <typename T>
struct CoordComp {
    std::vector<T> v;
    bool sorted;

    CoordComp() : sorted(false) {}

    int size() { return v.size(); }

    void add(T x) { v.push_back(x); }

    void build() {
        std::sort(v.begin(), v.end());

        v.erase(std::unique(v.begin(), v.end()), v.end());
        sorted = true;
    }

    int get_idx(T x) {
        assert(sorted);
        return lower_bound(v.begin(), v.end(), x) - v.begin();
    }

    T &operator[](int i) { return v[i]; }
};


#define For(i, a, b) for (int i = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int i = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For(i, 0, n)
#define rrep(i, n) rFor(i, n, 0)
#define fi first
#define se second

using namespace std;

using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;

using namespace atcoder;

int get_back(int v, int pv, Graph<> &gr, vector<bool> &used) {
    used[v] = true;
    for (auto &e : gr[v]) {
        if (e.to == pv) continue;
        if (used[e.to]) return e.idx;
        int tmp = get_back(e.to, v, gr, used);
        if (tmp >= 0) return tmp;
    }
    return -1;
}

void dfs(int v, int pv, Graph<> &gr, vector<int> &col, vector<int> &ans) {
    for (auto &e : gr[v]) {
        if (e.to == pv || col[e.to]) continue;
        col[e.to] = 1;
        ans[e.idx] = e.to;
        dfs(e.to, v, gr, col, ans);
    }
}

int main() {
    int n;
    scanf("%d", &n);
    Graph<int> gr(n);
    rep(i, n) {
        int a, b;
        scanf("%d%d", &a, &b);
        --a;
        --b;
        gr.add_edge(a, b, 1, i);
        gr.add_edge(b, a, 1, i);
    }
    auto es = gr.edges();

    dsu uf(n);
    for (auto &e : es) uf.merge(e.from, e.to);
    vector<int> cnt(n, 0);
    for (auto &e : es) ++cnt[uf.leader(e.from)];
    rep(i, n) if (i == uf.leader(i)) {
        if (uf.size(i) > cnt[i]) {
            puts("No");
            return 0;
        }
    }

    vector<int> col(n, 0), ans(es.size(), -1);
    vector<bool> used(n, false);
    rep(i, n) if (col[i] == 0) {
        int b = get_back(i, -1, gr, used);
        ans[b] = es[b].from;
        col[es[b].from] = 1;
        dfs(es[b].from, -1, gr, col, ans);
    }
    puts("Yes");
    rep(i, n) printf("%d\n", ans[i] + 1);
}
0