結果
問題 |
No.1640 簡単な色塗り
|
ユーザー |
![]() |
提出日時 | 2021-08-06 22:56:04 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,933 bytes |
コンパイル時間 | 2,559 ms |
コンパイル使用メモリ | 208,768 KB |
最終ジャッジ日時 | 2025-01-23 16:07:34 |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 38 WA * 15 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:150:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 150 | scanf("%d", &n); | ~~~~~^~~~~~~~~~ main.cpp:154:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 154 | scanf("%d%d", &a, &b); | ~~~~~^~~~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h> #include <atcoder/dsu> #include <vector> template <class T = int> struct Edge { int from, to; T cost; int idx; Edge() = default; Edge(int from, int to, T cost = 1, int idx = 0) : from(from), to(to), cost(cost), idx(idx) {} }; template <class T = int> struct Graph { std::vector<std::vector<Edge<T>>> es; int edge_num; Graph(int n) : edge_num(0) { es.resize(n); } int size() { return es.size(); } void add_edge(int from, int to, T cost = 1, int idx = 0) { es[from].emplace_back(from, to, cost, idx); ++edge_num; } std::vector<Edge<T>> edges() { std::vector<Edge<T>> tmp; for (int v = 0; v < (int)es.size(); ++v) { for (auto& e : es[v]) { tmp.push_back(e); } } if (tmp.size() >= 2 && tmp[0].idx == tmp[1].idx) { return tmp; } std::vector<Edge<T>> res(edge_num); for (auto& e : tmp) res[e.idx] = e; return res; } std::vector<Edge<T>>& operator[](int i) { return es[i]; } }; #include <algorithm> #include <cassert> #include <vector> template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <class T> T div_floor(T a, T b) { if (b < 0) a *= -1, b *= -1; return a >= 0 ? a / b : (a + 1) / b - 1; } template <class T> T div_ceil(T a, T b) { if (b < 0) a *= -1, b *= -1; return a > 0 ? (a - 1) / b + 1 : a / b; } template <typename T> struct CoordComp { std::vector<T> v; bool sorted; CoordComp() : sorted(false) {} int size() { return v.size(); } void add(T x) { v.push_back(x); } void build() { std::sort(v.begin(), v.end()); v.erase(std::unique(v.begin(), v.end()), v.end()); sorted = true; } int get_idx(T x) { assert(sorted); return lower_bound(v.begin(), v.end(), x) - v.begin(); } T &operator[](int i) { return v[i]; } }; #define For(i, a, b) for (int i = (int)(a); (i) < (int)(b); ++(i)) #define rFor(i, a, b) for (int i = (int)(a)-1; (i) >= (int)(b); --(i)) #define rep(i, n) For(i, 0, n) #define rrep(i, n) rFor(i, n, 0) #define fi first #define se second using namespace std; using lint = long long; using pii = pair<int, int>; using pll = pair<lint, lint>; using namespace atcoder; int get_back(int v, int pv, Graph<> &gr, vector<bool> &used) { used[v] = true; for (auto &e : gr[v]) { if (e.to == pv) continue; if (used[e.to]) return e.idx; int tmp = get_back(e.to, v, gr, used); if (tmp >= 0) return tmp; } return -1; } void dfs(int v, int pv, Graph<> &gr, vector<int> &col, vector<int> &ans) { for (auto &e : gr[v]) { if (e.to == pv || col[e.to]) continue; col[e.to] = 1; ans[e.idx] = e.to; dfs(e.to, v, gr, col, ans); } } int main() { int n; scanf("%d", &n); Graph<int> gr(n); rep(i, n) { int a, b; scanf("%d%d", &a, &b); --a; --b; gr.add_edge(a, b, 1, i); gr.add_edge(b, a, 1, i); } auto es = gr.edges(); dsu uf(n); for (auto &e : es) uf.merge(e.from, e.to); vector<int> cnt(n, 0); for (auto &e : es) ++cnt[uf.leader(e.from)]; rep(i, n) if (i == uf.leader(i)) { if (uf.size(i) > cnt[i]) { puts("No"); return 0; } } vector<int> col(n, 0), ans(es.size(), -1); vector<bool> used(n, false); rep(i, n) if (col[i] == 0) { int b = get_back(i, -1, gr, used); ans[b] = es[b].from; col[es[b].from] = 1; dfs(es[b].from, -1, gr, col, ans); } puts("Yes"); rep(i, n) printf("%d\n", ans[i] + 1); }