結果
| 問題 |
No.1640 簡単な色塗り
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-08-06 22:49:03 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,458 bytes |
| コンパイル時間 | 2,147 ms |
| コンパイル使用メモリ | 184,800 KB |
| 実行使用メモリ | 11,264 KB |
| 最終ジャッジ日時 | 2024-06-29 15:54:15 |
| 合計ジャッジ時間 | 13,164 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 WA * 26 |
コンパイルメッセージ
main.cpp: In lambda function:
main.cpp:75:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
75 | for(auto [to, ind] : G[now]){
| ^
main.cpp:84:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
84 | for(auto [to, ind] : G[now]){
| ^
ソースコード
#include <bits/stdc++.h>
using namespace std;
struct UnionFind{
vector<int> par;
vector<int> rank;
vector<int> cnt;
UnionFind(int n){
par.resize(n);
rank.resize(n);
cnt.resize(n);
for(int i = 0; i < n; i++){
par[i] = i;
rank[i] = 1;
}
}
int root(int x){
if(par[x] == x) return x;
else return par[x] = root(par[x]);
}
bool same(int x, int y){
return root(x) == root(y);
}
void unite(int x, int y){
x = root(x);
y = root(y);
if(x == y) {
cnt[x] += 1;
return;
}
if(rank[x] < rank[y]) swap(x, y);
par[y] = x;
rank[x] += rank[y];
cnt[x] += cnt[y] + 1;
}
int size(int x) {
return rank[root(x)];
}
int count(int x){
return cnt[root(x)];
}
};
int main(){
int N;
cin >> N;
vector<int> A(N), B(N);
UnionFind U(N);
vector<vector<pair<int, int>>> G(N);
for(int i = 0; i < N; i++) {
cin >> A[i] >> B[i];
A[i]--;
B[i]--;
U.unite(A[i], B[i]);
G[A[i]].push_back({B[i], i});
G[B[i]].push_back({A[i], i});
}
vector<bool> used(N, false);
bool f = true;
for(int i = 0; i < N; i++){
int t = U.root(i);
if(used[t]) continue;
used[t] = true;
if(U.size(i) != U.count(i)) f = false;
}
if(!f) cout << "No" << endl;
else{
cout << "Yes" << endl;
vector<int> ans(N, -1);
vector<bool> use(N, false);
used.assign(N, false);
auto dfs = [&](auto dfs, int now) -> void{
for(auto [to, ind] : G[now]){
if(use[ind]) continue;
if(used[to]){
ans[ind] = now;
use[ind] = true;
used[now] = true;
break;
}
}
for(auto [to, ind] : G[now]){
if(use[ind]) continue;
if(used[to]) continue;
if(!used[now]){
ans[ind] = now;
used[now] = true;
use[ind] = true;
}
dfs(dfs, to);
break;
}
};
for(int i = 0; i < N; i++){
if(!used[i]) dfs(dfs, i);
}
for(int i = 0; i < N; i++) cout << ans[i] + 1 << endl;
}
}