結果

問題 No.1640 簡単な色塗り
ユーザー karinohitokarinohito
提出日時 2021-08-07 21:04:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 627 ms / 2,000 ms
コード長 1,782 bytes
コンパイル時間 3,139 ms
コンパイル使用メモリ 194,656 KB
実行使用メモリ 96,216 KB
最終ジャッジ日時 2023-09-12 03:58:25
合計ジャッジ時間 25,070 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 361 ms
96,036 KB
testcase_05 AC 360 ms
96,216 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 339 ms
57,764 KB
testcase_11 AC 275 ms
47,784 KB
testcase_12 AC 242 ms
42,764 KB
testcase_13 AC 561 ms
86,464 KB
testcase_14 AC 545 ms
85,560 KB
testcase_15 AC 167 ms
31,948 KB
testcase_16 AC 207 ms
38,424 KB
testcase_17 AC 444 ms
71,920 KB
testcase_18 AC 28 ms
9,392 KB
testcase_19 AC 236 ms
42,712 KB
testcase_20 AC 326 ms
55,792 KB
testcase_21 AC 253 ms
46,400 KB
testcase_22 AC 19 ms
7,492 KB
testcase_23 AC 355 ms
60,008 KB
testcase_24 AC 80 ms
18,944 KB
testcase_25 AC 249 ms
43,064 KB
testcase_26 AC 403 ms
65,852 KB
testcase_27 AC 156 ms
30,484 KB
testcase_28 AC 528 ms
82,208 KB
testcase_29 AC 409 ms
65,044 KB
testcase_30 AC 42 ms
16,832 KB
testcase_31 AC 345 ms
93,580 KB
testcase_32 AC 293 ms
82,040 KB
testcase_33 AC 185 ms
56,896 KB
testcase_34 AC 240 ms
70,060 KB
testcase_35 AC 183 ms
56,948 KB
testcase_36 AC 41 ms
16,868 KB
testcase_37 AC 54 ms
22,068 KB
testcase_38 AC 304 ms
85,456 KB
testcase_39 AC 115 ms
38,436 KB
testcase_40 AC 116 ms
39,512 KB
testcase_41 AC 241 ms
71,840 KB
testcase_42 AC 137 ms
44,232 KB
testcase_43 AC 144 ms
46,084 KB
testcase_44 AC 137 ms
44,760 KB
testcase_45 AC 98 ms
34,772 KB
testcase_46 AC 42 ms
18,376 KB
testcase_47 AC 29 ms
12,724 KB
testcase_48 AC 307 ms
87,256 KB
testcase_49 AC 11 ms
7,460 KB
testcase_50 AC 1 ms
4,376 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 AC 627 ms
95,864 KB
testcase_53 AC 622 ms
96,028 KB
07_evil_01.txt AC 1,455 ms
189,616 KB
07_evil_02.txt TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

//#include <atcoder/all>
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
//using namespace atcoder;
using ll = long long;
#define all(A) A.begin(),A.end()
using vll = vector<ll>;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
using Graph = vector<vector<ll>>;
Graph G;
vll E;
vector<bool> seen;

int main() {

	ll N;
	cin >> N;
	Graph G(N);
	vector<tuple<ll, ll, ll>> edge(N);
	vll E(N, 0);
	vector<multiset<ll>> SE(N);
	map<pair<ll, ll>, queue<ll>> M;
	rep(i, N) {
		ll A, B;
		cin >> A >> B;
		A--; B--;
		edge[i] = make_tuple(A, B, -1);
		M[make_pair(min(A, B), max(A, B))].push(i);
		SE[A].insert(B);
		SE[B].insert(A);
	}
	queue<ll> Q;
	rep(i, N) {
		if (SE[i].size() == 1) {
			Q.push(i);
		}
		else if (SE[i].size() == 0) {
			cout << "No" << endl;
			return 0;
		}
	}
	ll k = 0;
	ll j = 0;
	vector<bool> USE(N,false);
	while (1) {
		while (!Q.empty()) {
			auto p = Q.front();
			USE[p]=true;
			Q.pop();
			ll n = *SE[p].begin();
			ll m = M[make_pair(min(p, n), max(p, n))].front();
			M[make_pair(min(p, n), max(p, n))].pop();
			edge[m] = make_tuple(p, n, p);
			k++;
			SE[p].erase(SE[p].find(n));
			SE[n].erase(SE[n].find(p));
			if(k!=N&&SE[n].size()==0&&USE[n]==false){
				cout<<"No"<<endl;
				return 0;
			}
			if (SE[n].size() == 1) {
				Q.push(n);
			}

		}
		if (k == N) {
			cout << "Yes" << endl;
			rep(i, N) {
				cout << 1+get<2>(edge[i]) << endl;
			}
			return 0;
		}
		while (SE[j].size() == 0||USE[j]) {
			j++;
		}
		ll p = j;
		USE[p]=true;
		ll n = *SE[p].begin();
		ll m = M[make_pair(min(p, n), max(p, n))].front();
		M[make_pair(min(p, n), max(p, n))].pop();
		edge[m] = make_tuple(p, n, p);
		k++;
		SE[p].erase(SE[p].find(n));
		SE[n].erase(SE[n].find(p));
		if (SE[n].size() == 1) {
			Q.push(n);
		}

	}
}
0