結果

問題 No.1266 7 Colors
ユーザー tonyu0tonyu0
提出日時 2020-10-24 00:13:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,053 bytes
コンパイル時間 1,196 ms
コンパイル使用メモリ 123,896 KB
実行使用メモリ 14,804 KB
最終ジャッジ日時 2023-09-28 19:29:16
合計ジャッジ時間 8,233 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 269 ms
14,804 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 220 ms
4,376 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);
  }

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