結果

問題 No.1640 簡単な色塗り
ユーザー nawawannawawan
提出日時 2021-08-06 22:52:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,438 bytes
コンパイル時間 2,364 ms
コンパイル使用メモリ 183,936 KB
実行使用メモリ 23,320 KB
最終ジャッジ日時 2023-09-12 02:50:50
合計ジャッジ時間 15,455 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 204 ms
11,020 KB
testcase_05 AC 215 ms
23,320 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 1 ms
4,380 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 12 ms
4,380 KB
testcase_31 AC 97 ms
10,192 KB
testcase_32 AC 85 ms
9,384 KB
testcase_33 AC 53 ms
7,212 KB
testcase_34 AC 69 ms
8,324 KB
testcase_35 AC 54 ms
7,220 KB
testcase_36 AC 12 ms
4,384 KB
testcase_37 AC 17 ms
4,624 KB
testcase_38 AC 90 ms
9,748 KB
testcase_39 AC 35 ms
6,056 KB
testcase_40 AC 35 ms
5,948 KB
testcase_41 AC 71 ms
8,640 KB
testcase_42 AC 40 ms
6,484 KB
testcase_43 AC 42 ms
6,560 KB
testcase_44 AC 42 ms
6,416 KB
testcase_45 AC 29 ms
5,696 KB
testcase_46 AC 14 ms
4,464 KB
testcase_47 AC 9 ms
4,380 KB
testcase_48 AC 90 ms
10,200 KB
testcase_49 AC 5 ms
4,380 KB
testcase_50 AC 1 ms
4,380 KB
testcase_51 AC 2 ms
4,380 KB
testcase_52 AC 266 ms
21,264 KB
testcase_53 AC 266 ms
17,624 KB
07_evil_01.txt WA -
07_evil_02.txt WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: ラムダ関数内:
main.cpp:75:22: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   75 |             for(auto [to, ind] : G[now]){
      |                      ^
main.cpp:84:22: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   84 |             for(auto [to, ind] : G[now]){
      |                      ^

ソースコード

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);
    }

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

    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);
    for(int i = 0; i < N; i++) {
        cin >> A[i] >> B[i];
        A[i]--;
        B[i]--;
        U.unite(A[i], B[i]);
        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, -1);
        vector<bool> use(N, false);
        used.assign(N, false);
        auto dfs = [&](auto dfs, int now) -> void{
            for(auto [to, ind] : G[now]){
                if(use[ind]) continue;
                if(used[to]){
                    ans[ind] = now;
                    use[ind] = true;
                    used[now] = true;
                    break;
                }
            }
            for(auto [to, ind] : G[now]){
                if(use[ind]) continue;
                if(used[to]) continue;
                if(!used[now]){
                    ans[ind] = now;
                    used[now] = true;
                    use[ind] = true;
                }
                dfs(dfs, to);
            }
        };
        for(int i = 0; i < N; i++){
            if(!used[i]) dfs(dfs, i);
        }
        for(int i = 0; i < N; i++) cout << ans[i] + 1 << endl;
    }
}
0