結果

問題 No.1266 7 Colors
ユーザー 0w10w1
提出日時 2020-11-15 16:17:40
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 283 ms / 3,000 ms
コード長 1,459 bytes
コンパイル時間 2,620 ms
コンパイル使用メモリ 206,668 KB
実行使用メモリ 21,996 KB
最終ジャッジ日時 2023-09-30 07:00:48
合計ジャッジ時間 8,958 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 199 ms
5,916 KB
testcase_04 AC 274 ms
12,792 KB
testcase_05 AC 200 ms
6,456 KB
testcase_06 AC 276 ms
13,876 KB
testcase_07 AC 283 ms
15,388 KB
testcase_08 AC 270 ms
13,044 KB
testcase_09 AC 255 ms
10,704 KB
testcase_10 AC 248 ms
9,876 KB
testcase_11 AC 215 ms
7,268 KB
testcase_12 AC 223 ms
8,452 KB
testcase_13 AC 234 ms
9,180 KB
testcase_14 AC 201 ms
6,480 KB
testcase_15 AC 282 ms
15,708 KB
testcase_16 AC 228 ms
8,324 KB
testcase_17 AC 280 ms
14,976 KB
testcase_18 AC 257 ms
21,996 KB
testcase_19 AC 132 ms
19,372 KB
testcase_20 AC 133 ms
19,324 KB
testcase_21 AC 177 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);

  int N, M, Q; {
    cin >> N >> M >> Q;
  }

  vector<int> on(7 * N); {
    for (int i = 0; i < N; ++i) {
      string S; {
        cin >> S;
      }
      for (int j = 0; j < 7; ++j) {
        on[7 * i + j] = S[j] == '1';
      }
    }
  }

  vector<vector<int>> G(N); {
    for (int i = 0; i < M; ++i) {
      int U, V; {
        cin >> U >> V;
        --U, --V;
      }
      G[U].push_back(V);
      G[V].push_back(U);
    }
  }

  vector<int> sz(N * 7, 1);
  vector<int> fa(N * 7); { iota(fa.begin(), fa.end(), 0); }
  function<int(int)> find = [&](int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); };
  function<void(int, int)> unite = [&](int x, int y) {
    int fx = find(x);
    int fy = find(y);
    if (fx == fy) return;
    sz[fx] += sz[fy];
    fa[fy] = fx;
  };
  
  auto update = [&](int X, int Y) {
    if (on[X * 7 + (Y + 1) % 7]) unite(X * 7 + (Y + 1) % 7, X * 7 + Y);
    if (on[X * 7 + (Y + 6) % 7]) unite(X * 7 + (Y + 6) % 7, X * 7 + Y);
    for (int v : G[X]) if (on[v * 7 + Y]) unite(v * 7 + Y, X * 7 + Y);
  };

  for (int u = 0; u < N; ++u) {
    for (int c = 0; c < 7; ++c) {
      if (on[u * 7 + c]) update(u, c);
    }
  }

  while (Q--) {
    int P, X, Y; {
      cin >> P >> X >> Y;
      --X, --Y;
    }
    if (P == 1) {
      on[X * 7 + Y] = 1;
      update(X, Y);
    } else {
      cout << sz[find(X * 7)] << '\n';
    }
  }
}
0