結果

問題 No.1266 7 Colors
ユーザー nebukuro09nebukuro09
提出日時 2020-10-23 23:14:05
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 332 ms / 3,000 ms
コード長 2,110 bytes
コンパイル時間 1,022 ms
コンパイル使用メモリ 115,132 KB
実行使用メモリ 34,740 KB
最終ジャッジ日時 2023-09-04 10:52:20
合計ジャッジ時間 9,200 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 242 ms
12,116 KB
testcase_04 AC 309 ms
31,120 KB
testcase_05 AC 247 ms
13,400 KB
testcase_06 AC 318 ms
33,688 KB
testcase_07 AC 327 ms
33,464 KB
testcase_08 AC 310 ms
31,628 KB
testcase_09 AC 298 ms
18,820 KB
testcase_10 AC 284 ms
14,992 KB
testcase_11 AC 256 ms
13,148 KB
testcase_12 AC 270 ms
13,668 KB
testcase_13 AC 277 ms
14,140 KB
testcase_14 AC 247 ms
13,440 KB
testcase_15 AC 332 ms
33,360 KB
testcase_16 AC 278 ms
13,920 KB
testcase_17 AC 328 ms
34,740 KB
testcase_18 AC 254 ms
21,580 KB
testcase_19 AC 235 ms
19,632 KB
testcase_20 AC 234 ms
19,632 KB
testcase_21 AC 169 ms
11,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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);
    }
}


0