結果
問題 | No.1640 簡単な色塗り |
ユーザー |
![]() |
提出日時 | 2021-08-06 22:31:33 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,810 bytes |
コンパイル時間 | 1,859 ms |
コンパイル使用メモリ | 188,412 KB |
実行使用メモリ | 17,280 KB |
最終ジャッジ日時 | 2024-06-29 15:34:45 |
合計ジャッジ時間 | 9,632 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 WA * 18 |
ソースコード
#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 doubleconst ll mod = 1000000007;//Unionfindstruct unionfind {//自身が親であれば、その集合に属する頂点数に-1を掛けたもの//そうでなければ親のidvector<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]);}}}