結果

問題 No.1266 7 Colors
ユーザー やむなくやむなく
提出日時 2020-10-23 22:19:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 339 ms / 3,000 ms
コード長 4,092 bytes
コンパイル時間 1,857 ms
コンパイル使用メモリ 174,892 KB
実行使用メモリ 14,780 KB
最終ジャッジ日時 2023-09-28 16:12:08
合計ジャッジ時間 8,927 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 252 ms
5,764 KB
testcase_04 AC 313 ms
11,044 KB
testcase_05 AC 256 ms
6,288 KB
testcase_06 AC 325 ms
12,092 KB
testcase_07 AC 331 ms
13,292 KB
testcase_08 AC 316 ms
11,300 KB
testcase_09 AC 302 ms
9,396 KB
testcase_10 AC 292 ms
8,864 KB
testcase_11 AC 271 ms
6,880 KB
testcase_12 AC 281 ms
7,668 KB
testcase_13 AC 284 ms
8,212 KB
testcase_14 AC 255 ms
6,020 KB
testcase_15 AC 339 ms
13,292 KB
testcase_16 AC 275 ms
7,492 KB
testcase_17 AC 336 ms
12,776 KB
testcase_18 AC 276 ms
14,780 KB
testcase_19 AC 168 ms
14,292 KB
testcase_20 AC 167 ms
14,344 KB
testcase_21 AC 221 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//
// Created by yamunaku on 2020/10/23.
//

#include <bits/stdc++.h>
//#include <atcoder/all>

using namespace std;
//using namespace atcoder;

#define rep(i, n) for(int i = 0; i < (n); i++)
#define repl(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perl(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD9 998244353
#define MOD1 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)
#define CST(x) cout<<fixed<<setprecision(x)

using ll = long long;
using ld = long double;
using vi = vector<int>;
using mti = vector<vector<int>>;
using vl = vector<ll>;
using mtl = vector<vector<ll>>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
template<typename T>
using heap = priority_queue<T, vector<T>, function<bool(const T, const T)>>;

struct dsu {
public:
    dsu() : _n(0) {}

    dsu(int n) : _n(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
                std::remove_if(result.begin(), result.end(),
                               [&](const std::vector<int> &v) { return v.empty(); }),
                result.end());
        return result;
    }

private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
};

int main() {
    //CFS;
    int n, m, q;
    cin >> n >> m >> q;
    vector<string> s(n);
    rep(i, n) cin >> s[i];
    mti e(n);
    rep(i, m) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        e[u].push_back(v);
        e[v].push_back(u);
    }
    dsu uf(n * 7);
    rep(i, n) {
        rep(j, 6) {
            if (s[i][j] == '1' && s[i][(j + 1) % 7] == '1') {
                uf.merge(7 * i + j, 7 * i + j + 1);
            }
        }
        if (s[i][0] == '1' && s[i][6] == '1') {
            uf.merge(7 * i, 7 * i + 6);
        }
        for (auto &x : e[i]) {
            rep(j, 7) {
                if (s[i][j] == '1' && s[x][j] == '1') {
                    uf.merge(7 * i + j, 7 * x + j);
                }
            }
        }
    }
    rep(i, q) {
        int c, x, y;
        cin >> c >> x >> y;
        x--, y--;
        if (c == 1) {
            s[x][y] = '1';
            int k = (y + 1) % 7;
            if (s[x][k] == '1') {
                uf.merge(7 * x + y, 7 * x + k);
            }
            k = (y + 6) % 7;
            if (s[x][k] == '1') {
                uf.merge(7 * x + y, 7 * x + k);
            }
            for (auto &nx : e[x]) {
                if (s[x][y] == '1' && s[nx][y] == '1') {
                    uf.merge(7 * x + y, 7 * nx + y);
                }
            }
        } else {
            cout << uf.size(7 * x) << endl;
        }
    }
    return 0;
}
0