結果

問題 No.1266 7 Colors
ユーザー iqueue02iqueue02
提出日時 2020-10-24 15:23:31
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 325 ms / 3,000 ms
コード長 1,845 bytes
コンパイル時間 2,232 ms
コンパイル使用メモリ 207,312 KB
実行使用メモリ 14,676 KB
最終ジャッジ日時 2023-09-28 20:28:30
合計ジャッジ時間 9,189 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 237 ms
5,620 KB
testcase_04 AC 300 ms
10,896 KB
testcase_05 AC 244 ms
6,240 KB
testcase_06 AC 311 ms
12,052 KB
testcase_07 AC 325 ms
13,256 KB
testcase_08 AC 302 ms
11,160 KB
testcase_09 AC 281 ms
9,316 KB
testcase_10 AC 277 ms
8,860 KB
testcase_11 AC 252 ms
6,724 KB
testcase_12 AC 263 ms
7,540 KB
testcase_13 AC 269 ms
8,148 KB
testcase_14 AC 243 ms
6,032 KB
testcase_15 AC 322 ms
13,628 KB
testcase_16 AC 264 ms
7,520 KB
testcase_17 AC 317 ms
12,724 KB
testcase_18 AC 279 ms
14,676 KB
testcase_19 AC 165 ms
14,316 KB
testcase_20 AC 166 ms
14,340 KB
testcase_21 AC 223 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); i++)
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;

struct UnionFind{
  vector<int> data;
  int __size;
  UnionFind(int sz) {
  data.assign(sz, -1);
  __size = sz;
  }
 
  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if(x == y) return (false);
    if(data[x] > data[y]) swap(x, y);//親は負でサイズを保存
    __size--;
    data[x] += data[y];
    data[y] = x;
    return (true);
  }
 
  int find(int k) {
    if(data[k] < 0) return (k);
    return (data[k] = find(data[k]));
  }

  bool same(int x, int y){
    return find(x) == find(y);
  }
 
  int size(int k) {
    return (-data[find(k)]);
  }

  int union_count() {
    return (__size);
  }
};

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<string> s(n);
  vector<vector<int>> g(n);
  rep(i,n) cin >> s[i];
  rep(i,m) {
    int u, v;
    cin >> u >> v;
    u--; v--;
    g[u].emplace_back(v);
    g[v].emplace_back(u);
  }
  UnionFind uf(n * 7);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 7; j++) {
      if (s[i][j] == '0') continue;
      for (auto k : g[i]) if (s[k][j] == '1') uf.unite(i * 7 + j, k * 7 + j);
      
      int a = (j + 1) % 7, b = (j - 1 + 7) % 7;
      if (s[i][a] == '1') uf.unite(i * 7 + j, i * 7 + a);
      if (s[i][b] == '1') uf.unite(i * 7 + j, i * 7 + b);
    }
  }

  while (q--) {
    int t, u, x;
    cin >> t >> u >> x;
    u--; x--;
    if (t == 1) {
      s[u][x] = '1';
      for (auto v : g[u]) if (s[v][x] == '1') uf.unite(u * 7 + x, v * 7 + x);
      
      int a = (x + 1) % 7, b = (x - 1 + 7) % 7;
      if (s[u][a] == '1') uf.unite(u * 7 + x, u * 7 + a);
      if (s[u][b] == '1') uf.unite(u * 7 + x, u * 7 + b);
    } else {
      cout << uf.size(u * 7) << endl;
    }
  }
  return 0;
}
0