結果

問題 No.1640 簡単な色塗り
ユーザー harurunharurun
提出日時 2021-06-12 19:54:37
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 122 ms / 2,000 ms
コード長 3,645 bytes
コンパイル時間 1,309 ms
コンパイル使用メモリ 102,260 KB
実行使用メモリ 17,004 KB
最終ジャッジ日時 2023-09-12 01:24:21
合計ジャッジ時間 12,320 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 89 ms
17,004 KB
testcase_05 AC 85 ms
15,536 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,376 KB
testcase_10 AC 63 ms
10,712 KB
testcase_11 AC 50 ms
9,524 KB
testcase_12 AC 44 ms
8,560 KB
testcase_13 AC 103 ms
14,648 KB
testcase_14 AC 104 ms
14,672 KB
testcase_15 AC 32 ms
6,968 KB
testcase_16 AC 40 ms
8,020 KB
testcase_17 AC 83 ms
12,828 KB
testcase_18 AC 7 ms
4,376 KB
testcase_19 AC 46 ms
8,544 KB
testcase_20 AC 62 ms
10,560 KB
testcase_21 AC 50 ms
9,096 KB
testcase_22 AC 6 ms
4,380 KB
testcase_23 AC 68 ms
11,276 KB
testcase_24 AC 19 ms
5,436 KB
testcase_25 AC 46 ms
8,768 KB
testcase_26 AC 78 ms
11,776 KB
testcase_27 AC 32 ms
6,964 KB
testcase_28 AC 98 ms
14,140 KB
testcase_29 AC 73 ms
11,712 KB
testcase_30 AC 14 ms
4,856 KB
testcase_31 AC 116 ms
15,728 KB
testcase_32 AC 92 ms
14,096 KB
testcase_33 AC 56 ms
10,840 KB
testcase_34 AC 64 ms
12,628 KB
testcase_35 AC 51 ms
10,724 KB
testcase_36 AC 13 ms
5,344 KB
testcase_37 AC 17 ms
5,692 KB
testcase_38 AC 80 ms
14,972 KB
testcase_39 AC 36 ms
8,072 KB
testcase_40 AC 36 ms
8,120 KB
testcase_41 AC 73 ms
12,772 KB
testcase_42 AC 43 ms
8,868 KB
testcase_43 AC 40 ms
9,084 KB
testcase_44 AC 45 ms
8,988 KB
testcase_45 AC 30 ms
7,656 KB
testcase_46 AC 14 ms
5,268 KB
testcase_47 AC 10 ms
4,444 KB
testcase_48 AC 82 ms
15,004 KB
testcase_49 AC 5 ms
4,380 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 1 ms
4,376 KB
testcase_52 AC 120 ms
15,932 KB
testcase_53 AC 122 ms
15,944 KB
07_evil_01.txt AC 277 ms
29,380 KB
07_evil_02.txt AC 446 ms
42,248 KB
権限があれば一括ダウンロードができます

ソースコード

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>
#define Pl pair<pair<ll,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});
    }
  }
  vector<ll> seen(N);
  //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;
        }
      }
      */
      deque<P>que;
      que.push_back({linked.at(0),-1});
      //cerr<<"que\n";
      while(!que.empty()){
        P q=que.back();
        que.pop_back();
        //cerr<<"q:"<<q<<endl;
        for(P j:to.at(q.first)){
          //cerr<<"j:"<<j<<endl;
          if(j.second==q.second)continue;
          if(seen.at(j.first)){
            root=j.first;
            next_point=q.first;
            ans.at(j.second)=root+1;
            que.clear();
            break;
          }
          que.push_back({j.first,j.second});
        }
        seen.at(q.first)=1;
      }
      //cerr<<seen;
      //cerr<<"que end\n";
      if(next_point==-1){
        printf("No\n");
        cerr<<"2\n";
        return 0;
      }
      //cerr<<next_point<<"\n";
    }
    //cerr<<"root:"<<root<<"\n";
    //cerr<<"dq\n";
    deque<ll>dq;
    dq.push_back(root);
    while(!dq.empty()){
      ll q=dq.back();dq.pop_back();
      //cerr<<"q:"<<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);
          //cerr<<"push:"<<j.first<<endl;
        }
      }
    }
  }
  for(ll i:ans){
    if(i==-1){
      printf("No\n");
      cerr<<"3\n";
      return 0;
    }
  }
  cout<<"Yes\n";
  cout<<ans;
  return 0;
}
0