結果

問題 No.1266 7 Colors
ユーザー kawara_y_kyoprokawara_y_kyopro
提出日時 2020-10-23 23:30:16
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 168 ms / 3,000 ms
コード長 3,252 bytes
コンパイル時間 1,302 ms
コンパイル使用メモリ 130,252 KB
実行使用メモリ 14,728 KB
最終ジャッジ日時 2023-09-28 18:45:51
合計ジャッジ時間 5,240 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 79 ms
5,652 KB
testcase_04 AC 156 ms
10,932 KB
testcase_05 AC 87 ms
6,204 KB
testcase_06 AC 161 ms
11,980 KB
testcase_07 AC 167 ms
13,244 KB
testcase_08 AC 154 ms
11,208 KB
testcase_09 AC 134 ms
9,352 KB
testcase_10 AC 119 ms
8,884 KB
testcase_11 AC 92 ms
6,792 KB
testcase_12 AC 102 ms
7,584 KB
testcase_13 AC 110 ms
8,032 KB
testcase_14 AC 84 ms
5,892 KB
testcase_15 AC 163 ms
13,504 KB
testcase_16 AC 105 ms
7,552 KB
testcase_17 AC 168 ms
12,852 KB
testcase_18 AC 73 ms
14,728 KB
testcase_19 AC 75 ms
14,276 KB
testcase_20 AC 74 ms
14,300 KB
testcase_21 AC 40 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <bitset>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define ALL(v) v.begin(),v.end()
template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; }
template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; }
#define DEBUG_VLL(vec) REP(sz, vec.size()) std::cerr<<vec[sz]<<(sz==vec.size()-1?'\n':' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

class UnionFind {
public:
    int n;
    std::vector<int> par;
    UnionFind(int n) : n(n), par(n, -1) {}

    // 集合の代表を返す
    int find(int x) {
        if (par[x] < 0) return x;
        return par[x] = find(par[x]);
    }
    // x と y が同じ集合に属するかを判別
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    // x が属する集合の大きさを返す
    int size(int x) {
        return -par[find(x)];
    }
    // x と y を結合 (merge した場合に true を返す)
    bool unite(int x, int y) {
        int px = find(x), py = find(y);
        if (px == py) return false;
        if (par[px] > par[py]) swap(px, py);
        par[px] += par[py];
        par[py] = px;
        return true;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m, q; cin >> n >> m >> q;
    V<string> s(n);
    UnionFind uni(7 * n);
    for (int i = 0; i < n; i++) {
        cin >> s[i];
        for (int ci = 0; ci < 7; ci++) {
            if (s[i][ci] == '1' and s[i][(ci + 1) % 7] == '1') {
                uni.unite(ci * n + i, ((ci + 1) % 7) * n + i);
            }
            if (s[i][ci] == '1' and s[i][(ci + 6) % 7] == '1') {
                uni.unite(ci * n + i, ((ci + 6) % 7) * n + i);
            }
        }
    }

    V< V<int> > nocol(n);
    for (int i = 0; i < m; i++) {
        int u, v; cin >> u >> v;
        u--, v--;
        nocol[u].emplace_back(v), nocol[v].emplace_back(u);
        for (int ci = 0; ci < 7; ci++) {
            if (s[u][ci] == '1' and s[v][ci] == '1') uni.unite(n * ci + u, n * ci + v);
        }
    }

    for (int _q = 0; _q < q; _q++) {
        int t, x, y; cin >> t >> x >> y;
        x--, y--;
        if (t == 1) {
            s[x][y] = '1';
            if (s[x][(y + 1) % 7] == '1') {
                uni.unite(y * n + x, ((y + 1) % 7) * n + x);
            }
            if (s[x][(y + 6) % 7] == '1') {
                uni.unite(y * n + x, ((y + 6) % 7) * n + x);
            }
            for (int e: nocol[x]) {
                if (s[e][y] == '1') uni.unite(n * y + x, n * y + e);
            }
        } else {
            cout << uni.size(x) << '\n';
        }
    }
    return 0;
}
0