結果

問題 No.1266 7 Colors
ユーザー rodearodea
提出日時 2020-11-09 18:12:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 198 ms / 3,000 ms
コード長 3,300 bytes
コンパイル時間 1,488 ms
コンパイル使用メモリ 121,408 KB
実行使用メモリ 20,132 KB
最終ジャッジ日時 2023-09-29 22:13:39
合計ジャッジ時間 6,931 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 158 ms
6,520 KB
testcase_04 AC 188 ms
14,936 KB
testcase_05 AC 159 ms
7,064 KB
testcase_06 AC 189 ms
16,180 KB
testcase_07 AC 196 ms
18,344 KB
testcase_08 AC 188 ms
15,244 KB
testcase_09 AC 177 ms
12,304 KB
testcase_10 AC 169 ms
11,548 KB
testcase_11 AC 161 ms
8,116 KB
testcase_12 AC 165 ms
9,104 KB
testcase_13 AC 169 ms
10,136 KB
testcase_14 AC 164 ms
6,984 KB
testcase_15 AC 195 ms
18,540 KB
testcase_16 AC 172 ms
9,368 KB
testcase_17 AC 196 ms
17,772 KB
testcase_18 AC 198 ms
20,132 KB
testcase_19 AC 77 ms
19,868 KB
testcase_20 AC 76 ms
19,780 KB
testcase_21 AC 163 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;

using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;

template <class T> inline T chmax(T &a, const T b) {return a = (a < b) ? b : a;}
template <class T> inline T chmin(T &a, const T b) {return a = (a > b) ? b : a;}

constexpr int MOD = 1e9 + 7;
constexpr int inf = 1e9;
constexpr long long INF = 1e18;

#define all(a) (a).begin(), (a).end()

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

class UnionFind{
public:
    vector<int> par;
    vector<int> rank;
    vector<int> sz;
    int uf_size;

    UnionFind(int n){
        par.resize(n);
        rank.resize(n);
        sz.resize(n);
        uf_size = n;
        for(int i=0; i<n; i++){
            par[i] = i;
            rank[i] = 0;
            sz[i] = 1;
        }
    }

    int find(int x){
        if(par[x] == x) return x;
        else return par[x] = find(par[x]);
    }
    
    bool same(int x, int y){
        return find(x) == find(y);
    }

    bool unite(int x, int y){
        x = find(x);
        y = find(y);
        if(x == y) return false;
        if(rank[x] < rank[y]) swap(x, y);
        if(rank[x] == rank[y]) rank[x]++;
        uf_size--;
        par[y] = x;
        sz[x] = sz[x] + sz[y];
        return true;
    }

    int size(int x){
        return sz[find(x)];
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n, m, q; cin>>n>>m>>q;
    vector<string> s(n);
    for(int i=0; i<n; i++) cin>>s[i];

    vector<vector<int>> G(n);
    for(int i=0; i<m; i++){
        int u, v; cin>>u>>v;
        u--, v--;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    UnionFind uf(n * 7);
    for(int i=0; i<n; i++){
        for(int j=0; j<7; j++){
            if(s[i][j] == '0') continue;

            for(auto nv : G[i]){
                if(s[nv][j] == '1'){
                    uf.unite(i * 7 + j, nv * 7 + j);
                }
            }

            int up = (j + 1) % 7, down = (j - 1 + 7) % 7;
            if(s[i][up] == '1'){
                uf.unite(i * 7 + j, i * 7 + up);
            }
            if(s[i][down] == '1'){
                uf.unite(i * 7 + j, i * 7 + down);
            }
        }
    }

    while(q--){
        int z, x, y; cin>>z>>x>>y;
        x--, y--;
        if(z == 1){
            s[x][y] = '1';
            for(auto nv : G[x]){
                if(s[nv][y] == '1'){
                    uf.unite(x * 7 + y, nv * 7 + y);
                }
            }

            int up = (y + 1) % 7, down = (y - 1 + 7) % 7;
            if(s[x][up] == '1'){
                uf.unite(x * 7 + y, x * 7 + up);
            }
            if(s[x][down] == '1'){
                uf.unite(x * 7 + y, x * 7 + down);
            }
        }
        else{
            cout << uf.size(x * 7) << endl;
        }
    }

    return 0;
}
0