結果

問題 No.1266 7 Colors
ユーザー milanis48663220milanis48663220
提出日時 2020-10-23 22:38:38
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 236 ms / 3,000 ms
コード長 2,066 bytes
コンパイル時間 933 ms
コンパイル使用メモリ 88,488 KB
実行使用メモリ 14,688 KB
最終ジャッジ日時 2023-09-28 16:47:28
合計ジャッジ時間 6,272 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,772 KB
testcase_01 AC 6 ms
8,796 KB
testcase_02 AC 6 ms
8,856 KB
testcase_03 AC 179 ms
10,648 KB
testcase_04 AC 221 ms
12,748 KB
testcase_05 AC 178 ms
10,704 KB
testcase_06 AC 225 ms
12,940 KB
testcase_07 AC 234 ms
13,508 KB
testcase_08 AC 224 ms
12,700 KB
testcase_09 AC 210 ms
11,904 KB
testcase_10 AC 205 ms
11,868 KB
testcase_11 AC 182 ms
10,968 KB
testcase_12 AC 189 ms
11,168 KB
testcase_13 AC 202 ms
11,400 KB
testcase_14 AC 177 ms
10,624 KB
testcase_15 AC 236 ms
13,804 KB
testcase_16 AC 189 ms
11,144 KB
testcase_17 AC 221 ms
13,472 KB
testcase_18 AC 189 ms
14,688 KB
testcase_19 AC 71 ms
14,288 KB
testcase_20 AC 71 ms
14,492 KB
testcase_21 AC 166 ms
9,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) { }
  bool unionSet(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) {
    return root(x) == root(y);
  }
  int root(int x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  int size(int x) {
    return -data[root(x)];
  }
};

int n, m, q;
string s[100000];
vector<int> G[100000];

int to_idx(int i, int col){
    return i*7+col;
}

UnionFind uf(0);

void input(){
    cin >> n >> m >> q;
    uf = UnionFind(n*7);
    for(int i = 0; i < n; i++) cin >> s[i];
    for(int i = 0; i < m; i++){
        int u, v; cin >> u >> v; u--; v--;
        G[u].push_back(v);
        G[v].push_back(u);
        for(int col = 0; col < 7; col++){
            if(s[u][col] == '1' && s[v][col] == '1'){
                uf.unionSet(to_idx(u, col), to_idx(v, col));
            }
        }
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < 7; j++){
            if(s[i][j] == '1' && s[i][(j+1)%7] == '1'){
                uf.unionSet(to_idx(i, j), to_idx(i, (j+1)%7));
            }
        }
    }
}

void solve(){
    int t, x, y; cin >> t >> x >> y; x--; y--;
    if(t == 1){
        int nx = (y+1)%7;
        int pre = (y+6)%7;
        s[x][y] = '1';
        if(s[x][pre] == '1') uf.unionSet(to_idx(x, pre), to_idx(x, y));
        if(s[x][nx] == '1') uf.unionSet(to_idx(x, nx), to_idx(x, y));
        for(int to : G[x]){
            if(s[to][y] == '1'){
                uf.unionSet(to_idx(x, y), to_idx(to, y));
            }
        }
    }else{
        cout << uf.size(to_idx(x, 0)) << endl;
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    input();
    for(int i = 0; i < q; i++) solve();
}
0