結果

問題 No.1266 7 Colors
ユーザー simansiman
提出日時 2021-03-22 18:09:42
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 355 ms / 3,000 ms
コード長 2,702 bytes
コンパイル時間 2,215 ms
コンパイル使用メモリ 103,880 KB
実行使用メモリ 21,164 KB
最終ジャッジ日時 2023-08-15 20:29:25
合計ジャッジ時間 11,682 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
16,960 KB
testcase_01 AC 20 ms
17,120 KB
testcase_02 AC 19 ms
17,060 KB
testcase_03 AC 301 ms
18,984 KB
testcase_04 AC 337 ms
19,948 KB
testcase_05 AC 301 ms
18,896 KB
testcase_06 AC 340 ms
19,996 KB
testcase_07 AC 354 ms
20,120 KB
testcase_08 AC 335 ms
19,936 KB
testcase_09 AC 321 ms
19,464 KB
testcase_10 AC 319 ms
19,316 KB
testcase_11 AC 304 ms
19,100 KB
testcase_12 AC 308 ms
19,176 KB
testcase_13 AC 311 ms
19,220 KB
testcase_14 AC 296 ms
18,852 KB
testcase_15 AC 355 ms
20,420 KB
testcase_16 AC 303 ms
19,152 KB
testcase_17 AC 351 ms
20,260 KB
testcase_18 AC 307 ms
21,164 KB
testcase_19 AC 200 ms
20,696 KB
testcase_20 AC 202 ms
20,776 KB
testcase_21 AC 254 ms
18,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const int MAX_V = 100001;
const int MAX_N = 1000000;
vector<int> parent;
vector<int> _rank;
vector<int> _size;

class UnionFind {
public:
  UnionFind(int n) {
    for (int i = 0; i < n; ++i) {
      parent.push_back(i);
      _rank.push_back(0);
      _size.push_back(1);
    }
  }

  int find(int x) {
    if (parent[x] == x) {
      return x;
    } else {
      return parent[x] = find(parent[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;

    if (_rank[x] < _rank[y]) {
      parent[x] = y;
      _size[y] += _size[x];
    } else {
      parent[y] = x;
      _size[x] += _size[y];
      if (_rank[x] == _rank[y]) ++_rank[x];
    }
  }

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

  int size(int x) {
    return _size[find(x)];
  }
};

int calcId(int v, int color) {
  return MAX_V * color + v;
}

int S[MAX_V];
vector<int> E[MAX_V];
UnionFind uf(MAX_N);

void connect(int u, int v) {
  for (int c = 0; c < 7; ++c) {
    if ((S[u] >> c & 1) == 0) continue;
    if ((S[v] >> c & 1) == 0) continue;

    int id1 = calcId(u, c);
    int id2 = calcId(v, c);

    uf.unite(id1, id2);
  }
}

int main() {
  int N, M, Q;

  cin >> N >> M >> Q;

  for (int i = 0; i < N; ++i) {
    string s;
    cin >> s;
    int mask = 0;

    for (int c = 0; c < 7; ++c) {
      if (s[c] == '0') continue;
      mask |= (1 << c);
      int id = calcId(i, c);

      int bc = (c + 6) % 7;
      int ac = (c + 1) % 7;

      if (s[bc] == '1') {
        int bid = calcId(i, bc);
        uf.unite(id, bid);
      }
      if (s[ac] == '1') {
        int aid = calcId(i, ac);
        uf.unite(id, aid);
      }
    }

    S[i] = mask;
  }

  for (int i = 0; i < M; ++i) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    E[u].push_back(v);
    E[v].push_back(u);

    connect(u, v);
  }

  int t, x, y;
  for (int i = 0; i < Q; ++i) {
    cin >> t >> x >> y;
    --x;

    if (t == 1) {
      int c = y - 1;
      int id = calcId(x, c);
      S[x] |= 1 << c;
      int bc = (c + 6) % 7;
      int ac = (c + 1) % 7;

      if (S[x] >> bc & 1) {
        int bid = calcId(x, bc);
        uf.unite(id, bid);
      }
      if (S[x] >> ac & 1) {
        int aid = calcId(x, ac);
        uf.unite(id, aid);
      }

      for (int i = 0; i < E[x].size(); ++i) {
        int z = E[x][i];
        connect(x, z);
      }
    } else {
      int id = calcId(x, 0);
      cout << uf.size(id) << endl;
    }
  }

  return 0;
}

0