結果

問題 No.1640 簡単な色塗り
ユーザー carrot46carrot46
提出日時 2021-08-06 22:57:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 2,022 bytes
コンパイル時間 2,769 ms
コンパイル使用メモリ 214,380 KB
実行使用メモリ 14,456 KB
最終ジャッジ日時 2023-09-12 03:05:00
合計ジャッジ時間 14,466 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 153 ms
7,636 KB
testcase_05 AC 168 ms
14,456 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 103 ms
7,384 KB
testcase_11 AC 85 ms
6,448 KB
testcase_12 AC 78 ms
6,156 KB
testcase_13 AC 160 ms
9,492 KB
testcase_14 AC 164 ms
9,444 KB
testcase_15 AC 56 ms
5,096 KB
testcase_16 AC 69 ms
5,748 KB
testcase_17 AC 138 ms
8,328 KB
testcase_18 AC 13 ms
4,376 KB
testcase_19 AC 75 ms
6,280 KB
testcase_20 AC 104 ms
7,332 KB
testcase_21 AC 84 ms
6,476 KB
testcase_22 AC 10 ms
4,376 KB
testcase_23 AC 114 ms
7,580 KB
testcase_24 AC 32 ms
4,380 KB
testcase_25 AC 76 ms
6,260 KB
testcase_26 AC 132 ms
7,916 KB
testcase_27 AC 55 ms
5,088 KB
testcase_28 AC 162 ms
9,072 KB
testcase_29 AC 125 ms
7,732 KB
testcase_30 AC 8 ms
4,376 KB
testcase_31 AC 66 ms
12,756 KB
testcase_32 AC 48 ms
11,700 KB
testcase_33 AC 30 ms
8,632 KB
testcase_34 AC 42 ms
11,028 KB
testcase_35 AC 31 ms
8,784 KB
testcase_36 AC 8 ms
4,404 KB
testcase_37 AC 11 ms
5,104 KB
testcase_38 AC 56 ms
10,952 KB
testcase_39 AC 19 ms
6,848 KB
testcase_40 AC 21 ms
6,424 KB
testcase_41 AC 45 ms
9,648 KB
testcase_42 AC 22 ms
6,684 KB
testcase_43 AC 24 ms
8,120 KB
testcase_44 AC 23 ms
7,384 KB
testcase_45 AC 17 ms
6,580 KB
testcase_46 AC 9 ms
4,580 KB
testcase_47 AC 6 ms
4,380 KB
testcase_48 AC 61 ms
11,968 KB
testcase_49 AC 3 ms
4,376 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 1 ms
4,380 KB
testcase_52 AC 193 ms
11,960 KB
testcase_53 AC 198 ms
12,084 KB
07_evil_01.txt AC 435 ms
17,448 KB
07_evil_02.txt AC 644 ms
24,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/dsu>
using namespace std;
using namespace atcoder;
//#pragma GCC optimize("Ofast")
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
using tp = tuple<ll, ll, ll>;
const ll INF = (1LL<<60);

template<class T> bool chmin(T &a, const T b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T b){
    if(a < b) {a = b; return true;}
    else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
    if(!v.empty()){
        for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
        std::cout<<v.back();
    }
    if(endline) std::cout<<std::endl;
}

struct edge{
        int to, id;
        edge(){}
        edge(int a, int b) : to(a), id(b){}
    };
using ve = vector<edge>;


int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin>>N;
    vec res(N, -1), roots;
    vector<bool> visited(N, false);
    vector<ve> G(N);
    dsu d(N);
    rep(i, N){
        int a, b;
        cin>>a>>b;
        --a; --b;
        if(d.same(a, b)){
            roots.push_back(a);
            res[i] = a;
        }else {
            d.merge(a, b);
            G[a].emplace_back(b, i);
            G[b].emplace_back(a, i);
        }
    }
    auto dfs = [&](auto &&self, edge p) -> void{
        if(p.id != -1) res[p.id] = p.to;
        visited[p.to] = true;
        for(edge e : G[p.to]) if(!visited[e.to]) {
            self(self, e);
        }
    };
    for(int p : roots) {
        dfs(dfs, edge(p, -1));
    }
    if(*min_element(ALL(res)) == -1 || *min_element(ALL(visited)) == false){
        cout<<"No"<<endl;
    }else {
        cout<<"Yes"<<endl;
        rep(i, N) cout << res[i] + 1 << endl;
    }
}
0