結果

問題 No.1266 7 Colors
ユーザー totori_nyaatotori_nyaa
提出日時 2020-10-23 22:50:57
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 175 ms / 3,000 ms
コード長 2,388 bytes
コンパイル時間 2,344 ms
コンパイル使用メモリ 205,032 KB
実行使用メモリ 14,820 KB
最終ジャッジ日時 2023-09-28 17:18:05
合計ジャッジ時間 6,505 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,212 KB
testcase_01 AC 3 ms
5,320 KB
testcase_02 AC 3 ms
5,312 KB
testcase_03 AC 80 ms
7,808 KB
testcase_04 AC 146 ms
11,776 KB
testcase_05 AC 85 ms
8,060 KB
testcase_06 AC 151 ms
12,500 KB
testcase_07 AC 159 ms
13,284 KB
testcase_08 AC 152 ms
12,048 KB
testcase_09 AC 132 ms
10,720 KB
testcase_10 AC 128 ms
10,184 KB
testcase_11 AC 99 ms
8,592 KB
testcase_12 AC 107 ms
9,196 KB
testcase_13 AC 116 ms
9,656 KB
testcase_14 AC 88 ms
8,076 KB
testcase_15 AC 175 ms
13,688 KB
testcase_16 AC 111 ms
9,124 KB
testcase_17 AC 163 ms
13,504 KB
testcase_18 AC 69 ms
14,820 KB
testcase_19 AC 72 ms
14,344 KB
testcase_20 AC 72 ms
14,344 KB
testcase_21 AC 43 ms
6,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
#define endl '\n'
#define all(x) (x).begin(),(x).end()
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}
int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0};
long double eps = 1e-9;
long double pi = acos(-1);


class UnionFind{
    public:
    //親の番号を格納する。親だった場合は-(その集合のサイズ)
    vector<int> parent;

    UnionFind(int N){
        parent = vector<int>(N,-1);
    }

    int root(int A){
        if(parent[A] < 0) return A;
        return parent[A]=root(parent[A]);
    }

    int size(int A){
        return -parent[root(A)];
    }

    bool unite(int A, int B) {
        A = root(A), B = root(B);
        if(A == B) return false; 

        if(size(A) < size(B)) swap(A,B);
        parent[A] += parent[B];
        parent[B] = A;
        return true;
    }

    bool same(int A, int B){
        return root(A)==root(B);
    } 
};

vector<vector<int>> v(100010);

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);

    int n,m,q;
    cin>>n>>m>>q;
    int nn = n*7;
    UnionFind uni(nn);
    string s[n];
    for(int i=0;i<n;i++){
        cin>>s[i];
        for(int j=0;j<7;j++){
            if(s[i][j] == '1' && s[i][(j+6)%7] == '1'){
                uni.unite(j+i*7,(j+6)%7+i*7);
            }
        }
    }
    for(int i=0;i<m;i++){
        int a,b;
        cin>>a>>b;
        a--,b--;
        v[a].push_back(b);
        v[b].push_back(a);
        for(int j=0;j<7;j++){
            if(s[a][j] == s[b][j] && s[a][j] == '1'){
                uni.unite(j+a*7, j+b*7);
            }
        }
    }
    while(q--){
        int t,x,y;
        cin>>t>>x>>y;
        y--;
        x--;
        if(t == 1){
            s[x][y] = '1';
            int ny;
            ny = (y+1)%7;
            if(s[x][ny]=='1')uni.unite(ny+x*7,y+x*7);
            ny = (y+6)%7;
            if(s[x][ny]=='1')uni.unite(ny+x*7,y+x*7);
            for(auto i:v[x]){
                if(s[i][y] == '1')uni.unite(y+x*7,y+i*7);
            }
        }
        else{
            cout << uni.size(0+x*7) << endl;
        }
    }


}
0