結果
問題 | No.1640 簡単な色塗り |
ユーザー | tnakao0123 |
提出日時 | 2021-08-08 03:06:55 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 58 ms / 2,000 ms |
コード長 | 2,808 bytes |
コンパイル時間 | 1,009 ms |
コンパイル使用メモリ | 97,936 KB |
実行使用メモリ | 12,612 KB |
最終ジャッジ日時 | 2024-06-29 17:05:44 |
合計ジャッジ時間 | 9,069 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
7,168 KB |
testcase_01 | AC | 3 ms
6,784 KB |
testcase_02 | AC | 2 ms
7,296 KB |
testcase_03 | AC | 3 ms
7,296 KB |
testcase_04 | AC | 27 ms
9,088 KB |
testcase_05 | AC | 40 ms
12,544 KB |
testcase_06 | AC | 3 ms
7,296 KB |
testcase_07 | AC | 3 ms
7,168 KB |
testcase_08 | AC | 3 ms
7,296 KB |
testcase_09 | AC | 3 ms
6,784 KB |
testcase_10 | AC | 31 ms
10,752 KB |
testcase_11 | AC | 28 ms
9,984 KB |
testcase_12 | AC | 24 ms
9,856 KB |
testcase_13 | AC | 49 ms
12,416 KB |
testcase_14 | AC | 49 ms
12,544 KB |
testcase_15 | AC | 17 ms
9,088 KB |
testcase_16 | AC | 21 ms
9,344 KB |
testcase_17 | AC | 41 ms
11,520 KB |
testcase_18 | AC | 6 ms
7,552 KB |
testcase_19 | AC | 23 ms
9,600 KB |
testcase_20 | AC | 32 ms
10,624 KB |
testcase_21 | AC | 26 ms
9,856 KB |
testcase_22 | AC | 5 ms
7,552 KB |
testcase_23 | AC | 33 ms
10,880 KB |
testcase_24 | AC | 11 ms
8,320 KB |
testcase_25 | AC | 24 ms
9,856 KB |
testcase_26 | AC | 36 ms
11,264 KB |
testcase_27 | AC | 17 ms
8,960 KB |
testcase_28 | AC | 50 ms
12,288 KB |
testcase_29 | AC | 36 ms
11,264 KB |
testcase_30 | AC | 8 ms
7,424 KB |
testcase_31 | AC | 37 ms
10,880 KB |
testcase_32 | AC | 34 ms
10,368 KB |
testcase_33 | AC | 23 ms
9,344 KB |
testcase_34 | AC | 28 ms
9,728 KB |
testcase_35 | AC | 22 ms
9,216 KB |
testcase_36 | AC | 7 ms
7,296 KB |
testcase_37 | AC | 9 ms
7,680 KB |
testcase_38 | AC | 37 ms
10,496 KB |
testcase_39 | AC | 15 ms
8,320 KB |
testcase_40 | AC | 15 ms
8,448 KB |
testcase_41 | AC | 29 ms
9,728 KB |
testcase_42 | AC | 17 ms
8,704 KB |
testcase_43 | AC | 20 ms
8,704 KB |
testcase_44 | AC | 18 ms
8,704 KB |
testcase_45 | AC | 14 ms
8,320 KB |
testcase_46 | AC | 8 ms
7,424 KB |
testcase_47 | AC | 6 ms
7,168 KB |
testcase_48 | AC | 34 ms
10,624 KB |
testcase_49 | AC | 5 ms
6,912 KB |
testcase_50 | AC | 3 ms
6,784 KB |
testcase_51 | AC | 3 ms
7,168 KB |
testcase_52 | AC | 58 ms
12,544 KB |
testcase_53 | AC | 56 ms
12,612 KB |
07_evil_01.txt | RE | - |
07_evil_02.txt | RE | - |
ソースコード
/* -*- coding: utf-8 -*- * * 1640.cc: No.1640 簡単な色塗り - yukicoder */ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<string> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<deque> #include<algorithm> #include<numeric> #include<utility> #include<complex> #include<functional> using namespace std; /* constant */ const int MAX_N = 100000; /* typedef */ typedef pair<int,int> pii; typedef vector<pii> vpii; struct UFT { int links[MAX_N], ranks[MAX_N], sizes[MAX_N]; UFT() {} void init(int n) { for (int i = 0; i < n; i++) links[i] = i, ranks[i] = sizes[i] = 1; } int root(int i) { int i0 = i; while (links[i0] != i0) i0 = links[i0]; return (links[i] = i0); } int rank(int i) { return ranks[root(i)]; } int size(int i) { return sizes[root(i)]; } bool same(int i, int j) { return root(i) == root(j); } int merge(int i0, int i1) { int r0 = root(i0), r1 = root(i1), mr; if (r0 == r1) return r0; if (ranks[r0] == ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; ranks[r0]++; mr = r0; } else if (ranks[r0] > ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; mr = r0; } else { links[r0] = r1; sizes[r1] += sizes[r0]; mr = r1; } return mr; } }; /* global variables */ UFT uft; vpii nbrs[MAX_N]; int rts[MAX_N], res[MAX_N], eps[MAX_N]; int ps[MAX_N], pes[MAX_N], cis[MAX_N]; /* subroutines */ /* main */ int main() { int n; scanf("%d", &n); uft.init(n); memset(rts, -1, sizeof(rts)); memset(res, -1, sizeof(res)); for (int i = 0; i < n; i++) { int a, b; scanf("%d%d", &a, &b); a--, b--; int ra = uft.root(a), rb = uft.root(b); if (ra == rb) rts[ra] = a, res[ra] = i; else { int rm = uft.merge(ra, rb); if (rts[rm] < 0) { if (rts[ra] >= 0) rts[rm] = rts[ra], res[rm] = res[ra]; else rts[rm] = rts[rb], res[rm] = res[rb]; } nbrs[a].push_back(pii(b, i)); nbrs[b].push_back(pii(a, i)); } } for (int u = 0; u < n; u++) if (rts[uft.root(u)] < 0) { puts("No"); return 0; } memset(eps, -1, sizeof(eps)); for (int ru = 0; ru < n; ru++) { if (ru != uft.root(ru)) continue; int rt = rts[ru], re = res[ru]; eps[re] = rt; ps[rt] = pes[rt] = -1; for (int u = rt; u >= 0;) { vpii &nbru = nbrs[u]; int up = ps[u]; if (cis[u] < nbru.size()) { pii ve = nbru[cis[u]++]; int v = ve.first, e = ve.second; if (v != up) { ps[v] = u, pes[v] = e; u = v; } } else { if (up >= 0) eps[pes[u]] = u; u = up; } } } puts("Yes"); for (int i = 0; i < n; i++) printf("%d\n", eps[i] + 1); return 0; }