結果

問題 No.1266 7 Colors
ユーザー 👑 emthrmemthrm
提出日時 2020-10-23 22:34:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 172 ms / 3,000 ms
コード長 2,313 bytes
コンパイル時間 2,957 ms
コンパイル使用メモリ 207,508 KB
実行使用メモリ 14,712 KB
最終ジャッジ日時 2023-09-28 16:41:26
合計ジャッジ時間 6,533 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 77 ms
5,644 KB
testcase_04 AC 145 ms
10,976 KB
testcase_05 AC 83 ms
6,180 KB
testcase_06 AC 155 ms
11,964 KB
testcase_07 AC 162 ms
13,232 KB
testcase_08 AC 153 ms
11,164 KB
testcase_09 AC 135 ms
9,444 KB
testcase_10 AC 119 ms
8,796 KB
testcase_11 AC 99 ms
6,676 KB
testcase_12 AC 106 ms
7,540 KB
testcase_13 AC 109 ms
8,240 KB
testcase_14 AC 84 ms
5,880 KB
testcase_15 AC 172 ms
13,280 KB
testcase_16 AC 103 ms
7,496 KB
testcase_17 AC 162 ms
12,756 KB
testcase_18 AC 68 ms
14,712 KB
testcase_19 AC 70 ms
14,292 KB
testcase_20 AC 70 ms
14,336 KB
testcase_21 AC 40 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr ll LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

struct UnionFind {
  UnionFind(int n) : data(n, -1) {}

  int root(int ver) { return data[ver] < 0 ? ver : data[ver] = root(data[ver]); }

  bool unite(int u, int v) {
    u = root(u);
    v = root(v);
    if (u == v) return false;
    if (data[u] > data[v]) std::swap(u, v);
    data[u] += data[v];
    data[v] = u;
    return true;
  }

  bool same(int u, int v) { return root(u) == root(v); }

  int size(int ver) { return -data[root(ver)]; }

private:
  std::vector<int> data;
};

int main() {
  int n, m, q; cin >> n >> m >> q;
  vector<string> s(n); REP(i, n) cin >> s[i];
  UnionFind uf(n * 7);
  REP(i, n) REP(j, 7) {
    if (s[i][j] == '1' && s[i][(j + 1) % 7] == '1') uf.unite(i * 7 + j, i * 7 + (j + 1) % 7);
  }
  vector<vector<int>> graph(n);
  while (m--) {
    int u, v; cin >> u >> v; --u; --v;
    graph[u].emplace_back(v);
    graph[v].emplace_back(u);
    REP(i, 7) {
      if (s[u][i] == '1' && s[v][i] == '1') uf.unite(u * 7 + i, v * 7 + i);
    }
  }
  while (q--) {
    int query, x, y; cin >> query >> x >> y; --x;
    if (query == 1) {
      --y;
      s[x][y] = '1';
      if (s[x][(y + 6) % 7] == '1') uf.unite(x * 7 + (y + 6) % 7, x * 7 + y);
      if (s[x][(y + 1) % 7] == '1') uf.unite(x * 7 + y, x * 7 + (y + 1) % 7);
      for (int e : graph[x]) {
        if (s[e][y] == '1') uf.unite(x * 7 + y, e * 7 + y);
      }
    } else if (query == 2) {
      cout << uf.size(x * 7) << '\n';
    }
  }
  return 0;
}
0