結果

問題 No.1266 7 Colors
ユーザー onsen_manjuuuonsen_manjuuu
提出日時 2020-11-02 00:12:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,833 bytes
コンパイル時間 2,135 ms
コンパイル使用メモリ 172,956 KB
実行使用メモリ 14,672 KB
最終ジャッジ日時 2023-09-29 11:40:21
合計ジャッジ時間 10,410 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 245 ms
5,580 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 273 ms
14,672 KB
testcase_19 AC 158 ms
14,224 KB
testcase_20 AC 157 ms
14,308 KB
testcase_21 AC 222 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])tree.unit(id(x, y), id(x, (y + 1) % 7));
            if(st[y - 1])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