結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1,203 ms
13,604 KB
testcase_05 AC 85 ms
16,916 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 66 ms
9,888 KB
testcase_11 AC 49 ms
8,332 KB
testcase_12 AC 43 ms
7,916 KB
testcase_13 AC 100 ms
13,516 KB
testcase_14 AC 102 ms
13,184 KB
testcase_15 AC 31 ms
7,028 KB
testcase_16 AC 38 ms
7,324 KB
testcase_17 AC 79 ms
11,712 KB
testcase_18 AC 8 ms
4,380 KB
testcase_19 AC 43 ms
7,796 KB
testcase_20 AC 57 ms
9,700 KB
testcase_21 AC 47 ms
8,252 KB
testcase_22 AC 6 ms
4,380 KB
testcase_23 AC 66 ms
10,148 KB
testcase_24 AC 19 ms
4,948 KB
testcase_25 AC 46 ms
7,940 KB
testcase_26 AC 71 ms
10,936 KB
testcase_27 AC 30 ms
6,584 KB
testcase_28 AC 96 ms
12,832 KB
testcase_29 AC 71 ms
10,724 KB
testcase_30 AC 12 ms
4,380 KB
testcase_31 AC 86 ms
9,864 KB
testcase_32 AC 70 ms
9,156 KB
testcase_33 AC 46 ms
7,168 KB
testcase_34 AC 58 ms
7,952 KB
testcase_35 AC 45 ms
7,480 KB
testcase_36 AC 12 ms
4,376 KB
testcase_37 AC 15 ms
4,624 KB
testcase_38 AC 72 ms
9,364 KB
testcase_39 AC 29 ms
5,680 KB
testcase_40 AC 29 ms
5,880 KB
testcase_41 AC 64 ms
8,308 KB
testcase_42 AC 35 ms
6,116 KB
testcase_43 AC 36 ms
6,192 KB
testcase_44 AC 35 ms
6,312 KB
testcase_45 AC 27 ms
5,540 KB
testcase_46 AC 13 ms
4,384 KB
testcase_47 AC 8 ms
4,380 KB
testcase_48 AC 70 ms
9,628 KB
testcase_49 AC 4 ms
4,380 KB
testcase_50 AC 1 ms
4,380 KB
testcase_51 AC 2 ms
4,384 KB
testcase_52 AC 113 ms
16,136 KB
testcase_53 AC 120 ms
15,568 KB
07_evil_01.txt AC 249 ms
25,964 KB
07_evil_02.txt AC 408 ms
37,056 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 << "\n";
  }
}
0