結果
問題 |
No.1640 簡単な色塗り
|
ユーザー |
![]() |
提出日時 | 2021-08-08 00:40:54 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,694 bytes |
コンパイル時間 | 3,772 ms |
コンパイル使用メモリ | 269,844 KB |
実行使用メモリ | 25,904 KB |
最終ジャッジ日時 | 2024-06-29 17:03:13 |
合計ジャッジ時間 | 17,564 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 26 WA * 27 |
ソースコード
#include <bits/stdc++.h> #include <atcoder/dsu> using namespace std; using namespace atcoder; using graph = vector<vector<pair<int, int>>>; vector<int> a, b; int cycle(const graph &to, vector<bool> &seen, int v, int e) { seen.at(v) = true; for (auto &&[c, e_idx] : to.at(v)) { if (e_idx == e) { continue; } if (seen.at(c)) { return e_idx; } int res = cycle(to, seen, c, e_idx); if (res != -1) { return res; } } return -1; } void dfs(const graph &to, vector<int> &ans, vector<bool> &rev, int v) { for (auto &&[c, e_idx] : to.at(v)) { if (ans.at(e_idx) != -1) { continue; } if (rev.at(a.at(e_idx))) { ans.at(e_idx) = b.at(e_idx); } else { ans.at(e_idx) = a.at(e_idx); } dfs(to, ans, rev, c); } } int main() { int n; cin >> n; a = vector<int>(n); b = vector<int>(n); for (int i = 0; i < n; i++) { cin >> a.at(i) >> b.at(i); a.at(i)--; b.at(i)--; } dsu t(n); for (int i = 0; i < n; i++) { t.merge(a.at(i), b.at(i)); } auto gs = t.groups(); int sz = gs.size(); vector<int> idx(n), idx_in(n); vector<graph> to(sz); for (int i = 0; i < sz; i++) { int sz_in = gs.at(i).size(); to.at(i) = graph(sz_in); for (int j = 0; j < sz_in; j++) { idx.at(gs.at(i).at(j)) = i; idx_in.at(gs.at(i).at(j)) = j; } } for (int i = 0; i < n; i++) { int k = idx.at(a.at(i)), x = idx_in.at(a.at(i)), y = idx_in.at(b.at(i)); assert(k == idx.at(b.at(i))); to.at(k).at(x).emplace_back(y, i); to.at(k).at(y).emplace_back(x, i); } for (int i = 0; i < sz; i++) { graph to_in = to.at(i); int k = to_in.size(), s = 0; for (int j = 0; j < k; j++) { s += to_in.at(j).size(); } if (s != 2 * k) { puts("No"); return 0; } } puts("Yes"); vector<int> ans(n, -1); vector<bool> rev(n); for (int i = 0; i < sz; i++) { auto gs_in = gs.at(i); int sz_in = gs_in.size(); graph to_in = to.at(i); vector<bool> seen(sz_in); int e_idx = cycle(to_in, seen, 0, -1); ans.at(e_idx) = a.at(e_idx); rev.at(a.at(e_idx)) = true; dfs(to_in, ans, rev, idx_in.at(a.at(e_idx))); } for (int i = 0; i < n; i++) { cout << ans.at(i) + 1 << endl; } return 0; }