結果

問題 No.1640 簡単な色塗り
ユーザー Example0911Example0911
提出日時 2021-08-06 22:05:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,542 bytes
コンパイル時間 2,364 ms
コンパイル使用メモリ 210,688 KB
実行使用メモリ 13,652 KB
最終ジャッジ日時 2023-09-12 02:04:27
合計ジャッジ時間 15,442 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 199 ms
13,356 KB
testcase_05 AC 201 ms
13,336 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 1 ms
4,380 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 5 ms
4,380 KB
testcase_31 AC 31 ms
6,688 KB
testcase_32 AC 27 ms
6,540 KB
testcase_33 AC 18 ms
5,364 KB
testcase_34 AC 23 ms
5,884 KB
testcase_35 AC 18 ms
5,492 KB
testcase_36 AC 5 ms
4,380 KB
testcase_37 AC 6 ms
4,380 KB
testcase_38 AC 30 ms
6,540 KB
testcase_39 AC 12 ms
4,588 KB
testcase_40 AC 12 ms
4,584 KB
testcase_41 AC 23 ms
5,896 KB
testcase_42 AC 14 ms
5,080 KB
testcase_43 AC 14 ms
4,948 KB
testcase_44 AC 14 ms
4,832 KB
testcase_45 AC 11 ms
4,416 KB
testcase_46 AC 6 ms
4,380 KB
testcase_47 AC 4 ms
4,376 KB
testcase_48 AC 30 ms
6,536 KB
testcase_49 AC 3 ms
4,380 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 2 ms
4,380 KB
testcase_52 WA -
testcase_53 WA -
07_evil_01.txt WA -
07_evil_02.txt WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

#define int long long

using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = (1LL << 61);
ll mod = 1000000007;
struct UnionFind {
	vector<int>par, vsize, esize;
	UnionFind(int sz_) : par(sz_), vsize(sz_, 1), esize(sz_, 0) {
		for (int i = 0; i < sz_; i++)par[i] = i;
	}
	void init(int sz_) {
		par.resize(sz_);
		vsize.resize(sz_, 1);
		esize.resize(sz_, 0);
		for (int i = 0; i < sz_; i++)par[i] = i;
	}
	int root(int x) {
		if (par[x] == x)return x;
		else return par[x] = root(par[x]);
	}
	bool merge(int x, int y) {
		x = root(x), y = root(y);
		if (x == y) {
			esize[x]++;
			return false;
		}
		if (vsize[x] < vsize[y])swap(x, y);
		vsize[x] += vsize[y];
		esize[x] += esize[y] + 1;
		par[y] = x;
		return true;
	}
	bool issame(int x, int y) {
		return root(x) == root(y);
	}
	int vs(int x) {
		return vsize[root(x)];
	}
	int es(int x) {
		return esize[root(x)];
	}
};

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int N; cin >> N;
	UnionFind uf(N);
	vector<int>A(N), B(N);
	for (int i = 0; i < N; i++) {
		int a, b; cin >> a >> b; a--; b--;
		uf.merge(a, b);
		A[i] = a + 1; B[i] = b + 1;
	}
	int cnt = 0;
	for (int i = 0; i < N; i++) {
		if (uf.root(i) == i)cnt += min(uf.vs(i), uf.es(i));
	}
	if (cnt != N) {
		cout << "No" << endl; return 0;
	}
	cout << "Yes" << endl;
	map<int, bool>mp;
	for (int i = 0; i < N; i++) {
		if (mp[A[i]]) {
			cout << B[i] << endl;
			mp[B[i]] = true;
		}
		else {
			cout << A[i] << endl;
			mp[A[i]] = true;
		}
	}
	return 0;

}

0