結果

問題 No.1640 簡単な色塗り
ユーザー kwm_tkwm_t
提出日時 2021-08-07 16:56:10
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 2,036 bytes
コンパイル時間 4,172 ms
コンパイル使用メモリ 235,356 KB
実行使用メモリ 15,872 KB
最終ジャッジ日時 2024-06-29 16:52:45
合計ジャッジ時間 13,353 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 51 RE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#include "atcoder/all"
using namespace std;
using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
//const long long LINF = 1e18;
//const bool debug = false;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
vector<P>to[100005];
int ans[100005];
bool use[100005];
void dfs(int v, int p = -1) {
	use[v] = true;
	for (auto e : to[v]) {
		int nx = e.first;
		int id = e.second;
		if (use[nx])continue;
		ans[id] = nx;
		dfs(nx, v);
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n; cin >> n;
	vector<P>roop(n, { -1,-1 });
	dsu uf(n);
	vector<int>a(n), b(n);
	rep(i, n) {
		int a, b; cin >> a >> b; a--; b--;
		to[a].push_back({ b,i });
		to[b].push_back({ a,i });
		if (uf.same(a, b)) {
			if (-1 != roop[uf.leader(a)].first) {
				cout << "No" << endl;
				return 0;
			}
			ans[i] = min(a, b);
			roop[uf.leader(a)] = { i ,ans[i] };
		}
		else {
			int al = uf.leader(a);
			int bl = uf.leader(b);
			if (al != bl) {
				if ((-1 != roop[al].first) && (-1 != roop[bl].first)) {
					cout << "No" << endl;
					return 0;
				}
			}
			uf.merge(a, b);
			int nl = uf.leader(a);
			if (-1 != roop[al].first)roop[nl] = roop[al];
			if (-1 != roop[bl].first)roop[nl] = roop[bl];
		}
	}
	cout << "Yes" << endl;
	rep(i, n) {
		if (i != uf.leader(i))continue;
		int st = roop[uf.leader(i)].second;
		dfs(st);
	}
	rep(i, n) {
		cout << ans[i] + 1 << endl;
	}
	return 0;
}
0