結果

問題 No.1266 7 Colors
ユーザー stoqstoq
提出日時 2020-10-13 03:45:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 316 ms / 3,000 ms
コード長 1,766 bytes
コンパイル時間 2,214 ms
コンパイル使用メモリ 205,788 KB
実行使用メモリ 17,488 KB
最終ジャッジ日時 2023-09-27 23:56:20
合計ジャッジ時間 8,740 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 230 ms
5,900 KB
testcase_04 AC 275 ms
12,784 KB
testcase_05 AC 224 ms
6,560 KB
testcase_06 AC 277 ms
14,052 KB
testcase_07 AC 292 ms
15,860 KB
testcase_08 AC 280 ms
13,404 KB
testcase_09 AC 256 ms
11,020 KB
testcase_10 AC 248 ms
10,176 KB
testcase_11 AC 232 ms
7,440 KB
testcase_12 AC 231 ms
8,468 KB
testcase_13 AC 233 ms
9,116 KB
testcase_14 AC 230 ms
6,536 KB
testcase_15 AC 316 ms
15,964 KB
testcase_16 AC 244 ms
8,640 KB
testcase_17 AC 308 ms
15,436 KB
testcase_18 AC 275 ms
17,488 KB
testcase_19 AC 165 ms
17,228 KB
testcase_20 AC 166 ms
17,336 KB
testcase_21 AC 216 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < int(n); ++i)

struct UnionFind
{
  vector<int> par, sizes;

  UnionFind(int n) : par(n), sizes(n, 1)
  {
    iota(begin(par), end(par), 0);
  }

  int root(int x)
  {
    if (x == par[x])
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y)
  {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (sizes[x] < sizes[y])
      swap(x, y);
    par[y] = x;
    sizes[x] += sizes[y];
  }

  bool same(int x, int y) { return root(x) == root(y); }

  int size(int x) { return sizes[root(x)]; }
};

int main()
{
  int n, m, q;
  cin >> n >> m >> q;
  vector<string> s(n);
  rep(i, n) cin >> s[i];
  vector<vector<int>> G(n);
  rep(i, m)
  {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  UnionFind uf(n * 7);
  rep(i, n) rep(j, 7)
  {
    if (s[i][j] == '0')
      continue;
    for (auto to : G[i])
    {
      if (s[to][j] == '1')
        uf.unite(i * 7 + j, to * 7 + j);
    }
    int up = (j + 1) % 7, down = (j - 1 + 7) % 7;
    if (s[i][up] == '1')
      uf.unite(i * 7 + j, i * 7 + up);
    if (s[i][down] == '1')
      uf.unite(i * 7 + j, i * 7 + down);
  }

  rep(qi, q)
  {
    int type, x, y;
    cin >> type >> x >> y;
    x--, y--;
    if (type == 1)
    {
      s[x][y] = '1';
      for (auto to : G[x])
      {
        if (s[to][y] == '1')
          uf.unite(x * 7 + y, to * 7 + y);
      }
      int up = (y + 1) % 7, down = (y - 1 + 7) % 7;
      if (s[x][up] == '1')
        uf.unite(x * 7 + y, x * 7 + up);
      if (s[x][down] == '1')
        uf.unite(x * 7 + y, x * 7 + down);
    }
    else
    {
      cout << uf.size(x * 7) << "\n";
    }
  }
}
0