結果

問題 No.1640 簡単な色塗り
ユーザー SumitacchanSumitacchan
提出日時 2021-08-06 22:07:25
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 229 ms / 2,000 ms
コード長 4,086 bytes
コンパイル時間 2,667 ms
コンパイル使用メモリ 212,008 KB
実行使用メモリ 17,104 KB
最終ジャッジ日時 2023-09-12 02:08:08
合計ジャッジ時間 15,930 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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,388 KB
testcase_04 AC 153 ms
8,712 KB
testcase_05 AC 171 ms
17,104 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 115 ms
9,572 KB
testcase_11 AC 96 ms
8,592 KB
testcase_12 AC 83 ms
8,296 KB
testcase_13 AC 183 ms
13,288 KB
testcase_14 AC 192 ms
13,368 KB
testcase_15 AC 58 ms
6,484 KB
testcase_16 AC 72 ms
7,208 KB
testcase_17 AC 150 ms
11,656 KB
testcase_18 AC 14 ms
4,380 KB
testcase_19 AC 80 ms
7,800 KB
testcase_20 AC 113 ms
9,680 KB
testcase_21 AC 89 ms
8,488 KB
testcase_22 AC 10 ms
4,380 KB
testcase_23 AC 121 ms
10,212 KB
testcase_24 AC 33 ms
5,144 KB
testcase_25 AC 84 ms
7,980 KB
testcase_26 AC 135 ms
10,632 KB
testcase_27 AC 56 ms
6,516 KB
testcase_28 AC 179 ms
13,008 KB
testcase_29 AC 135 ms
10,624 KB
testcase_30 AC 10 ms
5,052 KB
testcase_31 AC 107 ms
16,316 KB
testcase_32 AC 89 ms
14,388 KB
testcase_33 AC 51 ms
10,820 KB
testcase_34 AC 75 ms
13,312 KB
testcase_35 AC 45 ms
10,920 KB
testcase_36 AC 10 ms
4,964 KB
testcase_37 AC 14 ms
5,984 KB
testcase_38 AC 83 ms
14,328 KB
testcase_39 AC 28 ms
7,996 KB
testcase_40 AC 27 ms
8,124 KB
testcase_41 AC 65 ms
12,484 KB
testcase_42 AC 31 ms
8,520 KB
testcase_43 AC 35 ms
9,440 KB
testcase_44 AC 34 ms
9,092 KB
testcase_45 AC 25 ms
7,608 KB
testcase_46 AC 11 ms
5,456 KB
testcase_47 AC 7 ms
4,640 KB
testcase_48 AC 105 ms
14,908 KB
testcase_49 AC 4 ms
4,380 KB
testcase_50 AC 2 ms
4,384 KB
testcase_51 AC 2 ms
4,380 KB
testcase_52 AC 229 ms
15,936 KB
testcase_53 AC 223 ms
15,928 KB
07_evil_01.txt AC 459 ms
25,864 KB
07_evil_02.txt AC 706 ms
37,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
//using namespace atcoder;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define REP(i, n) FOR(i,0,n)
#define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--)
#define IREP(i, n) IFOR(i,0,n)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define all(v) v.begin(),v.end()
#define SZ(v) ((int)v.size())
#define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define bit(n) (1LL<<(n))
#define debug(x) cout << #x << "=" << x << endl;
#define vdebug(v) { cout << #v << "=" << endl; REP(i_debug, v.size()){ cout << v[i_debug] << ","; } cout << endl; }
#define mdebug(m) { cout << #m << "=" << endl; REP(i_debug, m.size()){ REP(j_debug, m[i_debug].size()){ cout << m[i_debug][j_debug] << ","; } cout << endl;} }
#define pb push_back
#define fi first
#define se second
#define int long long
#define INF 1000000000000000000
template<typename T> istream &operator>>(istream &is, vector<T> &v){ for (auto &x : v) is >> x; return is; }
template<typename T> ostream &operator<<(ostream &os, vector<T> &v){ for(int i = 0; i < v.size(); i++) { cout << v[i]; if(i != v.size() - 1) cout << endl; }; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p){ cout << '(' << p.first << ',' << p.second << ')'; return os; }
template<typename T> void Out(T x) { cout << x << endl; }
template<typename T1, typename T2> void chOut(bool f, T1 y, T2 n) { if(f) Out(y); else Out(n); }

using vec = vector<int>;
using mat = vector<vec>;
using Pii = pair<int, int>;
using v_bool = vector<bool>;
using v_Pii = vector<Pii>;

//int dx[4] = {1,0,-1,0};
//int dy[4] = {0,1,0,-1};
//char d[4] = {'D','R','U','L'};

const int mod = 1000000007;
//const int mod = 998244353;

struct UnionFind
{
    vector<int> par, sz;
    int n_cc;

    UnionFind(int N){
        par = vector<int>(N);
        REP(i, N) par[i] = i;
        sz = vector<int>(N, 1);
        n_cc = N;
    }

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

    void unite(int x, int y){
        x = find(x);
        y = find(y);
        if(x == y) return;

        if(sz[x] < sz[y]) swap(x, y);

        par[y] = x;
        sz[x] += sz[y];
        n_cc--;
    }

    bool is_union(int x, int y){
        x = find(x);
        y = find(y);
        return x == y;
    }

    int union_size(int x){
        return sz[find(x)];
    }

};

template<typename T> 
struct edge{ int to; T cost; int id; };

template<typename T = long long>
struct Graph
{

    int N;
    vector<vector<edge<T>>> E;

    Graph(int N): N(N){
        E = vector<vector<edge<T>>>(N, vector<edge<T>>(0));
    }

    void add_Directed_edge(int from, int to, T cost = 1, int id = 0){
        E[from].push_back(edge<T>({ to, cost, id }));
    }

    void add_Undirected_edge(int v1, int v2, T cost = 1, int id = 0){
        add_Directed_edge(v1, v2, cost, id);
        add_Directed_edge(v2, v1, cost, id);
    }

    void dfs(int v, vec &ans, int p = -1){
        for(auto e: E[v]) if(e.to != p){
            ans[e.id] = e.to;
            dfs(e.to, ans, v);
        }
    }

};


signed main(){

    int N; cin >> N;
    vec ans(N, -1);
    
    Graph G(N);
    UnionFind UF(N);
    vec v0s;
    REP(i, N){
        int a, b; cin >> a >> b;
        a--; b--;
        if(UF.is_union(a, b)){
            ans[i] = a;
            v0s.pb(a);
        }else{
            UF.unite(a, b);
            G.add_Undirected_edge(a, b, 1, i);
        }
    }

    for(int v: v0s) G.dfs(v, ans);

    v_bool used(N, false);
    REP(i, N) if(ans[i] >= 0) used[ans[i]] = true;
    REP(i, N) if(!used[i]){
        Out("No");
        return 0;
    }

    Out("Yes");
    for(int x: ans) Out(x + 1);

    return 0;
}
0