結果

問題 No.1266 7 Colors
ユーザー tsutajtsutaj
提出日時 2020-10-23 22:25:24
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 187 ms / 3,000 ms
コード長 3,432 bytes
コンパイル時間 1,183 ms
コンパイル使用メモリ 109,304 KB
実行使用メモリ 14,820 KB
最終ジャッジ日時 2023-09-28 16:24:31
合計ジャッジ時間 5,263 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 89 ms
5,832 KB
testcase_04 AC 172 ms
11,152 KB
testcase_05 AC 97 ms
6,292 KB
testcase_06 AC 173 ms
12,140 KB
testcase_07 AC 183 ms
13,208 KB
testcase_08 AC 167 ms
11,304 KB
testcase_09 AC 146 ms
9,408 KB
testcase_10 AC 137 ms
9,224 KB
testcase_11 AC 105 ms
6,868 KB
testcase_12 AC 115 ms
7,688 KB
testcase_13 AC 131 ms
8,284 KB
testcase_14 AC 96 ms
6,080 KB
testcase_15 AC 187 ms
13,728 KB
testcase_16 AC 122 ms
7,724 KB
testcase_17 AC 182 ms
12,952 KB
testcase_18 AC 92 ms
14,820 KB
testcase_19 AC 93 ms
14,412 KB
testcase_20 AC 93 ms
14,400 KB
testcase_21 AC 48 ms
4,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

/**
 * @brief Union-Find
 * @docs ./docs/union_find.md
 */

#include <algorithm>
#include <vector>

struct UnionFind {
private:
    const int n;
    int size_;
    vector<int> uf;
public:
    // 初期化 UnionFind uni(n) のように宣言すれば良い
    UnionFind(int _n) : n(_n), size_(_n), uf(_n, -1) {}
    // find (木の根を求める)
    int find(int x) {return (uf[x] < 0) ? x : uf[x] = find(uf[x]);}
    // x と y が同じ集合に属するかどうか
    bool same(int x, int y) {return find(x) == find(y);}
    // x が属する集合の要素数
    int size(int x) {return -uf[find(x)];}
    // 集合はいくつあるか
    int size()      {return size_;}
    // x と y の属する集合を併合
    bool unite(int x, int y) {
        x = find(x); y = find(y);
        if(x == y) return false;
        size_--;
        if(-uf[x] < -uf[y]) swap(x, y);
        uf[x] += uf[y]; uf[y] = x;
        return true;
    }
};

int main() {
    int N, M, Q; scanf("%d%d%d", &N, &M, &Q);
    vector<string> s(N);
    for(int i=0; i<N; i++) cin >> s[i];

    using Graph = vector< vector<int> >;
    Graph G(N);
    UnionFind uf(N * 7);

    auto to_v = [](int v, int c) {
                    return 7*v + c;
                };

    for(int i=0; i<N; i++) {
        for(int j=0; j<7; j++) {
            int c0 = j, c1 = (c0 + 1) % 7;
            if(s[i][c0] == '1' and s[i][c1] == '1') {
                uf.unite(to_v(i, c0), to_v(i, c1));
            }
        }
    }
    for(int i=0; i<M; i++) {
        int u, v; scanf("%d%d", &u, &v); u--; v--;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
        for(int c=0; c<7; c++) {
            if(s[u][c] == '1' and s[v][c] == '1') {
                uf.unite(to_v(u, c), to_v(v, c));
            }
        }
    }

    while(Q--) {
        int t, x, y; scanf("%d%d%d", &t, &x, &y);
        if(t == 1) {
            x--; y--;
            s[x][y] = '1';
            int u = x, cu = y;
            for(auto v : G[u]) {
                if(s[u][cu] == '1' and s[v][cu] == '1') {
                    uf.unite(to_v(u, cu), to_v(v, cu));
                }
            }
            for(int dc=-1; dc<=+1; dc+=2) {
                int cv = (cu + dc + 7) % 7;
                if(s[u][cu] == '1' and s[u][cv] == '1') {
                    uf.unite(to_v(u, cu), to_v(u, cv));
                }
            }
        }
        if(t == 2) {
            x--;
            int ans = uf.size(to_v(x, 0));
            printf("%d\n", ans);
        }
    }
    return 0;
}
0