結果
| 問題 |
No.1266 7 Colors
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-10-23 23:14:05 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 333 ms / 3,000 ms |
| コード長 | 2,110 bytes |
| コンパイル時間 | 814 ms |
| コンパイル使用メモリ | 133,824 KB |
| 実行使用メモリ | 34,192 KB |
| 最終ジャッジ日時 | 2024-06-22 09:46:55 |
| 合計ジャッジ時間 | 7,294 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdlib, std.datetime;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
auto Q = s[2];
auto G = new int[][](N);
auto colors = new bool[][](N, 7);
auto uf = new UnionFind(N * 7);
foreach (i; 0..N) {
auto t = readln.chomp;
foreach (j; 0..7) if (t[j] == '1') colors[i][j] = true;
foreach (j; 0..7) if (colors[i][j] && colors[i][(j+1)%7]) uf.unite(i + N * j, i + (N * ((j + 1) % 7)));
}
foreach (_; 0..M) {
s = readln.split.map!(to!int);
auto u = s[0] - 1;
auto v = s[1] - 1;
G[u] ~= v;
G[v] ~= u;
foreach (i; 0..7) if (colors[u][i] && colors[v][i]) uf.unite(u + N * i, v + N * i);
}
foreach (_; 0..Q) {
auto q = readln.split.map!(to!int);
auto x = q[1] - 1;
auto y = q[2] - 1;
if (q[0] == 1) {
colors[x][y] = true;
foreach (v; G[x]) if (colors[v][y]) uf.unite(x + N * y, v + N * y);
int prev = (y + 6) % 7;
int next = (y + 1) % 7;
if (colors[x][prev]) uf.unite(x + N * y, x + N * prev);
if (colors[x][next]) uf.unite(x + N * y, x + N * next);
} else {
uf.size[uf.find(x)].writeln;
}
}
}
class UnionFind {
import std.algorithm : swap;
int n;
int[] table;
int[] size;
this(int n) {
this.n = n;
table = new int[](n);
table[] = -1;
size = new int[](n);
size[] = 1;
}
int find(int x) {
return table[x] < 0 ? x : (table[x] = find(table[x]));
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
if (table[x] > table[y]) swap(x, y);
table[x] += table[y];
table[y] = x;
size[x] += size[y];
}
bool same(int x, int y) {
return find(x) == find(y);
}
}