結果

問題 No.1266 7 Colors
ユーザー kk
提出日時 2020-10-30 19:20:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 243 ms / 3,000 ms
コード長 2,112 bytes
コンパイル時間 2,166 ms
コンパイル使用メモリ 208,000 KB
実行使用メモリ 20,300 KB
最終ジャッジ日時 2023-09-29 04:30:23
合計ジャッジ時間 7,727 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 171 ms
6,484 KB
testcase_04 AC 238 ms
14,868 KB
testcase_05 AC 175 ms
7,012 KB
testcase_06 AC 233 ms
16,104 KB
testcase_07 AC 243 ms
18,248 KB
testcase_08 AC 220 ms
15,112 KB
testcase_09 AC 218 ms
12,296 KB
testcase_10 AC 198 ms
11,660 KB
testcase_11 AC 175 ms
8,140 KB
testcase_12 AC 189 ms
9,068 KB
testcase_13 AC 185 ms
10,164 KB
testcase_14 AC 167 ms
7,000 KB
testcase_15 AC 237 ms
18,552 KB
testcase_16 AC 178 ms
9,376 KB
testcase_17 AC 230 ms
17,556 KB
testcase_18 AC 195 ms
20,300 KB
testcase_19 AC 77 ms
19,872 KB
testcase_20 AC 77 ms
19,964 KB
testcase_21 AC 163 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,b,e) for (int i=(int)(b); i<(int)(e); i++)
#define ALL(x) (x).begin(), (x).end()

const double PI = acos(-1);

class UFT {
  int n;
  int cnt;           // number of connected components
  vector<int> par;
  vector<int> rank;
  vector<int> sz;    // size of each component
public:
  UFT(int n) : n(n), cnt(n), par(n), rank(n), sz(n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      sz[i] = 1;
    }
  }
  
  int find(int x) {
    return par[x] == x ? x : par[x] = find(par[x]);
  }
  
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    
    --cnt;
    if (rank[x] < rank[y]) {
      par[x] = y;
      sz[y] += sz[x];
    } else {
      par[y] = x;
      sz[x] += sz[y];
      if (rank[x] == rank[y])
        ++rank[x];
    }
    
  }
  
  bool same(int x, int y) {
    return find(x) == find(y);
  }
  
  int compCnt() {
    return cnt;
  }
  
  int size(int x) {
    return sz[find(x)];
  }
};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, m, q;
  cin >> n >> m >> q;

  UFT tree(7 * n);
  vector<string> color(n);

  REP (i, n) {
    cin >> color[i];
    REP (j, 7) {
      int k = (j + 1) % 7;
      if (color[i][j] == '1' && color[i][k] == '1') {
        tree.unite(7*i+j, 7*i+k);
      }
    }
  }

  vector<vector<int> > edges(n);
  REP (i, m) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    edges[u].push_back(v);
    edges[v].push_back(u);
    REP (j, 7) {
      if (color[u][j] == '1' && color[v][j] == '1') {
        tree.unite(7*u+j, 7*v+j);
      }
    }
  }
  
  REP (_, q) {
    int t, x, y;
    cin >> t >> x >> y;
    --x, --y;
    if (t == 1) {
      color[x][y] = '1';
      if (color[x][(y+1)%7] == '1')
        tree.unite(7*x+y, 7*x+(y+1)%7);
      if (color[x][(y+6)%7] == '1')
        tree.unite(7*x+y, 7*x+(y+6)%7);
      for (int v: edges[x]) {
        if (color[v][y] == '1')
          tree.unite(7*x+y, 7*v+y);
      }
    } else {
      cout << tree.size(7*x) << endl;
    }
  }
  
  return 0;
}
0