結果

問題 No.1266 7 Colors
ユーザー stoqstoq
提出日時 2020-10-22 13:56:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 323 ms / 3,000 ms
コード長 1,766 bytes
コンパイル時間 2,209 ms
コンパイル使用メモリ 207,580 KB
実行使用メモリ 17,392 KB
最終ジャッジ日時 2023-09-28 14:35:51
合計ジャッジ時間 10,547 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 236 ms
5,876 KB
testcase_04 AC 303 ms
13,196 KB
testcase_05 AC 239 ms
6,840 KB
testcase_06 AC 308 ms
14,032 KB
testcase_07 AC 323 ms
15,860 KB
testcase_08 AC 296 ms
13,392 KB
testcase_09 AC 273 ms
11,056 KB
testcase_10 AC 289 ms
10,276 KB
testcase_11 AC 242 ms
7,588 KB
testcase_12 AC 243 ms
8,412 KB
testcase_13 AC 251 ms
9,048 KB
testcase_14 AC 233 ms
6,524 KB
testcase_15 AC 310 ms
15,984 KB
testcase_16 AC 252 ms
8,852 KB
testcase_17 AC 312 ms
15,308 KB
testcase_18 AC 275 ms
17,392 KB
testcase_19 AC 172 ms
16,892 KB
testcase_20 AC 167 ms
16,888 KB
testcase_21 AC 216 ms
4,388 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