結果
| 問題 |
No.1640 簡単な色塗り
|
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2021-08-06 22:34:47 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 74 ms / 2,000 ms |
| コード長 | 1,606 bytes |
| コンパイル時間 | 1,109 ms |
| コンパイル使用メモリ | 84,792 KB |
| 最終ジャッジ日時 | 2025-01-23 15:56:17 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 53 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <atcoder/dsu>
using namespace std;
using i32 = int32_t;
using i64 = int64_t;
#define rep(i,n) for(int i=0; i<(n); i++)
int N;
vector<vector<pair<int,int>>> E;
vector<int> used;
int main(){
cin >> N;
E.resize(N);
vector<int> I(N,0);
rep(i,N){
int a,b; cin >> a >> b; a--; b--;
E[a].push_back({b,i});
E[b].push_back({a,i});
I[a]++;
I[b]++;
}
used.assign(N,-1);
vector<int> q;
rep(i,N) if(I[i] == 1) q.push_back(i);
rep(i,q.size()){
int p = q[i];
for(auto e : E[p]) if(used[e.second] == -1){
used[e.second] = p;
I[e.first]--;
if(I[e.first] == 1){
q.push_back(e.first);
break;
}
}
I[p]--;
}
rep(s,N) if(I[s] >= 2){
int to = -1;
I[s]--;
for(auto e : E[s]) if(used[e.second] == -1){
used[e.second] = s;
to = e.first;
I[e.first]--;
break;
}
if(to == -1){ cout << "No\n"; return 0; }
while(to != -1){
int p = to;
I[p]--;
to = -1;
for(auto e : E[p]) if(used[e.second] == -1){
used[e.second] = p;
to = e.first;
I[e.first]--;
break;
}
}
}
rep(i,N) if(used[i] == -1){ cout << "No\n"; return 0; }
vector<int> filled(N,0);
rep(i,N) filled[used[i]] = 1;
rep(i,N) if(filled[i] == 0){ cout << "No\n"; return 0; }
cout << "Yes\n";
rep(i,N) cout << (used[i]+1) << "\n";
return 0;
}
struct ios_do_not_sync{
ios_do_not_sync(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
} ios_do_not_sync_instance;
Nachia