結果

問題 No.1266 7 Colors
ユーザー ShibuyapShibuyap
提出日時 2020-10-23 23:07:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 349 ms / 3,000 ms
コード長 2,346 bytes
コンパイル時間 2,075 ms
コンパイル使用メモリ 203,940 KB
実行使用メモリ 45,056 KB
最終ジャッジ日時 2023-09-28 17:47:52
合計ジャッジ時間 9,365 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
38,492 KB
testcase_01 AC 15 ms
38,612 KB
testcase_02 AC 14 ms
38,516 KB
testcase_03 AC 260 ms
40,440 KB
testcase_04 AC 312 ms
42,968 KB
testcase_05 AC 264 ms
40,608 KB
testcase_06 AC 322 ms
43,360 KB
testcase_07 AC 329 ms
44,044 KB
testcase_08 AC 318 ms
43,248 KB
testcase_09 AC 295 ms
42,352 KB
testcase_10 AC 301 ms
41,928 KB
testcase_11 AC 269 ms
41,132 KB
testcase_12 AC 277 ms
41,308 KB
testcase_13 AC 284 ms
41,572 KB
testcase_14 AC 264 ms
40,724 KB
testcase_15 AC 349 ms
44,236 KB
testcase_16 AC 276 ms
41,356 KB
testcase_17 AC 334 ms
43,832 KB
testcase_18 AC 280 ms
45,056 KB
testcase_19 AC 170 ms
44,816 KB
testcase_20 AC 172 ms
44,908 KB
testcase_21 AC 240 ms
39,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}

const int MAX_N = 1000100;
int par[MAX_N]; // 親
int rank_[MAX_N]; // 木の深さ
int cnt_[MAX_N]; // 属する頂点の個数(親のみ正しい)

// n要素で初期化
void UFinit(){
    for(int i=0;i<MAX_N;i++){
        par[i] = i;
        rank_[i] = 0;
        cnt_[i] = 1;
    }
}

// 木の根を求める
int find(int x){
    if(par[x] == x){
        return x;
    }else{
        return par[x] = find(par[x]);
    }
}

// xとyの属する集合を併合
void unite(int x, int y){
    x = find(x);
    y = find(y);
    if(x == y) return;

    if(rank_[x] < rank_[y]){
        par[x] = y;
        cnt_[y] += cnt_[x];
    }else{
        par[y] = x;
        cnt_[x] += cnt_[y];
        if(rank_[x] == rank_[y]) rank_[x]++;
    }
}

// xとyが同じ集合に属するか否か
bool same(int x, int y){
    return find(x) == find(y);
}

vector<int> G[MAX_N];

int main() {
    UFinit();
    int n, m, q;
    cin >> n >> m >> q;
    string s[n];
    rep(i,n) cin >> s[i];
    rep(i,m){
        int u, v;
        cin >> u >> v;
        u--; v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }

    rep(i,n){
        rep(j,7){
            if(s[i][j]=='1'&&s[i][(j+1)%7]=='1'){
                unite(i*7+j,i*7+(j+1)%7);
            }
        }
        rep(jj,G[i].size()){
            int j = G[i][jj];
            rep(k,7){
                if(s[i][k]=='1'&&s[j][k]=='1'){
                    unite(i*7+k,j*7+k);
                }
            }
        }
    }

    rep(i,q){
        int t, x, y;
        cin >> t >> x >> y;
        if(t == 1){
            x--; y--;
            s[x][y] = '1';
            rep(j,7){
                if(s[x][j]=='1'&&s[x][(j+1)%7]=='1'){
                    unite(x*7+j,x*7+(j+1)%7);
                }
            }
            rep(jj,G[x].size()){
                int j = G[x][jj];
                if(s[x][y]=='1'&&s[j][y]=='1'){
                    unite(x*7+y,j*7+y);
                }
            }
        }else{
            x--;
            cout << cnt_[find(x*7)] << endl;
        }
    }

    return 0;
}

0