結果
問題 |
No.1640 簡単な色塗り
|
ユーザー |
![]() |
提出日時 | 2021-06-14 18:07:45 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 150 ms / 2,000 ms |
コード長 | 2,526 bytes |
コンパイル時間 | 1,433 ms |
コンパイル使用メモリ | 100,076 KB |
最終ジャッジ日時 | 2025-01-22 08:13:15 |
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 53 |
ソースコード
#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); vector<ll>ans(N,-1); vector<vector<int>> groups=uf.groups(); for(vector<int> linked:groups){ bool is_cycle=true; ll root=-1,cycle_cnt=0; for(int point:linked){ ll link_size=to.at(point).size(); 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; } } } if(cycle_cnt>1){ printf("No\n"); cerr<<"1\n"; return 0; } if(is_cycle){ ll next_point=-1; deque<P>que; que.push_back({linked.at(0),-1}); while(!que.empty()){ P q=que.back(); que.pop_back(); for(P j:to.at(q.first)){ 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; } if(next_point==-1){ printf("No\n"); cerr<<"2\n"; return 0; } } deque<ll>dq; dq.push_back(root); while(!dq.empty()){ ll q=dq.back();dq.pop_back(); 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; }