結果

問題 No.1640 簡単な色塗り
ユーザー tokusakuraitokusakurai
提出日時 2021-08-06 22:02:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,456 bytes
コンパイル時間 2,331 ms
コンパイル使用メモリ 208,132 KB
実行使用メモリ 15,972 KB
最終ジャッジ日時 2023-09-12 01:55:18
合計ジャッジ時間 10,401 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 WA -
testcase_09 AC 2 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 7 ms
4,376 KB
testcase_31 AC 65 ms
12,036 KB
testcase_32 AC 58 ms
11,488 KB
testcase_33 AC 29 ms
7,796 KB
testcase_34 AC 49 ms
11,820 KB
testcase_35 AC 28 ms
7,732 KB
testcase_36 AC 7 ms
4,376 KB
testcase_37 AC 11 ms
5,352 KB
testcase_38 AC 53 ms
10,644 KB
testcase_39 AC 18 ms
6,304 KB
testcase_40 AC 19 ms
6,676 KB
testcase_41 AC 46 ms
10,108 KB
testcase_42 AC 22 ms
6,516 KB
testcase_43 AC 26 ms
7,608 KB
testcase_44 AC 22 ms
6,496 KB
testcase_45 AC 18 ms
6,980 KB
testcase_46 AC 9 ms
4,380 KB
testcase_47 AC 6 ms
4,376 KB
testcase_48 AC 70 ms
13,096 KB
testcase_49 AC 3 ms
4,376 KB
testcase_50 AC 1 ms
4,376 KB
testcase_51 WA -
testcase_52 AC 99 ms
14,868 KB
testcase_53 AC 96 ms
13,076 KB
07_evil_01.txt WA -
07_evil_02.txt WA -
権限があれば一括ダウンロードができます

ソースコード

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){
                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