結果

問題 No.1640 簡単な色塗り
ユーザー hayatenhayaten
提出日時 2021-08-10 12:10:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,471 bytes
コンパイル時間 3,000 ms
コンパイル使用メモリ 222,368 KB
実行使用メモリ 16,956 KB
最終ジャッジ日時 2023-09-12 04:07:04
合計ジャッジ時間 15,214 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 210 ms
16,956 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 WA -
testcase_09 AC 2 ms
4,380 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 226 ms
13,128 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 180 ms
11,616 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 110 ms
8,300 KB
testcase_22 AC 12 ms
4,380 KB
testcase_23 AC 147 ms
9,872 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 163 ms
10,704 KB
testcase_27 AC 69 ms
6,608 KB
testcase_28 AC 208 ms
12,620 KB
testcase_29 AC 163 ms
10,468 KB
testcase_30 AC 12 ms
4,380 KB
testcase_31 AC 91 ms
9,776 KB
testcase_32 AC 80 ms
8,920 KB
testcase_33 AC 50 ms
7,136 KB
testcase_34 AC 64 ms
7,872 KB
testcase_35 AC 49 ms
7,252 KB
testcase_36 AC 12 ms
4,384 KB
testcase_37 AC 16 ms
4,396 KB
testcase_38 AC 82 ms
9,320 KB
testcase_39 AC 31 ms
5,660 KB
testcase_40 AC 32 ms
6,152 KB
testcase_41 AC 68 ms
8,132 KB
testcase_42 AC 39 ms
6,252 KB
testcase_43 AC 39 ms
6,164 KB
testcase_44 AC 40 ms
6,192 KB
testcase_45 AC 30 ms
5,328 KB
testcase_46 AC 13 ms
4,380 KB
testcase_47 AC 9 ms
4,384 KB
testcase_48 AC 82 ms
9,568 KB
testcase_49 AC 5 ms
4,384 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 1 ms
4,384 KB
testcase_52 AC 278 ms
16,088 KB
testcase_53 AC 265 ms
15,520 KB
07_evil_01.txt AC 557 ms
25,984 KB
07_evil_02.txt AC 845 ms
36,808 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;
      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