結果

問題 No.1640 簡単な色塗り
ユーザー 夜
提出日時 2021-08-06 23:13:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,858 bytes
コンパイル時間 1,323 ms
コンパイル使用メモリ 109,752 KB
実行使用メモリ 16,364 KB
最終ジャッジ日時 2023-09-12 03:12:42
合計ジャッジ時間 14,843 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,688 KB
testcase_01 AC 4 ms
7,748 KB
testcase_02 AC 4 ms
7,608 KB
testcase_03 AC 4 ms
7,596 KB
testcase_04 AC 207 ms
11,576 KB
testcase_05 AC 224 ms
16,204 KB
testcase_06 AC 4 ms
7,592 KB
testcase_07 AC 4 ms
7,532 KB
testcase_08 AC 4 ms
7,748 KB
testcase_09 AC 4 ms
7,552 KB
testcase_10 AC 156 ms
11,884 KB
testcase_11 AC 125 ms
11,096 KB
testcase_12 AC 110 ms
11,544 KB
testcase_13 AC 250 ms
15,016 KB
testcase_14 AC 256 ms
15,484 KB
testcase_15 AC 79 ms
9,956 KB
testcase_16 AC 98 ms
11,000 KB
testcase_17 AC 202 ms
13,988 KB
testcase_18 AC 19 ms
8,372 KB
testcase_19 AC 108 ms
10,788 KB
testcase_20 AC 147 ms
11,376 KB
testcase_21 AC 120 ms
11,152 KB
testcase_22 AC 15 ms
8,240 KB
testcase_23 AC 160 ms
12,224 KB
testcase_24 AC 43 ms
9,088 KB
testcase_25 AC 111 ms
10,916 KB
testcase_26 AC 176 ms
12,076 KB
testcase_27 AC 74 ms
9,868 KB
testcase_28 AC 242 ms
15,228 KB
testcase_29 AC 183 ms
13,476 KB
testcase_30 AC 17 ms
8,756 KB
testcase_31 AC 156 ms
15,052 KB
testcase_32 AC 123 ms
14,112 KB
testcase_33 AC 75 ms
11,828 KB
testcase_34 AC 114 ms
13,608 KB
testcase_35 AC 82 ms
11,824 KB
testcase_36 AC 18 ms
8,816 KB
testcase_37 AC 25 ms
9,272 KB
testcase_38 AC 128 ms
13,368 KB
testcase_39 AC 46 ms
10,284 KB
testcase_40 AC 47 ms
9,976 KB
testcase_41 AC 100 ms
12,328 KB
testcase_42 AC 52 ms
10,100 KB
testcase_43 AC 60 ms
11,900 KB
testcase_44 AC 57 ms
11,140 KB
testcase_45 AC 42 ms
10,560 KB
testcase_46 AC 20 ms
9,080 KB
testcase_47 AC 14 ms
8,492 KB
testcase_48 AC 142 ms
15,212 KB
testcase_49 AC 8 ms
7,984 KB
testcase_50 RE -
testcase_51 AC 4 ms
7,604 KB
testcase_52 AC 301 ms
16,356 KB
testcase_53 AC 284 ms
16,364 KB
07_evil_01.txt AC 670 ms
25,364 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++) {
					co++;
					if (!bo[vec[now][j].first]) {
						bo[vec[now][j].first] = true;
						bo2[vec[now][j].second] = true;
						que.push(make_pair(vec[now][j].first, now));
					}
					else if (!bo2[vec[now][j].second]) {
						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