結果

問題 No.1640 簡単な色塗り
ユーザー simansiman
提出日時 2023-02-08 18:56:35
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,384 bytes
コンパイル時間 15,923 ms
コンパイル使用メモリ 108,664 KB
実行使用メモリ 12,112 KB
最終ジャッジ日時 2023-09-20 09:50:15
合計ジャッジ時間 24,894 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 RE -
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 757 ms
11,968 KB
testcase_05 AC 203 ms
11,928 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 RE -
testcase_10 AC 142 ms
8,788 KB
testcase_11 AC 115 ms
7,820 KB
testcase_12 AC 102 ms
7,280 KB
testcase_13 AC 226 ms
11,864 KB
testcase_14 AC 229 ms
11,816 KB
testcase_15 AC 72 ms
6,100 KB
testcase_16 AC 89 ms
6,872 KB
testcase_17 AC 187 ms
10,408 KB
testcase_18 AC 16 ms
4,380 KB
testcase_19 AC 99 ms
7,316 KB
testcase_20 AC 139 ms
8,716 KB
testcase_21 AC 111 ms
7,760 KB
testcase_22 AC 11 ms
4,380 KB
testcase_23 AC 149 ms
9,148 KB
testcase_24 AC 40 ms
4,976 KB
testcase_25 AC 103 ms
7,292 KB
testcase_26 AC 169 ms
9,616 KB
testcase_27 AC 69 ms
6,180 KB
testcase_28 AC 217 ms
11,400 KB
testcase_29 AC 164 ms
9,696 KB
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
testcase_50 AC 2 ms
4,996 KB
testcase_51 AC 1 ms
4,936 KB
testcase_52 AC 271 ms
11,888 KB
testcase_53 AC 268 ms
11,880 KB
07_evil_01.txt AC 579 ms
22,688 KB
07_evil_02.txt AC 918 ms
33,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <stack>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const int MAX_N = 200000;
vector<int> parent;
vector<int> _rank;
vector<int> _size;

class UnionFind {
public:
  UnionFind(int n) {
    for (int i = 0; i < n; ++i) {
      parent.push_back(i);
      _rank.push_back(0);
      _size.push_back(1);
    }
  }

  int find(int x) {
    if (parent[x] == x) {
      return x;
    } else {
      return parent[x] = find(parent[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;

    if (_rank[x] < _rank[y]) {
      parent[x] = y;
      _size[y] += _size[x];
    } else {
      parent[y] = x;
      _size[x] += _size[y];
      if (_rank[x] == _rank[y]) ++_rank[x];
    }
  }

  bool same(int x, int y) {
    return find(x) == find(y);
  }

  int size(int x) {
    return _size[find(x)];
  }
};

struct Edge {
  int id;
  int from;
  int to;

  Edge(int id = -1, int from = -1, int to = -1) {
    this->id = id;
    this->from = from;
    this->to = to;
  }
};

int main() {
  int N;

  cin >> N;
  UnionFind uf(N + 1);

  vector<Edge> E[N + 1];
  vector<Edge> edges;
  int a, b;
  for (int i = 0; i < N; ++i) {
    cin >> a >> b;
    uf.unite(a, b);

    E[a].push_back(Edge(i, a, b));
    E[b].push_back(Edge(i, b, a));
    edges.push_back(Edge(i, a, b));
  }

  int counter[N + 1];
  memset(counter, 0, sizeof(counter));

  for (Edge &e : edges) {
    int x = uf.find(e.from);

    counter[x]++;
  }

  typedef pair<int, int> P;
  int ans[N];
  memset(ans, -1, sizeof(ans));
  bool checked[N + 1];
  memset(checked, false, sizeof(checked));
  for (int v = 1; v <= N; ++v) {
    if (checked[v]) continue;

    int e_cnt = counter[uf.find(v)];
    if (e_cnt < uf.size(v)) {
      cout << "No" << endl;
      return 0;
    }

    bool visited[N + 1];
    memset(visited, false, sizeof(visited));
    stack<P> stk;
    stk.push(P(v, -1));
    int root = -1;
    int last_eid = -1;

    while (not stk.empty() && root == -1) {
      P p = stk.top();
      int u = p.first;
      int parent = p.second;
      stk.pop();

      visited[u] = true;

      for (Edge &e : E[u]) {
        if (e.id == parent) continue;
        if (visited[e.to] && root == -1) {
          root = e.to;
          last_eid = e.id;
        }

        stk.push(P(e.to, e.id));
      }
    }

    // fprintf(stderr, "v: %d, root: %d\n", v, root);
    stack<P> new_stk;
    memset(visited, false, sizeof(visited));
    new_stk.push(P(root, -1));
    while (not new_stk.empty()) {
      P p = new_stk.top();
      int u = p.first;
      int parent = p.second;
      new_stk.pop();

      if (visited[u]) continue;
      visited[u] = true;
      checked[u] = true;
      // fprintf(stderr, "  check: %d, es: %d\n", u, (int) E[u].size());

      for (Edge &e : E[u]) {
        if (e.id == parent) continue;
        if (e.id == last_eid) continue;

        // fprintf(stderr, "    id: %d, v: %d, ans: %d\n", e.id, e.to, ans[e.id]);
        assert(ans[e.id] == -1);
        ans[e.id] = e.to;
        new_stk.push(P(e.to, e.id));
      }
    }

    ans[last_eid] = root;
  }

  cout << "Yes" << endl;
  for (int i = 0; i < N; ++i) {
    cout << ans[i] << endl;
  }

  return 0;
}

0