結果
問題 | No.1640 簡単な色塗り |
ユーザー | platinum |
提出日時 | 2021-06-19 13:24:06 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 300 ms / 2,000 ms |
コード長 | 2,527 bytes |
コンパイル時間 | 2,546 ms |
コンパイル使用メモリ | 205,264 KB |
最終ジャッジ日時 | 2025-01-22 10:01:47 |
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 51 RE * 2 |
ソースコード
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (int)(n); i++) using namespace std; using LL = long long; using P = pair<int,int>; using vv = vector<vector<int>>; const int INF = (int)1e9; const LL LINF = (LL)1e18; const int Max_N = (int)1e5; class UnionFind { public: vector <long long> par; vector <long long> siz; UnionFind(long long sz_): par(sz_), siz(sz_, (long long)1) { for (long long i = 0; i < sz_; ++i) par[i] = i; } void init(long long sz_) { par.resize(sz_); siz.assign(sz_, (long long)1); for (long long i = 0; i < sz_; ++i) par[i] = i; } long long root(long long x) { while (par[x] != x) { x = par[x] = par[par[x]]; } return x; } bool unite(long long x, long long y) { x = root(x); y = root(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } bool same(long long x, long long y) { return root(x) == root(y); } long long size(long long x) { return siz[root(x)]; } }; struct edge{ int to, num; edge(int to, int num) : to(to), num(num) {} }; vector<vector<edge>> G; int con[100010], visited[100010], ans[100010]; int st = -1, erase_num = -1; bool fin = false; void dfs(int n, int par = -1){ for(auto e : G[n]){ if(e.to == par) continue; if(e.num == erase_num) continue; ans[e.num] = e.to + 1; dfs(e.to, n); } } void connect(int n){ con[n] = 1; for(auto e : G[n]){ if(con[e.to]) continue; connect(e.to); } } void cycle(int n, int edge_prev = -1){ visited[n] = 1; for(auto e : G[n]){ if(fin) return; if(e.num == edge_prev) continue; if(visited[e.to]){ st = e.to; ans[e.num] = e.to + 1; erase_num = e.num; fin = true; break; } cycle(e.to, e.num); } } int main(){ int N; cin >> N; G.resize(N); assert(1 <= N and N <= Max_N); vector<int> A(N); UnionFind uf(N); rep(i,N){ int a, b; cin >> a >> b; assert(1 <= a and a <= N); assert(1 <= b and b <= N); a--; b--; uf.unite(a, b); A[i] = a; G[a].emplace_back(b, i); G[b].emplace_back(a, i); } vector<int> edge_num(N); rep(i,N){ int p = uf.root(A[i]); edge_num[p]++; } rep(i,N){ if(uf.root(i) != i) continue; int siz = uf.size(i); if(siz != edge_num[i]){ cout << "No" << endl; return 0; } } rep(i,N){ if(!con[i]){ connect(i); fin = false; cycle(i); //rep(j,G[st].size()) cout << G[st][j].to << endl; dfs(st); } } //cout << st << " " << erase_num << endl; cout << "Yes" << endl; rep(i,N) cout << ans[i] << endl; return 0; }