結果

問題 No.1640 簡単な色塗り
ユーザー hayatenhayaten
提出日時 2021-08-10 12:12:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,505 ms / 2,000 ms
コード長 2,507 bytes
コンパイル時間 2,954 ms
コンパイル使用メモリ 223,208 KB
実行使用メモリ 16,888 KB
最終ジャッジ日時 2023-09-12 04:05:06
合計ジャッジ時間 18,302 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1,505 ms
13,268 KB
testcase_05 AC 209 ms
16,888 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 142 ms
9,768 KB
testcase_11 AC 115 ms
8,352 KB
testcase_12 AC 99 ms
7,940 KB
testcase_13 AC 225 ms
13,196 KB
testcase_14 AC 225 ms
13,200 KB
testcase_15 AC 73 ms
6,572 KB
testcase_16 AC 89 ms
7,332 KB
testcase_17 AC 181 ms
11,588 KB
testcase_18 AC 16 ms
4,384 KB
testcase_19 AC 100 ms
7,852 KB
testcase_20 AC 136 ms
9,584 KB
testcase_21 AC 109 ms
8,156 KB
testcase_22 AC 12 ms
4,380 KB
testcase_23 AC 148 ms
9,924 KB
testcase_24 AC 40 ms
4,844 KB
testcase_25 AC 101 ms
8,104 KB
testcase_26 AC 167 ms
10,520 KB
testcase_27 AC 69 ms
6,432 KB
testcase_28 AC 212 ms
12,928 KB
testcase_29 AC 162 ms
10,588 KB
testcase_30 AC 12 ms
4,380 KB
testcase_31 AC 91 ms
9,748 KB
testcase_32 AC 80 ms
9,172 KB
testcase_33 AC 51 ms
7,232 KB
testcase_34 AC 66 ms
7,872 KB
testcase_35 AC 51 ms
7,172 KB
testcase_36 AC 12 ms
4,380 KB
testcase_37 AC 17 ms
4,416 KB
testcase_38 AC 80 ms
9,320 KB
testcase_39 AC 33 ms
5,728 KB
testcase_40 AC 33 ms
5,968 KB
testcase_41 AC 69 ms
8,252 KB
testcase_42 AC 38 ms
6,200 KB
testcase_43 AC 40 ms
6,172 KB
testcase_44 AC 39 ms
6,220 KB
testcase_45 AC 29 ms
5,416 KB
testcase_46 AC 13 ms
4,376 KB
testcase_47 AC 9 ms
4,380 KB
testcase_48 AC 85 ms
9,512 KB
testcase_49 AC 4 ms
4,380 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 2 ms
4,380 KB
testcase_52 AC 273 ms
16,088 KB
testcase_53 AC 272 ms
15,748 KB
07_evil_01.txt AC 547 ms
25,760 KB
07_evil_02.txt AC 860 ms
36,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/dsu>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define ALL(v) v.begin(), v.end()
#define pb push_back
using namespace std;
using namespace atcoder;

using P = pair<int, int>;
using Graph = vector<vector<P>>;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
Graph G;
bool non_tree[400000];


int main() {
  int n;
  cin >> n;
  dsu d(n);
  vector<P> self_loop(n);
  vector<int> edge_enable(n);
  G.resize(n);
  rep(i, n){
    int a, b;
    cin >> a >> b;
    a--, b--;
    if(a == b){
      self_loop[a].first = 1;
      self_loop[a].second = i;
      non_tree[d.leader(a)] = true;
      edge_enable[i] = 1;
      continue;
    }
    G[a].pb({b, i});
    G[b].pb({a, i});
    if(d.same(a, b)){
      non_tree[d.leader(a)] = true;
    }
    if(non_tree[d.leader(a)] | non_tree[d.leader(b)]){
      d.merge(a, b);
      non_tree[d.leader(a)] = true;
    }
    d.merge(a, b);
  }
  rep(i, n){
    if(d.leader(i) != i)continue;
    if(!non_tree[i]){
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  vector<vector<int>> lis = d.groups();
  vector<int> used(n);
  vector<int> ans(n, -1);
  auto root_v = [&](auto &&self, int u, int p, int e, int &start) -> void{
    if(used[u] == 1){
      start = u;
      edge_enable[e] = 1;
      ans[e] = u;
      return;
    }
    used[u] = 1;
    for(auto [v, b] : G[u]){
      if(v ==p)continue;
      self(self, v, u, b, start);
    }
    return;
  };
  auto dfs = [&](auto &&self, int u, int p, int e, vector<int>& res) -> void{
    used[u] = 1;
    if(e != -1 && res[e] == -1){
      res[e] = u;
    }
    for(auto [v, b] : G[u]){
      if(v ==p)continue;
      if(used[v])continue;
      if(edge_enable[b] == 1)continue;
      self(self, v, u, b, res);
    }
    return;
  };
  for(auto s : lis){
    int root = -1;
    for (auto d : s){
      if(self_loop[d].first == 1){
        root = d;
        ans[self_loop[d].second] = d;
      }
    }
    int root_tmp = -1;
    for (auto d : s){
      root_tmp = d;
      break;
    }
    if(root == -1){
      root_v(root_v, root_tmp, -1, -1, root);
    }
    used.assign(n, 0);
    dfs(dfs, root, -1, -1, ans);
  }
  rep(i, n){
    cout << ans[i] + 1 << endl;
  }
}
0