結果
問題 | No.1640 簡単な色塗り |
ユーザー | SSRS |
提出日時 | 2021-08-06 21:48:31 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 283 ms / 2,000 ms |
コード長 | 2,705 bytes |
コンパイル時間 | 1,871 ms |
コンパイル使用メモリ | 188,036 KB |
実行使用メモリ | 16,156 KB |
最終ジャッジ日時 | 2024-06-29 14:57:51 |
合計ジャッジ時間 | 16,503 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 1 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 224 ms
9,600 KB |
testcase_05 | AC | 235 ms
16,156 KB |
testcase_06 | AC | 2 ms
6,944 KB |
testcase_07 | AC | 2 ms
6,944 KB |
testcase_08 | AC | 2 ms
6,944 KB |
testcase_09 | AC | 2 ms
6,940 KB |
testcase_10 | AC | 151 ms
8,832 KB |
testcase_11 | AC | 123 ms
7,936 KB |
testcase_12 | AC | 111 ms
8,320 KB |
testcase_13 | AC | 240 ms
13,060 KB |
testcase_14 | AC | 250 ms
13,656 KB |
testcase_15 | AC | 77 ms
6,944 KB |
testcase_16 | AC | 96 ms
7,680 KB |
testcase_17 | AC | 197 ms
11,520 KB |
testcase_18 | AC | 17 ms
6,940 KB |
testcase_19 | AC | 106 ms
7,296 KB |
testcase_20 | AC | 141 ms
8,448 KB |
testcase_21 | AC | 116 ms
7,936 KB |
testcase_22 | AC | 12 ms
6,944 KB |
testcase_23 | AC | 157 ms
9,600 KB |
testcase_24 | AC | 43 ms
6,944 KB |
testcase_25 | AC | 108 ms
7,552 KB |
testcase_26 | AC | 174 ms
9,344 KB |
testcase_27 | AC | 74 ms
6,940 KB |
testcase_28 | AC | 235 ms
13,392 KB |
testcase_29 | AC | 175 ms
10,752 KB |
testcase_30 | AC | 14 ms
6,940 KB |
testcase_31 | AC | 119 ms
12,972 KB |
testcase_32 | AC | 109 ms
12,160 KB |
testcase_33 | AC | 71 ms
8,960 KB |
testcase_34 | AC | 99 ms
11,388 KB |
testcase_35 | AC | 53 ms
7,680 KB |
testcase_36 | AC | 16 ms
6,944 KB |
testcase_37 | AC | 22 ms
6,940 KB |
testcase_38 | AC | 99 ms
11,136 KB |
testcase_39 | AC | 44 ms
6,944 KB |
testcase_40 | AC | 46 ms
6,944 KB |
testcase_41 | AC | 93 ms
10,364 KB |
testcase_42 | AC | 40 ms
6,944 KB |
testcase_43 | AC | 56 ms
8,320 KB |
testcase_44 | AC | 52 ms
7,680 KB |
testcase_45 | AC | 40 ms
7,040 KB |
testcase_46 | AC | 19 ms
6,944 KB |
testcase_47 | AC | 12 ms
6,944 KB |
testcase_48 | AC | 121 ms
13,172 KB |
testcase_49 | AC | 6 ms
6,940 KB |
testcase_50 | AC | 2 ms
6,944 KB |
testcase_51 | AC | 2 ms
6,944 KB |
testcase_52 | AC | 281 ms
14,540 KB |
testcase_53 | AC | 283 ms
14,536 KB |
07_evil_01.txt | AC | 655 ms
26,720 KB |
07_evil_02.txt | AC | 926 ms
29,276 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; int main(){ int N; cin >> N; vector<vector<pair<int, int>>> E(N); for (int i = 0; i < N; i++){ int A, B; cin >> A >> B; A--; B--; E[A].push_back(make_pair(i, B)); E[B].push_back(make_pair(i, A)); } bool ok = true; vector<int> ans(N); vector<bool> used(N, false); vector<int> d(N); for (int i = 0; i < N; i++){ d[i] = E[i].size(); } for (int i = 0; i < N; i++){ if (!used[i]){ used[i] = true; vector<int> id_v; set<int> id_e; queue<int> Q; Q.push(i); while (!Q.empty()){ int v = Q.front(); Q.pop(); id_v.push_back(v); for (auto P : E[v]){ id_e.insert(P.first); int w = P.second; if (!used[w]){ used[w] = true; Q.push(w); } } } if (id_v.size() != id_e.size()){ ok = false; break; } queue<int> Q2; for (int v : id_v){ if (d[v] == 1){ Q2.push(v); } } while (!Q2.empty()){ int v = Q2.front(); Q2.pop(); for (auto P : E[v]){ int id = P.first; int w = P.second; d[w]--; if (d[w] > 0){ ans[id] = v; } if (d[w] == 1){ Q2.push(w); } } } vector<int> cycle; for (int v : id_v){ if (d[v] == 2){ cycle.push_back(v); used[v] = false; } } vector<int> cycle_v = {cycle[0]}; vector<int> cycle_e; while (true){ int v = cycle_v.back(); used[v] = true; bool ok2 = false; for (auto P : E[v]){ int id = P.first; int w = P.second; if (!used[w]){ cycle_e.push_back(id); cycle_v.push_back(w); ok2 = true; break; } } if (!ok2){ for (auto P : E[v]){ int id = P.first; int w = P.second; if (w == cycle[0]){ bool ok3 = true; if (cycle_e.size() == 1){ if (cycle_e[0] == id){ ok3 = false; } } if (ok3){ cycle_e.push_back(id); break; } } } break; } } int cnt = cycle.size(); for (int j = 0; j < cnt; j++){ ans[cycle_e[j]] = cycle_v[j]; } } } if (!ok){ cout << "No" << endl; } else { cout << "Yes" << endl; for (int i = 0; i < N; i++){ cout << ans[i] + 1 << endl; } } }