結果

問題 No.1640 簡単な色塗り
ユーザー chocoruskchocorusk
提出日時 2021-08-06 21:42:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 223 ms / 2,000 ms
コード長 2,371 bytes
コンパイル時間 3,565 ms
コンパイル使用メモリ 192,888 KB
実行使用メモリ 16,020 KB
最終ジャッジ日時 2023-09-12 01:45:47
合計ジャッジ時間 15,183 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,132 KB
testcase_01 AC 2 ms
6,064 KB
testcase_02 AC 3 ms
6,964 KB
testcase_03 AC 4 ms
7,592 KB
testcase_04 AC 188 ms
11,420 KB
testcase_05 AC 195 ms
16,020 KB
testcase_06 AC 3 ms
7,232 KB
testcase_07 AC 3 ms
7,180 KB
testcase_08 AC 4 ms
7,240 KB
testcase_09 AC 2 ms
4,584 KB
testcase_10 AC 126 ms
9,836 KB
testcase_11 AC 101 ms
9,676 KB
testcase_12 AC 89 ms
9,040 KB
testcase_13 AC 188 ms
11,772 KB
testcase_14 AC 196 ms
11,464 KB
testcase_15 AC 66 ms
8,996 KB
testcase_16 AC 81 ms
8,824 KB
testcase_17 AC 159 ms
10,676 KB
testcase_18 AC 17 ms
7,552 KB
testcase_19 AC 91 ms
9,356 KB
testcase_20 AC 119 ms
9,948 KB
testcase_21 AC 99 ms
9,488 KB
testcase_22 AC 13 ms
7,380 KB
testcase_23 AC 128 ms
10,220 KB
testcase_24 AC 38 ms
8,160 KB
testcase_25 AC 88 ms
9,092 KB
testcase_26 AC 140 ms
10,396 KB
testcase_27 AC 62 ms
8,944 KB
testcase_28 AC 177 ms
11,500 KB
testcase_29 AC 139 ms
10,360 KB
testcase_30 AC 11 ms
5,672 KB
testcase_31 AC 58 ms
6,044 KB
testcase_32 AC 48 ms
6,168 KB
testcase_33 AC 34 ms
5,796 KB
testcase_34 AC 43 ms
5,500 KB
testcase_35 AC 34 ms
5,068 KB
testcase_36 AC 10 ms
5,636 KB
testcase_37 AC 13 ms
6,076 KB
testcase_38 AC 52 ms
5,876 KB
testcase_39 AC 23 ms
5,692 KB
testcase_40 AC 23 ms
5,664 KB
testcase_41 AC 44 ms
6,300 KB
testcase_42 AC 27 ms
4,736 KB
testcase_43 AC 28 ms
4,760 KB
testcase_44 AC 27 ms
6,076 KB
testcase_45 AC 20 ms
5,792 KB
testcase_46 AC 11 ms
5,832 KB
testcase_47 AC 8 ms
5,688 KB
testcase_48 AC 52 ms
5,928 KB
testcase_49 AC 4 ms
5,624 KB
testcase_50 AC 2 ms
5,548 KB
testcase_51 AC 3 ms
7,776 KB
testcase_52 AC 215 ms
13,796 KB
testcase_53 AC 223 ms
13,800 KB
07_evil_01.txt RE -
07_evil_02.txt RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
struct unionfind{
    vector<int> par, sz;
    unionfind() {}
    unionfind(int n):par(n), sz(n, 1){
        for(int i=0; i<n; i++) par[i]=i;
    }
    int find(int x){
        if(par[x]==x) return x;
        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[x]=y;
        sz[y]+=sz[x];
    }
    bool same(int x, int y){
        return find(x)==find(y);
    }
    int size(int x){
        return sz[find(x)];
    }
};
int main()
{
    int n; cin>>n;
    int a[100010], b[100010];
    unionfind uf(n);
    bool ok[100010]={};
    for(int i=0; i<n; i++){
        cin>>a[i]>>b[i];
        a[i]--; b[i]--;
        if(uf.same(a[i], b[i])){
            ok[i]=1;
        }
        uf.unite(a[i], b[i]);
    }
    int c[100010]={}, e[100010]={};
    for(int i=0; i<n; i++){
        c[uf.find(i)]++;
        e[uf.find(a[i])]++;
    }
    for(int i=0; i<n; i++){
        if(c[i]==0) continue;
        if(c[i]-1==e[i]){
            cout<<"No"<<endl;
            return 0;
        }
    }
    vector<P> g[100010];
    int i0;
    bool used[100010]={};
    int ans[100010];
    auto dfs=[&](auto dfs, int x)->void{
        used[x]=1;
        for(auto q:g[x]){
            if(i0==q.second || used[q.first]) continue;
            int y=q.first;
            dfs(dfs, y);
            ans[q.second]=y;
        }
    };
    for(int i=0; i<n; i++){
        g[a[i]].push_back({b[i], i});
        g[b[i]].push_back({a[i], i});
    }
    for(int i=0; i<n; i++){
        if(used[a[i]]) continue;
        if(!ok[i]) continue;
        i0=i;
        dfs(dfs, a[i]);
        ans[i]=a[i];
    }
    cout<<"Yes"<<endl;
    for(int i=0; i<n; i++) cout<<ans[i]+1<<endl;
    return 0;
}
0