結果

問題 No.1266 7 Colors
ユーザー naribownaribow
提出日時 2020-10-24 00:37:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,640 bytes
コンパイル時間 2,312 ms
コンパイル使用メモリ 208,792 KB
実行使用メモリ 19,916 KB
最終ジャッジ日時 2023-09-28 19:45:13
合計ジャッジ時間 9,205 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 RE -
testcase_02 RE -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 270 ms
19,916 KB
testcase_19 AC 162 ms
19,716 KB
testcase_20 AC 163 ms
19,484 KB
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

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 {
            cout << uf.size(x*7) << endl;
        }
    }
}
0