結果

問題 No.1266 7 Colors
ユーザー onsen_manjuuuonsen_manjuuu
提出日時 2020-11-02 00:15:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 283 ms / 3,000 ms
コード長 1,849 bytes
コンパイル時間 1,639 ms
コンパイル使用メモリ 174,956 KB
実行使用メモリ 14,764 KB
最終ジャッジ日時 2023-09-29 11:41:05
合計ジャッジ時間 8,960 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 244 ms
5,688 KB
testcase_04 AC 267 ms
10,916 KB
testcase_05 AC 236 ms
6,216 KB
testcase_06 AC 267 ms
11,972 KB
testcase_07 AC 283 ms
13,024 KB
testcase_08 AC 270 ms
11,216 KB
testcase_09 AC 256 ms
9,468 KB
testcase_10 AC 250 ms
8,860 KB
testcase_11 AC 240 ms
6,828 KB
testcase_12 AC 243 ms
7,552 KB
testcase_13 AC 247 ms
8,152 KB
testcase_14 AC 236 ms
5,892 KB
testcase_15 AC 279 ms
13,192 KB
testcase_16 AC 242 ms
7,604 KB
testcase_17 AC 274 ms
12,816 KB
testcase_18 AC 274 ms
14,764 KB
testcase_19 AC 157 ms
14,404 KB
testcase_20 AC 157 ms
14,412 KB
testcase_21 AC 224 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;
using ll = long long;

class UnionFind{
  private:
    vector<int> data;

  public:
    UnionFind(int n) : data(n,-1) {}
    int root(int i) { return data[i] < 0 ? i : data[i] = root(data[i]); }
    int size(int i) { return -data[root(i)]; }
    bool same(int x, int y) {return root(x) == root(y); }
    bool connected() {return size(0) == (int)data.size(); }

    bool unit(int i,int j){
        i = root(i);
        j = root(j);
        if(size(j) > size(i))swap(j, i);
        if(i != j){
            data[i] += data[j];
            data[j] = i;
        }
        return i != j;
    }
};

int N, M, Q;

int id(int city, int color) {
    return city * 7 + color;
}

int main() {
    cin >> N >> M >> Q;
    vector<string> s(N);
    vector<vector<int>> hen(N);
    for(auto &i: s) {cin >> i; for(auto &j : i) j -= '0';};
    UnionFind tree(7 * N);
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < 7; j++)if(s[i][j] && s[i][(j + 1) % 7])tree.unit(id(i, j), id(i, (j + 1) % 7));
    }


    for(int i = 0; i < M; i++) {int a, b; cin >> a >> b; a--, b--; hen[a].push_back(b), hen[b].push_back(a);
        for(int j = 0; j < 7; j++) {
            if(s[a][j] && s[b][j]) tree.unit(id(a, j), id(b, j));
        }
    }






    for(int i = 0; i < Q; i++) {
        int t; cin >> t;
        if(t == 1) {
            int x, y; cin >> x >> y; x--, y--;
            auto& st = s[x];
            st[y] = 1;
            if(st[(y + 1) % 7])tree.unit(id(x, y), id(x, (y + 1) % 7));
            if(st[(y - 1 + 7) % 7])tree.unit(id(x, y), id(x, (y - 1 + 7) % 7));
            for(auto nxt : hen[x]) {
                if(s[nxt][y]) tree.unit(id(x, y), id(nxt, y));
            }
        } else {
            int x, y; cin >> x >> y; x--;
            cout << tree.size(id(x, 0)) << '\n';

        }
    }


}
0