結果

問題 No.1640 簡単な色塗り
ユーザー harurunharurun
提出日時 2021-06-12 11:01:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,733 bytes
コンパイル時間 1,288 ms
コンパイル使用メモリ 103,168 KB
実行使用メモリ 17,360 KB
最終ジャッジ日時 2023-09-12 01:24:07
合計ジャッジ時間 10,458 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 92 ms
15,996 KB
testcase_05 AC 88 ms
17,148 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 14 ms
5,240 KB
testcase_31 AC 104 ms
16,536 KB
testcase_32 AC 92 ms
15,572 KB
testcase_33 AC 60 ms
10,996 KB
testcase_34 AC 63 ms
11,900 KB
testcase_35 AC 50 ms
10,436 KB
testcase_36 AC 13 ms
5,064 KB
testcase_37 AC 17 ms
5,708 KB
testcase_38 AC 77 ms
13,936 KB
testcase_39 AC 37 ms
8,156 KB
testcase_40 AC 35 ms
7,952 KB
testcase_41 AC 71 ms
12,704 KB
testcase_42 AC 43 ms
8,732 KB
testcase_43 AC 39 ms
8,616 KB
testcase_44 AC 44 ms
9,048 KB
testcase_45 AC 29 ms
7,292 KB
testcase_46 AC 15 ms
5,328 KB
testcase_47 AC 11 ms
4,632 KB
testcase_48 AC 85 ms
14,104 KB
testcase_49 AC 5 ms
4,376 KB
testcase_50 AC 1 ms
4,376 KB
testcase_51 AC 1 ms
4,376 KB
testcase_52 AC 135 ms
17,360 KB
testcase_53 WA -
07_evil_01.txt WA -
07_evil_02.txt WA -
権限があれば一括ダウンロードができます

ソースコード

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