結果

問題 No.1266 7 Colors
ユーザー firiexpfiriexp
提出日時 2020-10-23 22:39:39
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 167 ms / 3,000 ms
コード長 2,300 bytes
コンパイル時間 1,143 ms
コンパイル使用メモリ 108,028 KB
実行使用メモリ 12,080 KB
最終ジャッジ日時 2023-09-28 16:49:40
合計ジャッジ時間 5,227 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 83 ms
5,528 KB
testcase_04 AC 149 ms
9,252 KB
testcase_05 AC 93 ms
5,792 KB
testcase_06 AC 154 ms
9,972 KB
testcase_07 AC 167 ms
10,824 KB
testcase_08 AC 150 ms
9,500 KB
testcase_09 AC 133 ms
8,200 KB
testcase_10 AC 129 ms
7,600 KB
testcase_11 AC 102 ms
6,224 KB
testcase_12 AC 114 ms
6,600 KB
testcase_13 AC 116 ms
7,080 KB
testcase_14 AC 90 ms
5,856 KB
testcase_15 AC 166 ms
11,008 KB
testcase_16 AC 107 ms
6,812 KB
testcase_17 AC 161 ms
10,692 KB
testcase_18 AC 91 ms
12,080 KB
testcase_19 AC 88 ms
11,756 KB
testcase_20 AC 90 ms
11,820 KB
testcase_21 AC 48 ms
4,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

class UnionFind {
    int n;
    vector<int> uni;
    int forest_size;
public:
    explicit UnionFind(int n) : n(n), uni(static_cast<u32>(n), -1), forest_size(n) {};

    int root(int a){
        if (uni[a] < 0) return a;
        else return (uni[a] = root(uni[a]));
    }

    bool unite(int a, int b) {
        a = root(a);
        b = root(b);
        if(a == b) return false;
        if(uni[a] > uni[b]) swap(a, b);
        uni[a] += uni[b];
        uni[b] = a;
        forest_size--;
        return true;
    }
    int size(){ return forest_size; }
    int size(int i){ return -uni[root(i)]; }
    bool same(int a, int b) { return root(a) == root(b); }
};

int main() {
    int n, m, q;
    cin >> n >> m >> q;
    vector<int> C(n);
    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;
        for (int j = 0; j < s.size(); ++j) {
            if(s[j] == '1') C[i] |= (1 << j);
        }
    }
    UnionFind uf(7*n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < 7; ++j) {
            if(C[i]&(1 << j) && C[i]&(1 << ((j+1)%7))){
                uf.unite(i*7+j, i*7+(j+1)%7);
            }
        }
    }
    vector<vector<int>> G(n);
    for (int i = 0; i < m; ++i) {
        int a, b;
        scanf("%d %d", &a, &b);
        a--; b--;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
        for (int j = 0; j < 7; ++j) {
            if(C[a]&(1 << j) && C[b]&(1 << j)) uf.unite(a*7+j, b*7+j);
        }
    }
    while(q--){
        int t, x, y;
        scanf("%d %d %d", &t, &x, &y); x--; y--;
        if(t == 1){
            C[x] |= (1 << y);
            if(C[x]&(1 << (y+1)%7)) uf.unite(x*7+y, x*7+(y+1)%7);
            if(C[x]&(1 << (y+6)%7)) uf.unite(x*7+y, x*7+(y+6)%7);
            for (auto &&i : G[x]) {
                if(C[i]&(1 << y)) uf.unite(x*7+y, i*7+y);
            }
        }else {

            printf("%d\n", uf.size(x*7));
        }
    }

    return 0;
}
0