結果

問題 No.1266 7 Colors
ユーザー marroncastlemarroncastle
提出日時 2020-10-24 01:47:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 168 ms / 3,000 ms
コード長 2,852 bytes
コンパイル時間 2,235 ms
コンパイル使用メモリ 208,356 KB
実行使用メモリ 14,780 KB
最終ジャッジ日時 2023-09-28 19:59:17
合計ジャッジ時間 6,277 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 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 AC 142 ms
11,004 KB
testcase_05 AC 88 ms
6,216 KB
testcase_06 AC 160 ms
12,100 KB
testcase_07 AC 166 ms
13,152 KB
testcase_08 AC 158 ms
11,300 KB
testcase_09 AC 140 ms
9,380 KB
testcase_10 AC 128 ms
8,924 KB
testcase_11 AC 97 ms
6,748 KB
testcase_12 AC 107 ms
7,548 KB
testcase_13 AC 117 ms
8,056 KB
testcase_14 AC 87 ms
5,960 KB
testcase_15 AC 168 ms
13,344 KB
testcase_16 AC 109 ms
7,540 KB
testcase_17 AC 161 ms
13,024 KB
testcase_18 AC 70 ms
14,780 KB
testcase_19 AC 74 ms
14,332 KB
testcase_20 AC 74 ms
14,336 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)];
  }

  vector<vector<int>> groups()
  {
    vector<int> leader_buf(_n), group_size(_n);
    for (int i = 0; i < _n; i++)
    {
      leader_buf[i] = leader(i);
      group_size[leader_buf[i]]++;
    }
    vector<vector<int>> result(_n);
    for (int i = 0; i < _n; i++)
    {
      result[i].reserve(group_size[i]);
    }
    for (int i = 0; i < _n; i++)
    {
      result[leader_buf[i]].push_back(i);
    }
    result.erase(
        remove_if(result.begin(), result.end(),
                  [&](const vector<int> &v) { return v.empty(); }),
        result.end());
    return result;
  }

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);
    }
  }
  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);
    }
  }
  rep(i, Q)
  {
    cin >> t >> a >> b;
    a--;
    b--;
    if (t == 1)
    {
      S[a][b] = '1';
      if (S[a][(b + 6) % 7] == '1')
        uf.merge(a * 7 + (b + 6) % 7, a * 7 + b);
      if (S[a][(b + 1) % 7] == '1')
        uf.merge(a * 7 + (b + 1) % 7, a * 7 + b);
      for (auto v : edge[a])
      {
        if (S[v][b] == '1')
          uf.merge(a * 7 + b, v * 7 + b);
      }
    }
    else
    {
      cout << uf.size(a * 7) << '\n';
    }
  }

  return 0;
}
0