結果

問題 No.1266 7 Colors
ユーザー kk
提出日時 2020-10-30 18:44:10
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,094 bytes
コンパイル時間 2,136 ms
コンパイル使用メモリ 206,496 KB
実行使用メモリ 20,212 KB
最終ジャッジ日時 2023-09-29 04:28:51
合計ジャッジ時間 7,828 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 198 ms
20,212 KB
testcase_19 AC 75 ms
19,820 KB
testcase_20 AC 75 ms
19,868 KB
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

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) {
      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