結果

問題 No.1640 簡単な色塗り
ユーザー nawawannawawan
提出日時 2021-08-06 23:35:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 249 ms / 2,000 ms
コード長 2,599 bytes
コンパイル時間 2,712 ms
コンパイル使用メモリ 220,264 KB
実行使用メモリ 14,656 KB
最終ジャッジ日時 2023-09-12 03:23:37
合計ジャッジ時間 16,111 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 192 ms
9,708 KB
testcase_05 AC 203 ms
14,656 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 135 ms
8,076 KB
testcase_11 AC 108 ms
7,016 KB
testcase_12 AC 95 ms
7,004 KB
testcase_13 AC 213 ms
10,436 KB
testcase_14 AC 208 ms
10,432 KB
testcase_15 AC 68 ms
5,816 KB
testcase_16 AC 83 ms
6,240 KB
testcase_17 AC 171 ms
9,100 KB
testcase_18 AC 15 ms
4,380 KB
testcase_19 AC 97 ms
6,552 KB
testcase_20 AC 129 ms
7,852 KB
testcase_21 AC 103 ms
6,924 KB
testcase_22 AC 11 ms
4,376 KB
testcase_23 AC 147 ms
7,980 KB
testcase_24 AC 37 ms
4,516 KB
testcase_25 AC 94 ms
6,864 KB
testcase_26 AC 158 ms
8,600 KB
testcase_27 AC 65 ms
5,536 KB
testcase_28 AC 194 ms
10,232 KB
testcase_29 AC 149 ms
8,516 KB
testcase_30 AC 12 ms
4,388 KB
testcase_31 AC 91 ms
10,180 KB
testcase_32 AC 79 ms
9,436 KB
testcase_33 AC 50 ms
7,256 KB
testcase_34 AC 65 ms
8,308 KB
testcase_35 AC 51 ms
7,188 KB
testcase_36 AC 12 ms
4,384 KB
testcase_37 AC 17 ms
4,620 KB
testcase_38 AC 83 ms
9,700 KB
testcase_39 AC 32 ms
6,032 KB
testcase_40 AC 32 ms
5,940 KB
testcase_41 AC 68 ms
8,516 KB
testcase_42 AC 37 ms
6,140 KB
testcase_43 AC 39 ms
6,532 KB
testcase_44 AC 39 ms
6,596 KB
testcase_45 AC 29 ms
5,640 KB
testcase_46 AC 14 ms
4,444 KB
testcase_47 AC 9 ms
4,376 KB
testcase_48 AC 84 ms
9,616 KB
testcase_49 AC 5 ms
4,380 KB
testcase_50 AC 1 ms
4,376 KB
testcase_51 AC 1 ms
4,376 KB
testcase_52 AC 249 ms
12,864 KB
testcase_53 AC 242 ms
13,080 KB
07_evil_01.txt AC 528 ms
19,792 KB
07_evil_02.txt AC 809 ms
27,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct UnionFind{
    vector<int> par;
    vector<int> rank;
    vector<int> cnt;
    UnionFind(int n){
        par.resize(n);
        rank.resize(n);
        cnt.resize(n, 0);
        for(int i = 0; i < n; i++){
            par[i] = i;
            rank[i] = 1;
        }
    }

    int root(int x){
        if(par[x] == x) return x;
        else return par[x] = root(par[x]);
    }

    bool same(int x, int y){
        return root(x) == root(y);
    }

    bool unite(int x, int y){
        x = root(x);
        y = root(y);
        if(x == y) {
            cnt[x] += 1;
            return false;
        }
        if(rank[x] < rank[y]) swap(x, y);
        par[y] = x;
        rank[x] += rank[y];
        cnt[x] += cnt[y] + 1;
        return true;
    }

    int size(int x) {
        return rank[root(x)];
    }
    int count(int x){
        return cnt[root(x)];
    }
};
int main(){
    int N;
    cin >> N;
    vector<int> A(N), B(N);
    UnionFind U(N);
    vector<vector<pair<int, int>>> G(N);
    vector<int> r, le, ri, ind;
    for(int i = 0; i < N; i++) {
        cin >> A[i] >> B[i];
        A[i]--;
        B[i]--;
        bool f = U.unite(A[i], B[i]);
        if(!f) {
            r.push_back(A[i]);
            le.push_back(A[i]);
            ri.push_back(B[i]);
            ind.push_back(i);
        }
        else{
            G[A[i]].push_back({B[i], i});
            G[B[i]].push_back({A[i], i});
        }
    }
    vector<bool> used(N, false);
    bool f = true;
    for(int i = 0; i < N; i++){
        int t = U.root(i);
        if(used[t]) continue;
        used[t] = true;
        if(U.size(i) != U.count(i)) f = false;
    }
    if(!f) cout << "No" << endl;
    else{
        cout << "Yes" << endl;
        vector<int> ans(N);
        for(int i = 0; i < N; i++) ans[i] = i;
        used.assign(N, false);
        auto dfs = [&](auto dfs, int now, int par) -> void{
            for(auto [to, id] : G[now]){
                if(to == par) continue;
                ans[id] = to;
                used[to] = true;
                dfs(dfs, to, now);
            }
        };
        for(int i = 0; i < (int)r.size(); i++){
            dfs(dfs, r[i], -1);
        }
        for(int i = 0; i < (int)le.size(); i++){
            if(!used[le[i]]) {
                ans[ind[i]] = le[i];
                used[le[i]] = true;
            }
            else if(!used[ri[i]]) {
                ans[ind[i]] = ri[i];
                used[ri[i]] = true;
            }
        }
        for(int i = 0; i < N; i++) cout << ans[i] + 1 << endl;
    }
}
0