結果

問題 No.1640 簡単な色塗り
ユーザー tokusakuraitokusakurai
提出日時 2021-08-06 22:06:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 106 ms / 2,000 ms
コード長 2,495 bytes
コンパイル時間 4,399 ms
コンパイル使用メモリ 208,972 KB
実行使用メモリ 16,036 KB
最終ジャッジ日時 2023-09-12 02:05:20
合計ジャッジ時間 12,897 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 38 ms
9,708 KB
testcase_05 AC 45 ms
16,036 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 41 ms
7,220 KB
testcase_11 AC 32 ms
6,592 KB
testcase_12 AC 29 ms
6,284 KB
testcase_13 AC 70 ms
9,764 KB
testcase_14 AC 72 ms
9,312 KB
testcase_15 AC 21 ms
5,660 KB
testcase_16 AC 24 ms
5,768 KB
testcase_17 AC 57 ms
8,328 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 28 ms
6,224 KB
testcase_20 AC 41 ms
7,276 KB
testcase_21 AC 33 ms
6,420 KB
testcase_22 AC 4 ms
4,380 KB
testcase_23 AC 47 ms
7,724 KB
testcase_24 AC 11 ms
4,376 KB
testcase_25 AC 29 ms
6,148 KB
testcase_26 AC 50 ms
7,828 KB
testcase_27 AC 20 ms
5,096 KB
testcase_28 AC 70 ms
9,056 KB
testcase_29 AC 50 ms
7,816 KB
testcase_30 AC 7 ms
4,384 KB
testcase_31 AC 71 ms
12,080 KB
testcase_32 AC 59 ms
11,552 KB
testcase_33 AC 41 ms
9,168 KB
testcase_34 AC 53 ms
11,824 KB
testcase_35 AC 32 ms
7,784 KB
testcase_36 AC 8 ms
4,580 KB
testcase_37 AC 11 ms
5,360 KB
testcase_38 AC 57 ms
10,764 KB
testcase_39 AC 23 ms
6,756 KB
testcase_40 AC 24 ms
6,732 KB
testcase_41 AC 56 ms
10,108 KB
testcase_42 AC 24 ms
6,592 KB
testcase_43 AC 30 ms
7,672 KB
testcase_44 AC 30 ms
7,796 KB
testcase_45 AC 19 ms
7,020 KB
testcase_46 AC 10 ms
4,828 KB
testcase_47 AC 6 ms
4,480 KB
testcase_48 AC 66 ms
13,296 KB
testcase_49 AC 3 ms
4,376 KB
testcase_50 AC 1 ms
4,380 KB
testcase_51 AC 1 ms
4,380 KB
testcase_52 AC 106 ms
14,864 KB
testcase_53 AC 103 ms
13,148 KB
07_evil_01.txt AC 199 ms
17,448 KB
07_evil_02.txt AC 305 ms
24,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

struct io_setup{
    io_setup(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

vector<int> u, v;

template<bool directed = false>
struct Graph{
    struct edge{
        int to, id;
        edge(int to, int id) : to(to), id(id) {}
    };

    vector<vector<edge>> es;
    const int n;
    int m;

    vector<int> ans;
    vector<bool> used;

    Graph(int n) : es(n), n(n), m(0), ans(n), used(n, false) {}

    void add_edge(int from, int to){
        es[from].emplace_back(to, m);
        if(!directed) es[to].emplace_back(from, m);
        m++;
    }

    vector<int> ids;

    void dfs1(int now, int pre = -1){
        used[now] = true;
        each(e, es[now]){
            if(e.to == pre) continue;
            if(used[e.to]) ids.eb(e.id);
            else dfs1(e.to, now);
        }
    }

    void dfs2(int now, int pre = -1){
        each(e, es[now]){
            if(e.to == pre || e.id == ids[0]) continue;
            ans[e.id] = e.to;
            dfs2(e.to, now);
        }
    }

    void solve(){
        rep(i, n){
            if(used[i]) continue;
            ids.clear();
            dfs1(i, -1);
            if(sz(ids) != 1 && !(sz(ids) == 2 && ids[0] == ids[1])){
                cout << "No\n";
                return;
            }

            int j = ids[0];
            dfs2(u[j]);
            ans[j] = u[j];
        }

        cout << "Yes\n";
        rep(i, n) cout << ans[i]+1 << '\n';
    }
};

int main(){
    int N; cin >> N;

    Graph G(N);
    u.resize(N), v.resize(N);

    rep(i, N){
        cin >> u[i] >> v[i]; u[i]--, v[i]--;
        G.add_edge(u[i], v[i]);
    }

    G.solve();
}
0