結果

問題 No.1266 7 Colors
ユーザー ShibuyapShibuyap
提出日時 2020-10-23 23:07:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 354 ms / 3,000 ms
コード長 2,346 bytes
コンパイル時間 2,383 ms
コンパイル使用メモリ 206,940 KB
実行使用メモリ 45,144 KB
最終ジャッジ日時 2024-07-21 12:26:52
合計ジャッジ時間 9,953 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
38,684 KB
testcase_01 AC 15 ms
38,688 KB
testcase_02 AC 15 ms
38,812 KB
testcase_03 AC 269 ms
40,604 KB
testcase_04 AC 333 ms
43,160 KB
testcase_05 AC 275 ms
40,856 KB
testcase_06 AC 341 ms
43,548 KB
testcase_07 AC 349 ms
44,288 KB
testcase_08 AC 333 ms
43,164 KB
testcase_09 AC 314 ms
42,396 KB
testcase_10 AC 307 ms
42,140 KB
testcase_11 AC 282 ms
41,244 KB
testcase_12 AC 298 ms
41,244 KB
testcase_13 AC 302 ms
41,756 KB
testcase_14 AC 277 ms
40,732 KB
testcase_15 AC 354 ms
44,192 KB
testcase_16 AC 294 ms
41,504 KB
testcase_17 AC 354 ms
43,808 KB
testcase_18 AC 281 ms
45,144 KB
testcase_19 AC 179 ms
44,956 KB
testcase_20 AC 179 ms
44,832 KB
testcase_21 AC 243 ms
39,580 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