結果

問題 No.1640 簡単な色塗り
ユーザー 夜
提出日時 2021-08-07 01:56:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 282 ms / 2,000 ms
コード長 1,866 bytes
コンパイル時間 1,142 ms
コンパイル使用メモリ 109,992 KB
実行使用メモリ 16,520 KB
最終ジャッジ日時 2023-09-12 03:37:44
合計ジャッジ時間 14,973 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,596 KB
testcase_01 AC 3 ms
7,664 KB
testcase_02 AC 3 ms
7,552 KB
testcase_03 AC 4 ms
7,500 KB
testcase_04 AC 208 ms
11,460 KB
testcase_05 AC 228 ms
16,520 KB
testcase_06 AC 4 ms
7,540 KB
testcase_07 AC 4 ms
7,508 KB
testcase_08 AC 4 ms
7,536 KB
testcase_09 AC 4 ms
7,688 KB
testcase_10 AC 151 ms
11,884 KB
testcase_11 AC 124 ms
11,032 KB
testcase_12 AC 112 ms
11,540 KB
testcase_13 AC 246 ms
14,944 KB
testcase_14 AC 249 ms
15,776 KB
testcase_15 AC 76 ms
10,076 KB
testcase_16 AC 96 ms
11,052 KB
testcase_17 AC 199 ms
13,856 KB
testcase_18 AC 19 ms
8,304 KB
testcase_19 AC 105 ms
10,572 KB
testcase_20 AC 145 ms
11,260 KB
testcase_21 AC 119 ms
11,032 KB
testcase_22 AC 15 ms
8,244 KB
testcase_23 AC 163 ms
12,344 KB
testcase_24 AC 44 ms
8,900 KB
testcase_25 AC 111 ms
10,832 KB
testcase_26 AC 174 ms
12,200 KB
testcase_27 AC 74 ms
9,796 KB
testcase_28 AC 231 ms
15,236 KB
testcase_29 AC 176 ms
13,472 KB
testcase_30 AC 17 ms
8,472 KB
testcase_31 AC 141 ms
14,980 KB
testcase_32 AC 118 ms
14,164 KB
testcase_33 AC 73 ms
11,828 KB
testcase_34 AC 103 ms
13,644 KB
testcase_35 AC 72 ms
11,840 KB
testcase_36 AC 18 ms
8,696 KB
testcase_37 AC 26 ms
9,400 KB
testcase_38 AC 122 ms
13,484 KB
testcase_39 AC 42 ms
10,476 KB
testcase_40 AC 44 ms
10,096 KB
testcase_41 AC 90 ms
12,028 KB
testcase_42 AC 51 ms
10,168 KB
testcase_43 AC 56 ms
11,732 KB
testcase_44 AC 54 ms
11,008 KB
testcase_45 AC 44 ms
10,560 KB
testcase_46 AC 20 ms
9,012 KB
testcase_47 AC 14 ms
8,492 KB
testcase_48 AC 132 ms
15,192 KB
testcase_49 AC 8 ms
8,096 KB
testcase_50 AC 4 ms
7,708 KB
testcase_51 AC 4 ms
7,604 KB
testcase_52 AC 282 ms
16,136 KB
testcase_53 AC 279 ms
16,188 KB
07_evil_01.txt AC 636 ms
25,412 KB
07_evil_02.txt RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
vector<vector<pair<int,int>>> vec(200020);
bool bo[200020] = { false }, bo1[200020] = { false };
bool bo2[200020] = { false };
int ans[200020];
int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		int a, b;
		cin >> a >> b;
		vec[a].emplace_back(make_pair(b,i));
		vec[b].emplace_back(make_pair(a,i));
	}
	for (int i = 1; i <= n; i++) {
		if (!bo[i]) {
			queue<pair<int, int>> que;
			queue<int> que1;
			que.push(make_pair(i, -1));
			bo[i] = true;
			set<int> st;
			int co = 0, a = -1, b = -1, c = -1;
			while (!que.empty()) {
				pair<int, int> p = que.front();
				int now = que.front().first;
				que.pop();
				st.insert(now);
				for (int j = 0; j < vec[now].size(); j++) {
					if (!bo[vec[now][j].first]) {
						co++;
						bo[vec[now][j].first] = true;
						bo2[vec[now][j].second] = true;
						que.push(make_pair(vec[now][j].first, now));
					}
					if (!bo2[vec[now][j].second]) {
						co++;
						a = now;
						b = vec[now][j].first;
						c = vec[now][j].second;
					}
				}
			}
			if (st.size() == co + 1) {
				cout << "No" << endl;
				return 0;
			}
			ans[c] = a;
			que1.push(a);
			bo1[a] = true;
			while (!que1.empty()) {
				int now = que1.front();
				que1.pop();
				for (int j = 0; j < vec[now].size(); j++) {
					if (vec[now][j].second != c) {
						if (!bo1[vec[now][j].first]) {
							bo1[vec[now][j].first] = true;
							que1.push(vec[now][j].first);
							ans[vec[now][j].second] = vec[now][j].first;
						}
					}
				}
			}
		}
	}
	cout << "Yes" << endl;
	for (int i = 0; i < n; i++) {
		cout << ans[i] << endl;
	}
}
0