結果

問題 No.1266 7 Colors
ユーザー marroncastlemarroncastle
提出日時 2020-10-24 00:24:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,690 bytes
コンパイル時間 3,057 ms
コンパイル使用メモリ 172,504 KB
実行使用メモリ 14,832 KB
最終ジャッジ日時 2023-09-28 19:35:23
合計ジャッジ時間 5,509 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 79 ms
5,704 KB
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 71 ms
14,832 KB
testcase_19 AC 74 ms
14,336 KB
testcase_20 AC 73 ms
14,404 KB
testcase_21 AC 40 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// g++ A.cpp -std=c++14 -I . && ./a.out
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, a, b) for (int i = a; i < (int)(b); i++)
#define all(v) v.begin(), v.end()
using ll = long long;
const ll INF = 1e18;
// 変数定義
int N, M, Q, a, b, c, d, e, x, y, t, T, total, cnt, ans;
struct dsu
{
public:
  dsu() : _n(0) {}
  dsu(int n) : _n(n), parent_or_size(n, -1) {}

  int merge(int a, int b)
  {
    assert(0 <= a && a < _n);
    assert(0 <= b && b < _n);
    int x = leader(a), y = leader(b);
    if (x == y)
      return x;
    if (-parent_or_size[x] < -parent_or_size[y])
      swap(x, y);
    parent_or_size[x] += parent_or_size[y];
    parent_or_size[y] = x;
    return x;
  }

  bool same(int a, int b)
  {
    assert(0 <= a && a < _n);
    assert(0 <= b && b < _n);
    return leader(a) == leader(b);
  }

  int leader(int a)
  {
    assert(0 <= a && a < _n);
    if (parent_or_size[a] < 0)
      return a;
    return parent_or_size[a] = leader(parent_or_size[a]);
  }

  int size(int a)
  {
    assert(0 <= a && a < _n);
    return -parent_or_size[leader(a)];
  }

private:
  int _n;
  // root node: -1 * component size
  // otherwise: parent
  vector<int> parent_or_size;
};

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N >> M >> Q;
  dsu uf(N * 7);
  vector<string> S(N);
  rep(i, N)
  {
    cin >> S[i];
    rep(j, 7)
    {
      if (S[i][j] == '1' && S[i][(j + 1) % 7] == '1')
      {
        uf.merge(i * 7 + j, i * 7 + (j + 1) % 7);
        // cout << i * 7 + j << ' ' << i * 7 + (j + 1) % 7 << '\n';
      }
    }
  }
  vector<vector<int>> edge(N);
  rep(i, M)
  {
    cin >> x >> y;
    x--;
    y--;
    edge[x].push_back(y);
    edge[y].push_back(x);
    rep(j, 7)
    {
      if (S[x][j] == '1' && S[y][j] == '1')
      {
        uf.merge(x * 7 + j, y * 7 + j);
        // cout << x * 7 + j << ' ' << y * 7 + j << '\n';
      }
    }
  }
  rep(i, Q)
  {
    cin >> t >> a >> b;
    a--;
    b--;
    if (t == 1)
    {
      S[a][b] = '1';
      if (S[a][abs((b - 1) % 7)] == '1')
      {
        uf.merge(a * 7 + abs((b - 1) % 7), a * 7 + b);
        // cout << a * 7 + abs((b - 1) % 7) << ' ' << a * 7 + b << '\n';
      }
      if (S[a][(b + 1) % 7] == '1')
      {
        uf.merge(a * 7 + (b + 1) % 7, a * 7 + b);
        // cout << a * 7 + ((b + 1) % 7) << ' ' << a * 7 + b << '\n';
      }
      for (auto v : edge[a])
      {
        if (S[v][b] == '1')
        {
          uf.merge(a * 7 + b, v * 7 + b);
          // cout << a * 7 + b << ' ' << v * 7 + b << '\n';
        }
      }
    }
    else
    {
      cout << uf.size(a * 7) << '\n';
    }
  }

  return 0;
}
0