結果

問題 No.1640 簡単な色塗り
ユーザー Example0911Example0911
提出日時 2021-08-06 22:30:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,099 bytes
コンパイル時間 2,653 ms
コンパイル使用メモリ 217,144 KB
実行使用メモリ 25,792 KB
最終ジャッジ日時 2023-09-12 02:26:13
合計ジャッジ時間 23,314 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 372 ms
25,792 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 2 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,376 KB
testcase_31 AC 28 ms
7,152 KB
testcase_32 AC 25 ms
6,480 KB
testcase_33 AC 17 ms
5,464 KB
testcase_34 AC 22 ms
5,936 KB
testcase_35 AC 17 ms
5,472 KB
testcase_36 AC 5 ms
4,376 KB
testcase_37 AC 6 ms
4,384 KB
testcase_38 AC 26 ms
6,412 KB
testcase_39 AC 11 ms
4,568 KB
testcase_40 AC 11 ms
4,752 KB
testcase_41 AC 22 ms
6,136 KB
testcase_42 AC 13 ms
4,828 KB
testcase_43 AC 13 ms
4,944 KB
testcase_44 AC 13 ms
4,944 KB
testcase_45 AC 10 ms
4,424 KB
testcase_46 AC 5 ms
4,380 KB
testcase_47 AC 4 ms
4,380 KB
testcase_48 AC 27 ms
6,412 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
07_evil_01.txt WA -
07_evil_02.txt TLE -
権限があれば一括ダウンロードができます

ソースコード

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;
	map<int, int>c;
	map<int, int>nowc;
	for (int i = 0; i < N; i++) {
		c[A[i]]++;
		c[B[i]]++;
		nowc[A[i]]++;
		nowc[B[i]]++;
	}
	for (int i = 0; i < N; i++) {
		if (c[A[i]] == 1) {
			cout << A[i] << endl;
			mp[A[i]] = true;
			nowc[A[i]]--;
		}
		else if(c[B[i]] == 1){
			cout << B[i] << endl;
			mp[B[i]] = true;
			nowc[B[i]]--;
		}
		else if (nowc[A[i]] == 1) {
			cout << A[i] << endl;
			mp[A[i]] = true;
			nowc[A[i]]--;
		}
		else if (nowc[B[i]] == 1) {
			cout << B[i] << endl;
			mp[B[i]] = true;
			nowc[B[i]]--;
		}
		else {
			if (mp[A[i]]) {
				cout << B[i] << endl;
				mp[B[i]] = true;
				nowc[B[i]]--;
			}
			else {
				cout << A[i] << endl;
				mp[A[i]] = true;
				nowc[A[i]]--;
			}
		}
	}
	return 0;

}

0