結果

問題 No.1640 簡単な色塗り
ユーザー harurunharurun
提出日時 2021-06-12 11:01:13
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,733 bytes
コンパイル時間 1,463 ms
コンパイル使用メモリ 100,748 KB
最終ジャッジ日時 2025-01-22 07:46:39
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30 WA * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<atcoder/dsu>
#include<iostream>
#include<stdio.h>
#include<vector>
#include<deque>
#include<algorithm>
#include<utility>
using namespace atcoder;
using namespace std;
#define ll long long
#define P pair<ll,ll>

ostream& operator<<(ostream& os,const P& a){
  return os<<a.first<<" "<<a.second;
}

ostream& operator<<(ostream& os,const vector<ll>& a){
  for(ll i:a){
    os<<i<<"\n";
  }
  return os;
}

ostream& operator<<(ostream& os,const vector<vector<int>>& a){
  for(vector<int> i:a){
    for(int j:i){
      os<<j<<" ";
    }
    os<<"\n";
  }
  return os;
}

ostream& operator<<(ostream& os,const vector<P>& a){
  for(P i:a){
    os<<i<<"\n";
  }
  return os;
}

signed main(){
  ll N,A,B;
  cin>>N;
  dsu uf(N);
  vector<vector<P>>to(N);
  for(int i=0;i<N;i++){
    cin>>A>>B;
    uf.merge(A-1,B-1);
    to.at(A-1).push_back({B-1,i});
    if(A!=B){
      to.at(B-1).push_back({A-1,i});
    }
  }
  //cerr<<"to \n";
  vector<ll>ans(N,-1);
  vector<vector<int>> groups=uf.groups();
  //cerr<<groups;
  for(vector<int> linked:groups){
    bool is_cycle=true;
    ll root=-1,cycle_cnt=0;
    vector<P>degree;
    /*
    if(linked.size()==1){
      printf("No\n");
      cerr<<"0\n";
      return 0;
    }
    */
    for(int point:linked){
      ll link_size=to.at(point).size();
      //cerr<<link_size<<endl;
      for(ll k=0;k<link_size;k++){
        //自己ループを検出する
        if(to.at(point).at(k).first==point){
          cycle_cnt++;
          is_cycle=false;
          ans.at(to.at(point).at(k).second)=point+1;
          root=point;
        }
      }
      degree.push_back({link_size,point});
    }
    if(cycle_cnt>1){
      printf("No\n");
      //cerr<<"1\n";
      return 0;
    }
    //サイクルならば、次数が2以上の場所を探す
    //次数でソートする
    //cerr<<degree;
    if(is_cycle){
      sort(degree.rbegin(),degree.rend());
      root=degree.at(0).second;
      //cerr<<root<<endl;
      ll next_point=-1;
      for(P next:to.at(root)){
        if(to.at(next.first).size()>=2){
          next_point=next.first;
          ans.at(next.second)=root+1;
          break;
        }
      }
      if(next_point==-1){
        printf("No\n");
        //cerr<<"2\n";
        return 0;
      }
    }
    //cerr<<root<<"\n";
    //cerr<<"dq\n";
    deque<ll>dq;
    dq.push_back(root);
    while(!dq.empty()){
      ll q=dq.back();dq.pop_back();
      //cerr<<q<<"\n";
      for(P j:to.at(q)){
        if(ans.at(j.second)==-1){
          ans.at(j.second)=j.first+1;
          dq.push_back(j.first);
        }
      }
    }
  }
  for(ll i:ans){
    if(i==-1){
      printf("No\n");
      //cerr<<"3\n";
      return 0;
    }
  }
  cout<<"Yes\n";
  cout<<ans;
  return 0;
}
0