結果

問題 No.1640 簡単な色塗り
ユーザー たたき@競プロ
提出日時 2023-08-30 07:55:20
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 1,135 bytes
コンパイル時間 6,731 ms
コンパイル使用メモリ 315,572 KB
実行使用メモリ 13,952 KB
最終ジャッジ日時 2024-12-31 22:34:12
合計ジャッジ時間 17,804 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 53
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using mint=modint998244353; //1000000007;
using ll=long long;
using pp=pair<int,int>;
#define sr string 
#define vc vector
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define pb push_back
#define all(v) v.begin(),v.end()
#define pque priority_queue
#define bpc(a) __builtin_popcount(a)
int main(){
  int n;cin>>n;
  vc<pp>v(n); rep(i,n)cin>>v[i].fi>>v[i].se; 
  rep(i,n)v[i].fi--,v[i].se--;
  vc vx(n,vc<pp>(0));
  dsu d(n);
  vc<bool>bo(n,false);
  rep(i,n){
    auto [a,b]=v[i];
    if(!d.same(a,b)){
      d.merge(a,b),bo[i]=true;
      vx[a].pb({b,i}); vx[b].pb({a,i});
    }
  }
  vc<int>ans(n,-1);
  auto f=[&](auto f,int a,int p)->void{
    for(auto [au,i]:vx[a])if(au!=p){
      ans[i]=au;
      f(f,au,a);
    }
    return;
  };
  vc<int>ok(n,0);
  rep(i,n)if(!bo[i]){
    auto [a,b]=v[i];
    if(ok[d.leader(a)])continue;
    ok[d.leader(a)]=1;
    ans[i]=a;
    f(f,a,-1);
  }
  
  rep(i,n)if(ans[i]==-1){
    cout<<"No";
    return 0;
  }
  cout<<"Yes"<<"\n";
  rep(i,n)cout<<ans[i]+1<<"\n";
}
  
0