結果

問題 No.1266 7 Colors
ユーザー tonyu0tonyu0
提出日時 2020-10-24 00:19:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 325 ms / 3,000 ms
コード長 2,065 bytes
コンパイル時間 1,330 ms
コンパイル使用メモリ 123,088 KB
実行使用メモリ 14,800 KB
最終ジャッジ日時 2023-09-28 19:31:43
合計ジャッジ時間 8,123 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 245 ms
5,676 KB
testcase_04 AC 316 ms
11,108 KB
testcase_05 AC 253 ms
6,168 KB
testcase_06 AC 314 ms
11,952 KB
testcase_07 AC 322 ms
13,080 KB
testcase_08 AC 309 ms
11,284 KB
testcase_09 AC 285 ms
9,268 KB
testcase_10 AC 279 ms
8,940 KB
testcase_11 AC 255 ms
6,748 KB
testcase_12 AC 259 ms
7,468 KB
testcase_13 AC 272 ms
8,064 KB
testcase_14 AC 244 ms
6,008 KB
testcase_15 AC 325 ms
13,208 KB
testcase_16 AC 265 ms
7,588 KB
testcase_17 AC 323 ms
12,628 KB
testcase_18 AC 274 ms
14,800 KB
testcase_19 AC 163 ms
14,384 KB
testcase_20 AC 164 ms
14,340 KB
testcase_21 AC 226 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (n); ++i)
#define rrep(i, j, n) for (int i = (n)-1; j <= i; --i)

template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> &a) {
  os << "{";
  for (size_t i = 0; i < a.size(); ++i) os << (i > 0 ? "," : "") << a[i];
  return os << "}";
}

[[maybe_unused]] constexpr ll MOD = 1000000007;
[[maybe_unused]] constexpr int INF = 0x3f3f3f3f;
[[maybe_unused]] constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL;

class disjoint_set {
public:
  disjoint_set(int n) : par(n, -1) {}
  void merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return;
    if (par[x] > par[y]) swap(x, y);
    par[x] += par[y];
    par[y] = x;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int root(int x) {
    if (par[x] < 0) return x;
    return par[x] = root(par[x]);
  }
  int sizeOf(int x) { return -par[root(x)]; }

private:
  vector<int> par;
};

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  // 頂点iのj色はi * 7 + jで
  vector<string> s(n);
  vector<vector<int>> g(n);
  disjoint_set ds(n * 7);
  rep(i, 0, n) {
    cin >> s[i];
    rep(j, 0, 7) if (s[i][j] == '1' && s[i][(j + 1) % 7] == '1')
      ds.merge(i * 7 + j, i * 7 + (j + 1) % 7);
  }

  rep(i, 0, m) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    g[u].push_back(v);
    g[v].push_back(u);
    rep(j, 0, 7) if (s[u][j] == '1' && s[v][j] == '1')
      ds.merge(u * 7 + j, v * 7 + j);
  }

  rep(i, 0, q) {
    int t, x, y;
    cin >> t >> x >> y;
    --x, --y;
    if (t == 1) {
      s[x][y] = '1';
      rep(j, 0, 7) if (s[x][j] == '1' && s[x][(j + 1) % 7] == '1')
        ds.merge(x * 7 + j, x * 7 + (j + 1) % 7);
      for (int nx : g[x])
        if (s[nx][y] == '1') ds.merge(x * 7 + y, nx * 7 + y);
    } else
      cout << ds.sizeOf(x * 7) << '\n';
  }
  return 0;
}
0