結果

問題 No.1266 7 Colors
ユーザー 0w1
提出日時 2020-11-15 16:17:40
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 507 ms / 3,000 ms
コード長 1,459 bytes
コンパイル時間 3,075 ms
コンパイル使用メモリ 202,592 KB
最終ジャッジ日時 2025-01-16 00:58:23
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

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