結果

問題 No.1640 簡単な色塗り
ユーザー masutech16masutech16
提出日時 2021-08-07 21:47:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 3,888 bytes
コンパイル時間 2,228 ms
コンパイル使用メモリ 209,944 KB
実行使用メモリ 20,124 KB
最終ジャッジ日時 2023-09-12 03:59:31
合計ジャッジ時間 14,204 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,376 KB
testcase_04 AC 163 ms
10,476 KB
testcase_05 AC 175 ms
20,124 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 107 ms
7,836 KB
testcase_11 AC 86 ms
7,000 KB
testcase_12 AC 77 ms
6,624 KB
testcase_13 AC 163 ms
10,340 KB
testcase_14 AC 158 ms
10,152 KB
testcase_15 AC 56 ms
5,724 KB
testcase_16 AC 67 ms
6,140 KB
testcase_17 AC 133 ms
9,044 KB
testcase_18 AC 13 ms
4,380 KB
testcase_19 AC 75 ms
6,520 KB
testcase_20 AC 101 ms
7,468 KB
testcase_21 AC 82 ms
6,792 KB
testcase_22 AC 9 ms
4,504 KB
testcase_23 AC 110 ms
8,140 KB
testcase_24 AC 31 ms
4,380 KB
testcase_25 AC 76 ms
6,420 KB
testcase_26 AC 119 ms
8,568 KB
testcase_27 AC 53 ms
5,364 KB
testcase_28 AC 156 ms
9,844 KB
testcase_29 AC 119 ms
8,376 KB
testcase_30 AC 9 ms
4,628 KB
testcase_31 AC 62 ms
16,716 KB
testcase_32 AC 49 ms
13,640 KB
testcase_33 AC 33 ms
10,688 KB
testcase_34 AC 40 ms
14,176 KB
testcase_35 AC 33 ms
11,380 KB
testcase_36 AC 9 ms
4,924 KB
testcase_37 AC 12 ms
6,144 KB
testcase_38 AC 53 ms
13,336 KB
testcase_39 AC 22 ms
8,000 KB
testcase_40 AC 21 ms
7,572 KB
testcase_41 AC 43 ms
11,748 KB
testcase_42 AC 23 ms
7,880 KB
testcase_43 AC 24 ms
9,188 KB
testcase_44 AC 25 ms
8,908 KB
testcase_45 AC 19 ms
8,196 KB
testcase_46 AC 10 ms
5,436 KB
testcase_47 AC 7 ms
4,728 KB
testcase_48 AC 52 ms
15,796 KB
testcase_49 AC 3 ms
4,380 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 1 ms
4,376 KB
testcase_52 AC 189 ms
17,192 KB
testcase_53 AC 189 ms
15,480 KB
07_evil_01.txt AC 404 ms
19,136 KB
07_evil_02.txt AC 650 ms
26,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "/workspaces/compro/lib/template.hpp"


#line 1 "/workspaces/compro/lib/io/vector.hpp"
#include <iostream>
#include <vector>

#ifndef IO_VECTOR
#define IO_VECTOR

template <class T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  int size = v.size();
  for (int i = 0; i < size; i++) {
    std::cout << v[i];
    if (i != size - 1)
      std::cout << " ";
  }
  return out;
}

template <class T> std::istream &operator>>(std::istream &in, std::vector<T> &v) {
  for (auto &el : v) {
    std::cin >> el;
  }
  return in;
}

#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());
}


#line 2 "main.cpp"

const std::string YES = "Yes";
const std::string NO = "No";

// generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator)
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  std::cin >> N;
  std::vector<long long> A(N), B(N);
  for (int i = 0; i < N; ++i) {
    std::cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
  }

  vi chosen(N, 0);
  vector<vector<pair<int, int>>> g(N);
  REP(i, N) {
    g[A[i]].push_back({B[i], i});
    g[B[i]].push_back({A[i], i});
  }

  vector<bool> checked(N, false); // 頂点iが選ばれたか
  auto dfs = [&](int cur, int par, auto self) -> void {
    // curは既に選ばれているとする
    for (const auto &el : g[cur]) {
      if (el.first == par)
        continue;
      if (checked[el.first])
        continue;
      if (chosen[el.second] != 0)
        continue;
      chosen[el.second] = el.first + 1;
      checked[el.first] = true;
      self(el.first, cur, self);
    }
  };

  // ループ検出用
  vector<bool> visited(N, false);
  // ループに含まれている適当な頂点を返す
  auto check = [&](int cur, int par, auto self) -> int {
    if (visited[cur]) {
      // par -> cur の辺を探す
      for (const auto &el : g[cur]) {
        if (el.first == par) {
          // それを選ぶ
          checked[cur] = true;
          chosen[el.second] = cur + 1;
          return cur;
        }
      }
      return cur;
    }
    visited[cur] = true;
    for (const auto &el : g[cur]) {
      if (el.first == par)
        continue;
      auto val = self(el.first, cur, self);
      if (val != -1)
        return val;
    }
    return -1;
  };

  REP(i, N) {
    if (checked[i])
      continue;
    // 自己ループチェック
    bool self_loop = false;
    for (const auto &el : g[i]) {
      if (el.first == i) {
        chosen[el.second] = el.first + 1;
        checked[el.first] = true;
        self_loop = true;
        break;
      }
    }
    // ここを根にして適当に埋める(この連結成分は必ず埋まる)
    if (self_loop) {
      dfs(i, -1, dfs);
      continue;
    }

    // ループチェック
    if (auto idx = check(i, -1, check); idx != -1) {
      dfs(idx, -1, dfs);
      continue;
    }

    // どれにも当てはまらなければその連結成分は埋められないのでNO
    cout << NO << endl;
    return 0;
  }

  cout << YES << endl;
  REP(i, N) {
    // 上はありえないけど一応
    if (chosen[i] == 0) {
      cout << A[i] << endl;
    } else {
      cout << chosen[i] << endl;
    }
  }

  return 0;
}
0