結果

問題 No.1266 7 Colors
ユーザー naribownaribow
提出日時 2020-10-24 00:42:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 328 ms / 3,000 ms
コード長 3,657 bytes
コンパイル時間 2,338 ms
コンパイル使用メモリ 209,904 KB
実行使用メモリ 19,896 KB
最終ジャッジ日時 2023-09-28 19:47:15
合計ジャッジ時間 9,037 ms
ジャッジサーバーID
(参考情報)
judge15 / 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 240 ms
6,156 KB
testcase_04 AC 304 ms
14,640 KB
testcase_05 AC 248 ms
7,064 KB
testcase_06 AC 310 ms
16,164 KB
testcase_07 AC 325 ms
17,896 KB
testcase_08 AC 307 ms
15,052 KB
testcase_09 AC 291 ms
12,212 KB
testcase_10 AC 287 ms
11,104 KB
testcase_11 AC 260 ms
8,008 KB
testcase_12 AC 269 ms
9,068 KB
testcase_13 AC 272 ms
9,964 KB
testcase_14 AC 251 ms
7,136 KB
testcase_15 AC 328 ms
18,156 KB
testcase_16 AC 271 ms
9,384 KB
testcase_17 AC 318 ms
17,356 KB
testcase_18 AC 269 ms
19,896 KB
testcase_19 AC 162 ms
19,412 KB
testcase_20 AC 160 ms
19,724 KB
testcase_21 AC 215 ms
4,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
typedef unsigned long long ull;
typedef long double ldouble;
const ll INF = 1e18;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
template <class T>
inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}

// union by size + path having
class UnionFind {
   public:
    //素集合の要素の親(木構造の根)を配列*3で保持している.初期状態では頂点間の辺は存在せず,孤立した状態のため,自分自身を根として初期化している(par:
    //parent の略).
    vector<ll> par;

    // 各集合(木)の大きさ.根をたどるとその木の大きさが返されるように実装されている.この配列を用意しておくとある頂点の連結成分の大きさを簡単に求められる
    vector<ll> siz;

    // Constructor
    UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
        for (ll i = 0; i < sz_; ++i) par[i] = i;  // 初期では親は自分自身
    }
    void init(ll sz_) {
        par.resize(sz_);
        siz.assign(sz_, 1LL);  // resize だとなぜか初期化されなかった
        for (ll i = 0; i < sz_; ++i) par[i] = i;  // 初期では親は自分自身
    }

    // Member Function
    // Find
    ll root(ll x) {  // 根の検索
        while (par[x] != x) {
            x = par[x] = par[par[x]];  // x の親の親を x の親とする
        }
        return x;
    }

    // Union(Unite, Merge)
    bool merge(ll x, ll y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        // merge technique(データ構造をマージするテク.小を大にくっつける)
        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

    bool issame(ll x, ll y) {  // 連結判定
        return root(x) == root(y);
    }

    ll size(ll x) {  // 素集合のサイズ
        return siz[root(x)];
    }
};

typedef pair<int, int> P;

int main() {
    int n, m, q;
    cin >> n >> m >> q;
    vector<bool> s(n*7);
    vector<vector<int> > G(n);

    UnionFind uf(n*7);

    rep(i, n) {
        string str;
        cin >> str;
        rep(j, 7) {
            if(str[j] == '1') s[i*7+j] = true;
        }
        rep(j, 7) {
            if(s[i*7+j] && s[i*7+(j+1)%7]) {
                uf.merge(i*7+j, i*7+(j+1)%7);
            }
        }
    }

    rep(i, m) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
        rep(j, 7) {
            if(s[u*7+j] && s[v*7+j]) {
                uf.merge(u*7+j, v*7+j);
            }
        }
    }

    rep(i, q) {
        int query;
        cin >> query;
        int x, y;
        cin >> x >> y;
        if(query == 1) {
            // 都市xに色y
            y--;
            x--;
            s[x*7+y] = true;
            rep(j, G[x].size()) {
                int z = G[x][j];
                if(s[z*7 + y]) {
                    uf.merge(z*7 + y, x*7 + y);
                }
            }
            if(s[x*7 + (y-1+7)%7]) {
                uf.merge(x * 7 + (y - 1 + 7) % 7, x * 7 + y);
            }
            if(s[x*7 + (y+1)%7]) {
                uf.merge(x*7 + (y+1)%7, x*7 + y);
            }
        }
        else {
            x--;
            cout << uf.size(x*7) << endl;
        }
    }
}
0