結果

問題 No.1640 簡単な色塗り
ユーザー hayaten
提出日時 2021-08-06 23:02:49
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,138 bytes
コンパイル時間 2,675 ms
コンパイル使用メモリ 213,052 KB
最終ジャッジ日時 2025-01-23 16:09:19
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28 WA * 25
権限があれば一括ダウンロードができます

ソースコード

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<int>>;
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];


vector<int> bfs(int s, int n){
  vector<int> dist(n, 1e9);
  vector<int> pre(n, -1);
  dist[s] = 0;
  queue<int> que;
  que.push(s);
  while (!que.empty()){
    int v = que.front();
    que.pop();
    for(int u: G[v]){
      if(dist[u] != 1e9) continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      que.push(u);
    }
  }
  return dist;
}

int main() {
  int n;
  cin >> n;
  dsu d(n);
  vector<P> res;
  vector<int> cn(n);
  G.resize(n);
  rep(i, n){
    int a, b;
    cin >> a >> b;
    a--, b--;
    cn[a]++;
    cn[b]++;
    G[a].pb(b);
    G[b].pb(a);
    res.push_back({a, b});
    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);
  }
  int ans = 0;
  rep(i, n){
    if(d.leader(i) != i)continue;
    if(!non_tree[i]){
      cout << "No" << endl;
      return 0;
    }
  }
  int s = -1;
  int e = n;
  rep(i, n){
    if(chmin(e, cn[i])){
      s = i;
    }
  }
  vector<int> dist = bfs(s, n);
  cout << "Yes" << endl;
  set<int> st;
  vector<int> used(n, 0);
  rep(i, n){
    int a = res[i].first;
    int b = res[i].second;
    if(used[a] == 0 && used[b] == 0){
      if(dist[a] < dist[b]){
        cout << a + 1 << endl;
        used[a]++;
      }else{
        cout << b + 1 << endl;
        used[b]++;
      }
    }else if(used[b] == 0){
      cout << b + 1 << endl;
      used[b]++;
    }else{
      cout << a + 1 << endl;
      used[a]++;
    }
  }
}
0