結果

問題 No.1266 7 Colors
ユーザー SSRSSSRS
提出日時 2020-10-23 22:25:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 363 ms / 3,000 ms
コード長 1,962 bytes
コンパイル時間 1,737 ms
コンパイル使用メモリ 175,000 KB
実行使用メモリ 18,524 KB
最終ジャッジ日時 2023-09-28 16:24:25
合計ジャッジ時間 9,329 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 265 ms
5,944 KB
testcase_04 AC 340 ms
13,236 KB
testcase_05 AC 267 ms
6,452 KB
testcase_06 AC 352 ms
14,144 KB
testcase_07 AC 361 ms
15,600 KB
testcase_08 AC 350 ms
13,340 KB
testcase_09 AC 326 ms
11,204 KB
testcase_10 AC 322 ms
10,192 KB
testcase_11 AC 289 ms
7,532 KB
testcase_12 AC 291 ms
8,292 KB
testcase_13 AC 304 ms
9,064 KB
testcase_14 AC 268 ms
6,440 KB
testcase_15 AC 363 ms
15,996 KB
testcase_16 AC 299 ms
8,580 KB
testcase_17 AC 355 ms
15,352 KB
testcase_18 AC 278 ms
18,524 KB
testcase_19 AC 167 ms
18,152 KB
testcase_20 AC 166 ms
18,100 KB
testcase_21 AC 222 ms
4,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct unionfind{
  vector<int> p, sz;
  unionfind(int N){
    p = vector<int>(N, -1);
    sz = vector<int>(N, 1);
  }
  int root(int x){
    if (p[x] == -1){
      return x;
    } else {
      p[x] = root(p[x]);
      return p[x];
    }
  }
  bool same(int x, int y){
    return root(x) == root(y);
  }
  void unite(int x, int y){
    x = root(x);
    y = root(y);
    if (x != y){
      p[x] = y;
      sz[y] += sz[x];
    }
  }
  int size(int x){
    return sz[root(x)];
  }
};
int next(int x){
  return (x + 1) % 7;
}
int prev(int x){
  return (x + 6) % 7;
}
int main(){
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<string> s(N);
  for (int i = 0; i < N; i++){
    cin >> s[i];
  }
  vector<vector<int>> E(N);
  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);
  }
  unionfind UF(N * 7);
  for (int i = 0; i < N; i++){
    for (int j = 0; j < 7; j++){
      if (s[i][j] == '1' && s[i][next(j)] == '1'){
        int u = i * 7 + j;
        int v = i * 7 + next(j);
        UF.unite(u, v);
      }
    }
  }
  for (int i = 0; i < N; i++){
    for (int j : E[i]){
      for (int k = 0; k < 7; k++){
        if (s[i][k] == '1' && s[j][k] == '1'){
          UF.unite(i * 7 + k, j * 7 + k);
        }
      }
    }
  }
  for (int i = 0; i < Q; i++){
    int t, x, y;
    cin >> t >> x >> y;
    if (t == 1){
      x--;
      y--;
      s[x][y] = '1';
      if (s[x][prev(y)] == '1'){
        int u = x * 7 + prev(y);
        int v = x * 7 + y;
        UF.unite(u, v);
      }
      if (s[x][next(y)] == '1'){
        int u = x * 7 + y;
        int v = x * 7 + next(y);
        UF.unite(u, v);
      }
      for (int j : E[x]){
        if (s[j][y] == '1'){
          int u = x * 7 + y;
          int v = j * 7 + y;
          UF.unite(u, v);
        }
      }
    }
    if (t == 2){
      x--;
      cout << UF.size(x * 7) << endl;
    }
  }
}
0