結果

問題 No.1640 簡単な色塗り
ユーザー ゆきのんゆきのん
提出日時 2021-08-06 22:39:29
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 671 ms / 2,000 ms
コード長 3,479 bytes
コンパイル時間 2,853 ms
コンパイル使用メモリ 220,232 KB
実行使用メモリ 40,280 KB
最終ジャッジ日時 2023-09-12 02:41:44
合計ジャッジ時間 21,526 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
6,748 KB
testcase_01 AC 5 ms
6,584 KB
testcase_02 AC 4 ms
6,516 KB
testcase_03 AC 4 ms
6,520 KB
testcase_04 AC 252 ms
18,308 KB
testcase_05 AC 345 ms
40,280 KB
testcase_06 AC 4 ms
6,676 KB
testcase_07 AC 5 ms
6,568 KB
testcase_08 AC 5 ms
6,572 KB
testcase_09 AC 5 ms
6,516 KB
testcase_10 AC 329 ms
19,980 KB
testcase_11 AC 259 ms
17,572 KB
testcase_12 AC 217 ms
16,300 KB
testcase_13 AC 557 ms
27,076 KB
testcase_14 AC 538 ms
27,020 KB
testcase_15 AC 161 ms
13,604 KB
testcase_16 AC 197 ms
15,396 KB
testcase_17 AC 444 ms
23,916 KB
testcase_18 AC 29 ms
8,316 KB
testcase_19 AC 225 ms
16,332 KB
testcase_20 AC 324 ms
19,724 KB
testcase_21 AC 246 ms
17,256 KB
testcase_22 AC 21 ms
7,796 KB
testcase_23 AC 345 ms
20,860 KB
testcase_24 AC 76 ms
10,764 KB
testcase_25 AC 222 ms
16,456 KB
testcase_26 AC 392 ms
22,120 KB
testcase_27 AC 140 ms
13,300 KB
testcase_28 AC 518 ms
25,968 KB
testcase_29 AC 383 ms
22,004 KB
testcase_30 AC 29 ms
9,336 KB
testcase_31 AC 311 ms
24,292 KB
testcase_32 AC 270 ms
22,104 KB
testcase_33 AC 158 ms
17,040 KB
testcase_34 AC 210 ms
19,692 KB
testcase_35 AC 152 ms
17,356 KB
testcase_36 AC 29 ms
9,372 KB
testcase_37 AC 43 ms
10,564 KB
testcase_38 AC 272 ms
22,844 KB
testcase_39 AC 93 ms
13,552 KB
testcase_40 AC 95 ms
13,756 KB
testcase_41 AC 222 ms
20,000 KB
testcase_42 AC 117 ms
14,660 KB
testcase_43 AC 117 ms
15,060 KB
testcase_44 AC 115 ms
14,680 KB
testcase_45 AC 77 ms
12,820 KB
testcase_46 AC 34 ms
9,704 KB
testcase_47 AC 21 ms
8,584 KB
testcase_48 AC 289 ms
23,180 KB
testcase_49 AC 11 ms
7,596 KB
testcase_50 AC 4 ms
6,676 KB
testcase_51 AC 4 ms
6,816 KB
testcase_52 AC 671 ms
34,988 KB
testcase_53 AC 660 ms
35,040 KB
07_evil_01.txt RE -
07_evil_02.txt RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
//#include<atcoder/all>
//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
//using namespace atcoder;
//using namespace boost::multiprecision;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<ll,ll> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const ll mod=1e9 + 7;
const ll LINF=1ll<<60;
const int INF=1<<30;
//int dx[]={0,1,0,-1,0,1,-1,1,-1};
//int dy[]={0,0,1,0,-1,1,-1,-1,1};
int dx[]={0,0,1,-1};
int dy[]={-1,1,0,0};



const int M_N=100001;

struct UnionFind{
    int par[M_N];
    int rank[M_N];
    int sz[M_N];
    int p[M_N];
    bool t[M_N];

    void init(int n){
        for(int i=0;i<n;i++){
            par[i]=i;
            rank[i]=0;
            sz[i]=1;
            p[i]=0;
            t[i] = false;
        }
    }

    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){
            p[x]++;
            return;
        }
        if(rank[x]<rank[y]){
            par[x]=y;
            sz[y]+=sz[x];
            p[y]+=p[x]+1;
            t[y]|=t[x];
        }
        else{
            par[y]=x;
            sz[x]+=sz[y];
            p[x]+=p[y]+1;
            t[x]|=t[y];
            if(rank[x]==rank[y]) rank[x]++;
        }
    }

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

    int getp(int x){
        return p[find(x)];
    }

    bool gett(int x){
        return t[find(x)];
    }

    bool same(int x,int y){
        return find(x)==find(y);
    }
};
vector<int> G[1<<17];
map<pair<int,int>, int> id;
vector<int> Ans(1 << 17, 0);
vector<bool> used(1 << 17, false);
set<int> s;

void dfs(int u, int v){
    for(auto g:G[u]){
        if(g == v) continue;
        if(used[id[{u, g}]]) continue;
        used[id[{u, g}]] = true;
        Ans[id[{u, g}]] = g + 1;
        s.insert(g + 1);
        dfs(g, u);
    }
    return;
}


int main(){
    int n;cin >> n;
    vector<int> a(n),b(n);
    UnionFind uf;
    uf.init(n);
    vector<bool> f(n , false);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
        a[i]--,b[i]--;
        G[a[i]].pb(b[i]);
        G[b[i]].pb(a[i]);
        id[{a[i], b[i]}] = i;
        id[{b[i], a[i]}] = i;
        uf.unite(a[i], b[i]);
        if(uf.getp(a[i]) == uf.size(a[i]) and not uf.gett(a[i])){
            uf.t[uf.find(a[i])] = true;
            f[a[i]] = true;
        }
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
        if(uf.find(i)==i){
            if(uf.getp(i) >= uf.size(i)) ans += uf.size(i);
            else ans += uf.size(i) - 1;
        }
    }
    if(ans == n){
        puts("Yes");
        for (int i = 0; i < n; i++) {
            if(f[i]) dfs(i, i);
        }
        for (int i = 0; i < n; i++) {
            if(Ans[i] == 0){
                if(s.find(a[i] + 1) == s.end()) cout << a[i] + 1 << endl;
                else cout << b[i] + 1 << endl;
            }
            else{
                cout << Ans[i] << endl;
            }
        }
    }
    else puts("No");
    return 0;
}
0