結果

問題 No.1640 簡単な色塗り
ユーザー 👑 emthrmemthrm
提出日時 2021-08-06 22:27:17
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 2,545 bytes
コンパイル時間 2,490 ms
コンパイル使用メモリ 214,980 KB
実行使用メモリ 19,224 KB
最終ジャッジ日時 2023-09-12 02:26:27
合計ジャッジ時間 13,208 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 39 ms
9,904 KB
testcase_05 AC 46 ms
19,224 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 50 ms
12,156 KB
testcase_11 AC 40 ms
10,560 KB
testcase_12 AC 33 ms
9,908 KB
testcase_13 AC 94 ms
17,028 KB
testcase_14 AC 98 ms
16,948 KB
testcase_15 AC 25 ms
7,844 KB
testcase_16 AC 30 ms
9,124 KB
testcase_17 AC 77 ms
14,656 KB
testcase_18 AC 6 ms
4,384 KB
testcase_19 AC 35 ms
9,888 KB
testcase_20 AC 54 ms
11,956 KB
testcase_21 AC 40 ms
10,384 KB
testcase_22 AC 5 ms
4,380 KB
testcase_23 AC 56 ms
12,796 KB
testcase_24 AC 13 ms
5,620 KB
testcase_25 AC 36 ms
9,844 KB
testcase_26 AC 68 ms
13,744 KB
testcase_27 AC 24 ms
7,576 KB
testcase_28 AC 88 ms
16,436 KB
testcase_29 AC 59 ms
13,552 KB
testcase_30 AC 9 ms
5,416 KB
testcase_31 AC 102 ms
18,528 KB
testcase_32 AC 79 ms
16,748 KB
testcase_33 AC 44 ms
12,360 KB
testcase_34 AC 63 ms
14,536 KB
testcase_35 AC 48 ms
12,308 KB
testcase_36 AC 10 ms
5,416 KB
testcase_37 AC 13 ms
6,196 KB
testcase_38 AC 78 ms
17,288 KB
testcase_39 AC 26 ms
9,140 KB
testcase_40 AC 25 ms
9,332 KB
testcase_41 AC 67 ms
14,860 KB
testcase_42 AC 30 ms
10,204 KB
testcase_43 AC 35 ms
10,420 KB
testcase_44 AC 33 ms
10,236 KB
testcase_45 AC 21 ms
8,780 KB
testcase_46 AC 11 ms
5,692 KB
testcase_47 AC 8 ms
4,876 KB
testcase_48 AC 87 ms
17,504 KB
testcase_49 AC 4 ms
4,376 KB
testcase_50 AC 1 ms
4,376 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 AC 102 ms
19,084 KB
testcase_53 AC 105 ms
19,012 KB
07_evil_01.txt AC 267 ms
34,648 KB
07_evil_02.txt AC 428 ms
49,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

struct UnionFind {
  UnionFind(int n) : data(n, -1) {}

  int root(int ver) { return data[ver] < 0 ? ver : data[ver] = root(data[ver]); }

  bool unite(int u, int v) {
    u = root(u);
    v = root(v);
    if (u == v) return false;
    if (data[u] > data[v]) std::swap(u, v);
    data[u] += data[v];
    data[v] = u;
    return true;
  }

  bool same(int u, int v) { return root(u) == root(v); }

  int size(int ver) { return -data[root(ver)]; }

private:
  std::vector<int> data;
};

int main() {
  int n; cin >> n;
  vector<int> a(n), b(n); REP(i, n) cin >> a[i] >> b[i], --a[i], --b[i];
  vector<int> painted(n, false), ans(n, -1);
  vector<set<int>> cand(n);
  UnionFind uf(n);
  REP(i, n) {
    if (painted[a[i]] && painted[b[i]]) {
      cout << "No\n";
      return 0;
    }
    int choice = -1;
    if (painted[a[i]]) {
      choice = b[i];
    } else if (painted[b[i]] || !uf.unite(a[i], b[i])) {
      choice = a[i];
    } else {
      cand[a[i]].emplace(i);
      cand[b[i]].emplace(i);
    }
    if (choice == -1) continue;
    painted[choice] = true;
    ans[i] = choice;
    queue<int> que({choice});
    while (!que.empty()) {
      int ball = que.front(); que.pop();
      for (int id : cand[ball]) {
        const int target = (a[id] == ball ? b : a)[id];
        if (painted[target]) {
          cout << "No\n";
          return 0;
        }
        painted[target] = true;
        ans[id] = target;
        cand[target].erase(id);
        que.emplace(target);
      }
    }
  }
  if (count(ALL(painted), false) > 0) {
    cout << "No\n";
  } else {
    cout << "Yes\n";
    REP(i, n) cout << ans[i] + 1 << '\n';
  }
  return 0;
}
0