結果

問題 No.1640 簡単な色塗り
ユーザー rianoriano
提出日時 2021-08-06 22:31:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,810 bytes
コンパイル時間 2,053 ms
コンパイル使用メモリ 185,716 KB
実行使用メモリ 17,140 KB
最終ジャッジ日時 2023-09-12 02:29:58
合計ジャッジ時間 11,123 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 209 ms
17,128 KB
testcase_05 AC 209 ms
17,140 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 131 ms
11,064 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 89 ms
8,856 KB
testcase_16 AC 113 ms
10,208 KB
testcase_17 WA -
testcase_18 AC 18 ms
4,540 KB
testcase_19 AC 130 ms
10,956 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 12 ms
4,380 KB
testcase_23 AC 206 ms
14,424 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 7 ms
4,452 KB
testcase_31 AC 50 ms
11,352 KB
testcase_32 AC 46 ms
10,472 KB
testcase_33 AC 27 ms
8,288 KB
testcase_34 AC 37 ms
9,360 KB
testcase_35 AC 30 ms
8,448 KB
testcase_36 AC 7 ms
4,444 KB
testcase_37 AC 9 ms
4,840 KB
testcase_38 AC 45 ms
10,688 KB
testcase_39 AC 18 ms
6,256 KB
testcase_40 AC 18 ms
6,420 KB
testcase_41 AC 40 ms
9,324 KB
testcase_42 AC 21 ms
7,024 KB
testcase_43 AC 21 ms
6,928 KB
testcase_44 AC 21 ms
6,996 KB
testcase_45 AC 16 ms
5,964 KB
testcase_46 AC 8 ms
4,472 KB
testcase_47 AC 5 ms
4,376 KB
testcase_48 AC 51 ms
10,984 KB
testcase_49 AC 3 ms
4,376 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
07_evil_01.txt WA -
07_evil_02.txt WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define Pr pair<ll,ll>
#define Tp tuple<ll,ll,ll>
using Graph = vector<vector<int>>;
#define double long double

const ll mod = 1000000007;


//Unionfind
struct unionfind {
	//自身が親であれば、その集合に属する頂点数に-1を掛けたもの
	//そうでなければ親のid
	  vector<long long> r;
      vector<bool> act;
	  unionfind(long long N) {
		    r = vector<long long>(N, -1);
            act = vector<bool>(N,false);
	  }
	  long long root(long long x) {
		    if (r[x] < 0) return x;
		    return r[x] = root(r[x]);
	  }
	  bool unite(long long x, long long y) {
		    x = root(x);
		    y = root(y);
		    if (x == y) return false;
		    if (r[x] > r[y]) swap(x, y);
		    r[x] += r[y];
		    r[y] = x;
		    return true;
	  }
	  long long size(long long x) {
		    return max(-r[root(x)],0LL);
	  }
  
  // 2つのデータx, yが属する木が同じならtrueを返す
    bool same(long long x, long long y) { 
        long long rx = root(x);
        long long ry = root(y);
        if(rx==ry){
            act[rx] = true;
        }
        return rx == ry;
    }
    bool activate(ll x){
        long long rx = root(x);
        return act[rx];
    }
};
  //main関数内で unionfind friends(N+1);
  //などと宣言し  friends.unite(a,b);
  //のように使う
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    ll N; cin >> N;
    ll a[N],b[N];
    ll cnt[N+1];
    rep(i,N) cnt[i+1] = 0;
    Graph G(N+1);
    rep(i,N){
        cin >> a[i] >> b[i];
        G[a[i]].push_back(i);
        G[b[i]].push_back(i);
        cnt[a[i]]++; cnt[b[i]]++;
    }
    unionfind balls(N+1);
    rep(i,N){
        if(!balls.same(a[i],b[i])){
            balls.unite(a[i],b[i]);
        }
    }
    rep(i,N){
        if(!balls.activate(i+1)){
            cout << "No" << endl; return 0;
        }
    }
    cout << "Yes" << endl;
    set<ll> used,app;
    ll ans[N];
    rep(i,N) ans[i] = -1;
    queue<ll> go;
    rep(i,N){
        if(cnt[i+1]==1){
            go.push(i+1);
        }
    }
    while(!go.empty()){
        ll s = go.front(); go.pop();
        for(ll x:G[s]){
            if(used.count(x)) continue;
            ans[x] = s; app.insert(s);
            cnt[a[x]]--; cnt[b[x]]--;
            if(cnt[a[x]]==1){
                go.push(a[x]);
            }
            if(cnt[b[x]]==1){
                go.push(b[x]);
            }
            used.insert(x);
        }
    }
    rep(i,N){
        if(ans[i]>=0) cout << ans[i] << endl;
        else{
            if(app.count(a[i])){
            cout << b[i] << endl;
            app.insert(b[i]);
        }
        cout << a[i] << endl;
        app.insert(a[i]);
        }
    }
}
0