結果

問題 No.1266 7 Colors
ユーザー simansiman
提出日時 2021-03-22 18:09:42
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 366 ms / 3,000 ms
コード長 2,702 bytes
コンパイル時間 2,290 ms
コンパイル使用メモリ 142,260 KB
実行使用メモリ 21,252 KB
最終ジャッジ日時 2024-05-03 07:08:37
合計ジャッジ時間 10,825 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
17,360 KB
testcase_01 AC 21 ms
17,416 KB
testcase_02 AC 21 ms
17,288 KB
testcase_03 AC 306 ms
18,896 KB
testcase_04 AC 349 ms
19,840 KB
testcase_05 AC 300 ms
18,760 KB
testcase_06 AC 346 ms
19,952 KB
testcase_07 AC 365 ms
20,324 KB
testcase_08 AC 343 ms
19,844 KB
testcase_09 AC 341 ms
19,556 KB
testcase_10 AC 327 ms
19,464 KB
testcase_11 AC 313 ms
19,020 KB
testcase_12 AC 312 ms
19,200 KB
testcase_13 AC 320 ms
19,336 KB
testcase_14 AC 301 ms
18,948 KB
testcase_15 AC 366 ms
20,356 KB
testcase_16 AC 320 ms
19,128 KB
testcase_17 AC 358 ms
20,228 KB
testcase_18 AC 306 ms
21,252 KB
testcase_19 AC 199 ms
20,612 KB
testcase_20 AC 203 ms
20,616 KB
testcase_21 AC 260 ms
18,056 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