結果

問題 No.1266 7 Colors
ユーザー rodearodea
提出日時 2020-11-09 17:37:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 204 ms / 3,000 ms
コード長 4,244 bytes
コンパイル時間 2,530 ms
コンパイル使用メモリ 127,748 KB
実行使用メモリ 21,176 KB
最終ジャッジ日時 2023-09-29 22:12:55
合計ジャッジ時間 7,793 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 141 ms
6,472 KB
testcase_04 AC 183 ms
15,196 KB
testcase_05 AC 146 ms
7,232 KB
testcase_06 AC 195 ms
16,452 KB
testcase_07 AC 204 ms
18,536 KB
testcase_08 AC 193 ms
15,492 KB
testcase_09 AC 166 ms
12,568 KB
testcase_10 AC 162 ms
11,668 KB
testcase_11 AC 148 ms
8,332 KB
testcase_12 AC 158 ms
9,392 KB
testcase_13 AC 167 ms
10,432 KB
testcase_14 AC 141 ms
7,008 KB
testcase_15 AC 204 ms
18,884 KB
testcase_16 AC 159 ms
9,628 KB
testcase_17 AC 198 ms
17,776 KB
testcase_18 AC 183 ms
21,176 KB
testcase_19 AC 76 ms
19,888 KB
testcase_20 AC 73 ms
20,100 KB
testcase_21 AC 149 ms
4,380 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);
    }

    vector<bool> visited(7 * n, false);
    UnionFind uf(7 * n);
    for(int i=0; i<n; i++){
        for(int j=0; j<7; j++){
            if(s[i][j] == '1' && !visited[i * 7 + j]){
                visited[i * 7 + j] = true;
                queue<int> que;
                que.emplace(i * 7 + j);

                while(que.size()){
                    int cv = que.front(); que.pop();
                    int cnum = cv / 7, crem = cv % 7;

                    int nv;
                    for(auto nxt : G[cnum]){
                        nv = nxt * 7 + crem;
                        if(s[nxt][crem] == '1' && !visited[nv]){
                            visited[nv] = true;
                            uf.unite(cv, nv);
                            que.emplace(nv);
                        }
                    }

                    nv = cnum * 7 + (crem + 1) % 7;
                    if(s[cnum][(crem + 1) % 7] == '1' && !visited[nv]){
                        visited[nv] = true;
                        uf.unite(cv, nv);
                        que.emplace(nv);
                    }

                    nv = cnum * 7 + (crem - 1 + 7) % 7;
                    if(s[cnum][(crem - 1 + 7) % 7] == '1' && !visited[nv]){
                        visited[nv] = true;
                        uf.unite(cv, nv);
                        que.emplace(nv);
                    }
                }
            }
        }
    }

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

            nv = x * 7 + (y + 1) % 7;
            if(s[x][(y + 1) % 7] == '1'){
                uf.unite(cv, nv);
            }

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

    return 0;
}
0