結果
| 問題 |
No.1640 簡単な色塗り
|
| コンテスト | |
| ユーザー |
beet
|
| 提出日時 | 2021-08-06 21:37:16 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 54 ms / 2,000 ms |
| コード長 | 2,149 bytes |
| コンパイル時間 | 2,688 ms |
| コンパイル使用メモリ | 255,500 KB |
| 実行使用メモリ | 18,304 KB |
| 最終ジャッジ日時 | 2024-06-29 14:52:29 |
| 合計ジャッジ時間 | 11,515 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 53 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
const char newl = '\n';
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=int>
vector<T> read(size_t n){
vector<T> ts(n);
for(size_t i=0;i<n;i++) cin>>ts[i];
return ts;
}
struct UnionFind{
int num;
vector<int> rs,ps;
UnionFind(int n):num(n),rs(n,1),ps(n,0){
iota(ps.begin(),ps.end(),0);
}
int find(int x){
return (x==ps[x]?x:ps[x]=find(ps[x]));
}
bool same(int x,int y){
return find(x)==find(y);
}
void unite(int x,int y){
x=find(x);y=find(y);
if(x==y) return;
if(rs[x]<rs[y]) swap(x,y);
rs[x]+=rs[y];
ps[y]=x;
num--;
}
int size(int x){
return rs[find(x)];
}
int count() const{
return num;
}
};
template<typename F>
struct FixPoint : F{
FixPoint(F&& f):F(forward<F>(f)){}
template<typename... Args>
decltype(auto) operator()(Args&&... args) const{
return F::operator()(*this,forward<Args>(args)...);
}
};
template<typename F>
inline decltype(auto) MFP(F&& f){
return FixPoint<F>{forward<F>(f)};
}
//INSERT ABOVE HERE
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
int n;
cin>>n;
vector<int> as(n),bs(n);
for(int i=0;i<n;i++){
cin>>as[i]>>bs[i];
as[i]--;
bs[i]--;
}
UnionFind uf(n);
vector<int> rs,ans(n,-1);
vector<vector<int>> G(n);
for(int i=0;i<n;i++){
if(uf.same(as[i],bs[i])){
rs.emplace_back(as[i]);
ans[i]=as[i];
}else{
uf.unite(as[i],bs[i]);
G[as[i]].emplace_back(i);
G[bs[i]].emplace_back(i);
}
}
vector<int> used(n);
for(int r:rs){
if(used[r]) drop("No");
MFP([&](auto dfs,int v)->void{
if(used[v]) return;
used[v]=1;
for(int i:G[v]){
int u=as[i]^bs[i]^v;
if(used[u]) continue;
ans[i]=u;
dfs(u);
// cout<<i<<":"<<v<<' '<<u<<endl;
}
})(r);
}
cout<<"Yes"<<newl;
for(int a:ans) cout<<a+1<<newl;
return 0;
}
beet