結果
問題 | No.1640 簡単な色塗り |
ユーザー | harurun |
提出日時 | 2021-06-12 19:54:37 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 149 ms / 2,000 ms |
コード長 | 3,645 bytes |
コンパイル時間 | 1,477 ms |
コンパイル使用メモリ | 99,620 KB |
最終ジャッジ日時 | 2025-01-22 07:49:16 |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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); //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; }