結果
問題 | No.1266 7 Colors |
ユーザー | tnakao0123 |
提出日時 | 2020-10-24 22:53:31 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 162 ms / 3,000 ms |
コード長 | 2,884 bytes |
コンパイル時間 | 895 ms |
コンパイル使用メモリ | 98,720 KB |
実行使用メモリ | 17,800 KB |
最終ジャッジ日時 | 2024-07-21 15:48:19 |
合計ジャッジ時間 | 4,768 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 4 ms
6,016 KB |
testcase_01 | AC | 4 ms
6,144 KB |
testcase_02 | AC | 4 ms
5,888 KB |
testcase_03 | AC | 81 ms
8,576 KB |
testcase_04 | AC | 142 ms
14,336 KB |
testcase_05 | AC | 87 ms
9,216 KB |
testcase_06 | AC | 147 ms
15,232 KB |
testcase_07 | AC | 155 ms
16,384 KB |
testcase_08 | AC | 146 ms
14,592 KB |
testcase_09 | AC | 125 ms
12,672 KB |
testcase_10 | AC | 128 ms
11,904 KB |
testcase_11 | AC | 100 ms
9,984 KB |
testcase_12 | AC | 100 ms
10,496 KB |
testcase_13 | AC | 109 ms
11,264 KB |
testcase_14 | AC | 88 ms
9,088 KB |
testcase_15 | AC | 160 ms
16,768 KB |
testcase_16 | AC | 104 ms
10,624 KB |
testcase_17 | AC | 162 ms
16,128 KB |
testcase_18 | AC | 78 ms
17,800 KB |
testcase_19 | AC | 77 ms
17,664 KB |
testcase_20 | AC | 78 ms
17,736 KB |
testcase_21 | AC | 52 ms
7,040 KB |
ソースコード
/* -*- coding: utf-8 -*- * * 1266.cc: No.1266 7 Colors - 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; const int C = 7; const int MAX_GN = MAX_N * C; /* typedef */ typedef vector<int> vi; struct UFT { int links[MAX_GN], ranks[MAX_GN], sizes[MAX_GN]; 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 */ int cbs[MAX_N]; vi nbrs[MAX_N]; UFT uft; /* subroutines */ inline int upos(int i, int j) { return i * C + j; } /* main */ int main() { int n, m, qn; scanf("%d%d%d", &n, &m, &qn); uft.init(n * C); for (int i = 0; i < n; i++) { char s[16]; scanf("%s", s); for (int j = 0, bj = 1; j < C; j++, bj <<= 1) cbs[i] |= (bj * (s[j] - '0')); for (int j = 0, bj = 1; j < C; j++, bj <<= 1) { int j1 = (j + 1) % C, bj1 = (1 << j1); if ((cbs[i] & bj) && (cbs[i] & bj1)) uft.merge(upos(i, j), upos(i, j1)); } } for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); u--, v--; nbrs[u].push_back(v); nbrs[v].push_back(u); for (int j = 0, bj = 1; j < C; j++, bj <<= 1) if ((cbs[u] & bj) && (cbs[v] & bj)) uft.merge(upos(u, j), upos(v, j)); } while (qn--) { int op, x, y; scanf("%d%d%d", &op, &x, &y); x--, y--; if (op == 1) { int by = 1 << y; cbs[x] |= by; int pxy = upos(x, y); vi &nbrx = nbrs[x]; for (vi::iterator vit = nbrx.begin(); vit != nbrx.end(); vit++) { int v = *vit; if (cbs[v] & by) uft.merge(pxy, upos(v, y)); } int y0 = (y + C - 1) % C, y1 = (y + 1) % C; if (cbs[x] & (1 << y0)) uft.merge(pxy, upos(x, y0)); if (cbs[x] & (1 << y1)) uft.merge(pxy, upos(x, y1)); } else { int sz = uft.size(upos(x, 0)); printf("%d\n", sz); } } return 0; }