結果

問題 No.1266 7 Colors
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-10-23 23:04:30
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 326 ms / 3,000 ms
コード長 3,509 bytes
コンパイル時間 21,708 ms
コンパイル使用メモリ 114,092 KB
実行使用メモリ 22,612 KB
最終ジャッジ日時 2023-09-04 10:51:47
合計ジャッジ時間 29,060 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 235 ms
14,224 KB
testcase_04 AC 298 ms
18,300 KB
testcase_05 AC 246 ms
14,456 KB
testcase_06 AC 303 ms
19,476 KB
testcase_07 AC 316 ms
20,416 KB
testcase_08 AC 306 ms
19,928 KB
testcase_09 AC 285 ms
17,700 KB
testcase_10 AC 275 ms
16,728 KB
testcase_11 AC 252 ms
15,168 KB
testcase_12 AC 255 ms
15,608 KB
testcase_13 AC 262 ms
15,856 KB
testcase_14 AC 245 ms
14,492 KB
testcase_15 AC 326 ms
20,236 KB
testcase_16 AC 252 ms
17,056 KB
testcase_17 AC 314 ms
20,196 KB
testcase_18 AC 267 ms
22,612 KB
testcase_19 AC 245 ms
20,792 KB
testcase_20 AC 245 ms
21,136 KB
testcase_21 AC 165 ms
7,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}


enum K = 7;

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const M = readInt();
      const Q = readInt();
      auto S = new string[N];
      foreach (u; 0 .. N) {
        S[u] = readToken();
      }
      auto U = new int[M];
      auto V = new int[M];
      foreach (i; 0 .. M) {
        U[i] = readInt() - 1;
        V[i] = readInt() - 1;
      }
      auto T = new int[Q];
      auto X = new int[Q];
      auto Y = new int[Q];
      foreach (q; 0 .. Q) {
        T[q] = readInt();
        X[q] = readInt() - 1;
        Y[q] = readInt() - 1;
      }
      
      auto ss = new bool[][](N, K);
      foreach (u; 0 .. N) {
        foreach (k; 0 .. K) {
          ss[u][k] = (S[u][k] == '1');
        }
      }
      
      auto G = new int[][N];
      foreach (i; 0 .. M) {
        G[U[i]] ~= i;
        G[V[i]] ~= i;
      }
      
      auto uf = new int[N * K];
      uf[] = -1;
      foreach (u; 0 .. N) {
        foreach (k; 0 .. K) {
          if (ss[u][k] && ss[u][(k + 1) % K]) {
            uf.connect(u * K + k, u * K + (k + 1) % K);
          }
        }
      }
      foreach (i; 0 .. M) {
        foreach (k; 0 .. K) {
          if (ss[U[i]][k] && ss[V[i]][k]) {
            uf.connect(U[i] * K + k, V[i] * K + k);
          }
        }
      }
      debug {
        writeln("uf = ", uf);
      }
      
      foreach (q; 0 .. Q) {
        switch (T[q]) {
          case 1: {
            ss[X[q]][Y[q]] = true;
            foreach (k; [(Y[q] + K - 1) % K, (Y[q] + 1) % K]) {
              if (ss[X[q]][k]) {
                uf.connect(X[q] * K + Y[q], X[q] * K + k);
              }
            }
            foreach (i; G[X[q]]) {
              const v = U[i] ^ V[i] ^ X[q];
              if (ss[v][Y[q]]) {
                uf.connect(X[q] * K + Y[q], v * K + Y[q]);
              }
            }
          } break;
          case 2: {
            const ans = -uf[uf.root(X[q] * K + 0)];
            writeln(ans);
          } break;
          default: assert(false);
        }
      }
    }
  } catch (EOFException e) {
  }
}
0