結果

問題 No.1640 簡単な色塗り
ユーザー ゆきのんゆきのん
提出日時 2021-08-06 22:24:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,246 bytes
コンパイル時間 2,648 ms
コンパイル使用メモリ 219,868 KB
実行使用メモリ 43,616 KB
最終ジャッジ日時 2023-09-12 02:24:05
合計ジャッジ時間 19,658 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
12,752 KB
testcase_01 AC 6 ms
12,728 KB
testcase_02 AC 6 ms
12,888 KB
testcase_03 AC 6 ms
12,820 KB
testcase_04 AC 247 ms
23,440 KB
testcase_05 AC 346 ms
43,616 KB
testcase_06 AC 5 ms
10,740 KB
testcase_07 AC 5 ms
10,612 KB
testcase_08 AC 6 ms
12,716 KB
testcase_09 AC 5 ms
12,724 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 29 ms
15,204 KB
testcase_31 AC 280 ms
28,968 KB
testcase_32 AC 241 ms
27,124 KB
testcase_33 AC 138 ms
20,896 KB
testcase_34 AC 193 ms
25,132 KB
testcase_35 AC 145 ms
22,644 KB
testcase_36 AC 29 ms
15,196 KB
testcase_37 AC 39 ms
14,260 KB
testcase_38 AC 252 ms
27,604 KB
testcase_39 AC 81 ms
19,212 KB
testcase_40 AC 86 ms
19,524 KB
testcase_41 AC 203 ms
25,132 KB
testcase_42 AC 96 ms
20,384 KB
testcase_43 AC 107 ms
18,656 KB
testcase_44 AC 102 ms
18,604 KB
testcase_45 AC 68 ms
18,444 KB
testcase_46 AC 33 ms
13,464 KB
testcase_47 AC 21 ms
14,408 KB
testcase_48 AC 252 ms
27,908 KB
testcase_49 AC 12 ms
13,612 KB
testcase_50 AC 5 ms
10,792 KB
testcase_51 AC 6 ms
12,820 KB
testcase_52 WA -
testcase_53 WA -
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=500001;

struct UnionFind{
    int par[M_N];
    int rank[M_N];
    int sz[M_N];
    int p[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;
        }
    }

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

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

    int getp(int x){
        return p[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])) 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