結果

問題 No.1266 7 Colors
ユーザー Fu_LFu_L
提出日時 2024-03-02 14:22:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 151 ms / 3,000 ms
コード長 4,238 bytes
コンパイル時間 2,772 ms
コンパイル使用メモリ 215,032 KB
実行使用メモリ 16,268 KB
最終ジャッジ日時 2024-03-02 14:22:56
合計ジャッジ時間 8,542 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 1 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 88 ms
9,856 KB
testcase_04 AC 130 ms
14,336 KB
testcase_05 AC 90 ms
9,856 KB
testcase_06 AC 138 ms
15,104 KB
testcase_07 AC 151 ms
16,128 KB
testcase_08 AC 132 ms
14,464 KB
testcase_09 AC 116 ms
12,928 KB
testcase_10 AC 109 ms
12,416 KB
testcase_11 AC 95 ms
10,752 KB
testcase_12 AC 101 ms
11,136 KB
testcase_13 AC 111 ms
11,648 KB
testcase_14 AC 88 ms
9,856 KB
testcase_15 AC 143 ms
16,236 KB
testcase_16 AC 124 ms
11,264 KB
testcase_17 AC 141 ms
15,872 KB
testcase_18 AC 72 ms
16,244 KB
testcase_19 AC 75 ms
16,268 KB
testcase_20 AC 75 ms
16,268 KB
testcase_21 AC 45 ms
7,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, a, b) for(ll i = a; i < b; ++i)
#define rrep(i, a, b) for(ll i = a; i >= b; --i)
constexpr ll inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
template <typename T = int>
struct Edge {
    int from, to;
    T cost;
    int idx;
    Edge()
        : from(-1), to(-1), cost(-1), idx(-1) {}
    Edge(int from, int to, T cost = 1, int idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}
    operator int() const {
        return to;
    }
};
template <typename T = int>
struct Graph {
    Graph(int N)
        : n(N), es(0), g(N) {}
    int size() const {
        return n;
    }
    int edge_size() const {
        return es;
    }
    void add_edge(int from, int to, T cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }
    void add_directed_edge(int from, int to, T cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es++);
    }
    inline vector<Edge<T>>& operator[](const int& k) {
        assert(0 <= k and k < n);
        return g[k];
    }
    inline const vector<Edge<T>>& operator[](const int& k) const {
        assert(0 <= k and k < n);
        return g[k];
    }

   private:
    int n, es;
    vector<vector<Edge<T>>> g;
};
template <typename T = int>
using Edges = vector<Edge<T>>;
struct UnionFind {
    UnionFind(int N)
        : n(N), data(N, -1) {}
    int merge(int a, int b) {
        assert(0 <= a and a < n);
        assert(0 <= b and b < n);
        int x = leader(a), y = leader(b);
        if(x == y) return x;
        if(-data[x] < -data[y]) swap(x, y);
        data[x] += data[y];
        data[y] = x;
        return x;
    }
    bool same(int a, int b) {
        assert(0 <= a and a < n);
        assert(0 <= b and b < n);
        return leader(a) == leader(b);
    }
    int leader(int a) {
        assert(0 <= a and a < n);
        if(data[a] < 0) return a;
        return data[a] = leader(data[a]);
    }
    int size(int a) {
        assert(0 <= a and a < n);
        return -data[leader(a)];
    }
    vector<vector<int>> groups() {
        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]];
        }
        vector<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(remove_if(result.begin(), result.end(), [&](const vector<int>& v) { return v.empty(); }), result.end());
        return result;
    }

   private:
    int n;
    vector<int> data;
};
int main(void) {
    int n, m, q;
    cin >> n >> m >> q;
    UnionFind uf(7 * n);
    vector<string> s(n);
    rep(i, 0, n) {
        cin >> s[i];
        rep(j, 0, 7) {
            if(s[i][j] == '1' and s[i][(j + 1) % 7] == '1') {
                uf.merge(7 * i + j, 7 * i + (j + 1) % 7);
            }
        }
    }
    Graph<int> g(n);
    rep(i, 0, m) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        g.add_edge(u, v);
        rep(j, 0, 7) {
            if(s[u][j] == '1' and s[v][j] == '1') {
                uf.merge(7 * u + j, 7 * v + j);
            }
        }
    }
    while(q--) {
        int t, x, y;
        cin >> t >> x >> y;
        x--;
        y--;
        if(t == 1) {
            s[x][y] = '1';
            if(s[x][(y + 6) % 7] == '1') {
                uf.merge(7 * x + (y + 6) % 7, 7 * x + y);
            }
            if(s[x][(y + 1) % 7] == '1') {
                uf.merge(7 * x + y, 7 * x + (y + 1) % 7);
            }
            for(int to : g[x]) {
                if(s[to][y] == '1') {
                    uf.merge(7 * x + y, 7 * to + y);
                }
            }
        } else {
            cout << uf.size(7 * x) << '\n';
        }
    }
}
0