結果

問題 No.1640 簡単な色塗り
ユーザー kwm_tkwm_t
提出日時 2021-08-07 16:56:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 2,036 bytes
コンパイル時間 4,061 ms
コンパイル使用メモリ 233,068 KB
実行使用メモリ 14,140 KB
最終ジャッジ日時 2023-09-12 03:52:19
合計ジャッジ時間 12,662 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,772 KB
testcase_01 AC 3 ms
5,764 KB
testcase_02 AC 3 ms
5,772 KB
testcase_03 AC 4 ms
5,768 KB
testcase_04 AC 39 ms
11,084 KB
testcase_05 AC 44 ms
14,140 KB
testcase_06 AC 3 ms
5,764 KB
testcase_07 AC 3 ms
5,776 KB
testcase_08 AC 3 ms
5,832 KB
testcase_09 AC 3 ms
5,936 KB
testcase_10 AC 37 ms
9,176 KB
testcase_11 AC 28 ms
8,348 KB
testcase_12 AC 26 ms
8,064 KB
testcase_13 AC 54 ms
10,996 KB
testcase_14 AC 51 ms
10,936 KB
testcase_15 AC 19 ms
7,540 KB
testcase_16 AC 24 ms
7,800 KB
testcase_17 AC 42 ms
9,948 KB
testcase_18 AC 7 ms
6,384 KB
testcase_19 AC 25 ms
8,116 KB
testcase_20 AC 34 ms
8,932 KB
testcase_21 AC 27 ms
8,380 KB
testcase_22 AC 6 ms
6,264 KB
testcase_23 AC 37 ms
9,168 KB
testcase_24 AC 12 ms
6,812 KB
testcase_25 AC 26 ms
8,080 KB
testcase_26 AC 41 ms
9,744 KB
testcase_27 AC 18 ms
7,396 KB
testcase_28 AC 49 ms
10,788 KB
testcase_29 AC 39 ms
9,624 KB
testcase_30 AC 8 ms
6,592 KB
testcase_31 AC 41 ms
10,472 KB
testcase_32 AC 35 ms
9,616 KB
testcase_33 AC 25 ms
8,424 KB
testcase_34 AC 30 ms
9,404 KB
testcase_35 AC 25 ms
8,420 KB
testcase_36 AC 9 ms
6,520 KB
testcase_37 AC 11 ms
6,716 KB
testcase_38 AC 39 ms
10,012 KB
testcase_39 AC 18 ms
7,432 KB
testcase_40 AC 18 ms
7,744 KB
testcase_41 AC 32 ms
9,104 KB
testcase_42 AC 20 ms
7,776 KB
testcase_43 AC 20 ms
7,772 KB
testcase_44 AC 20 ms
7,844 KB
testcase_45 AC 15 ms
7,248 KB
testcase_46 AC 9 ms
6,556 KB
testcase_47 AC 6 ms
6,316 KB
testcase_48 AC 40 ms
9,896 KB
testcase_49 AC 4 ms
6,204 KB
testcase_50 AC 3 ms
5,864 KB
testcase_51 AC 3 ms
6,088 KB
testcase_52 AC 59 ms
12,540 KB
testcase_53 AC 58 ms
12,628 KB
07_evil_01.txt RE -
07_evil_02.txt RE -
権限があれば一括ダウンロードができます

ソースコード

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