結果

問題 No.1266 7 Colors
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-10-24 04:06:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 312 ms / 3,000 ms
コード長 1,929 bytes
コンパイル時間 2,042 ms
コンパイル使用メモリ 174,204 KB
実行使用メモリ 14,824 KB
最終ジャッジ日時 2023-09-28 20:06:39
合計ジャッジ時間 8,400 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 244 ms
5,708 KB
testcase_04 AC 298 ms
11,108 KB
testcase_05 AC 241 ms
6,396 KB
testcase_06 AC 301 ms
11,940 KB
testcase_07 AC 312 ms
13,228 KB
testcase_08 AC 294 ms
11,104 KB
testcase_09 AC 284 ms
9,260 KB
testcase_10 AC 271 ms
8,772 KB
testcase_11 AC 248 ms
6,616 KB
testcase_12 AC 257 ms
7,496 KB
testcase_13 AC 268 ms
8,020 KB
testcase_14 AC 242 ms
5,908 KB
testcase_15 AC 307 ms
13,208 KB
testcase_16 AC 258 ms
7,452 KB
testcase_17 AC 311 ms
12,788 KB
testcase_18 AC 276 ms
14,824 KB
testcase_19 AC 159 ms
14,208 KB
testcase_20 AC 159 ms
14,248 KB
testcase_21 AC 227 ms
4,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.10.24 04:06:21
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;


class UnionFind {
private:
    vector<int> par;
public:
    UnionFind(int n) {
        par.resize(n, -1);
    }
    int root(int x) {
        if (par[x] < 0) return x;
        return par[x] = root(par[x]);
    }
    bool unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return false;
        if (size(rx) < size(ry)) swap(rx, ry);
        par[rx] += par[ry];
        par[ry] = rx;
        return true;
    }
    bool same(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
    int size(int x) {
        return -par[root(x)];
    }
};
int main() {
    int n,m,q;cin >> n >> m >> q;
    vector<vector<int>> g(n);
    vector<string> s(n);
    UnionFind uf(7*n);
    for (int i = 0; i < n; i++) {
        cin >> s[i];
        for (int j = 0; j < 7; j++) {
            if (s[i][j] == '0' || s[i][(j+1)%7] == '0') continue;
            uf.unite(7*i+j,7*i+(j+1)%7);
        }
    }
    for (int i = 0; i < m; i++) {
        int u,v;cin >> u >> v;
        u--;v--;
        g[u].push_back(v);
        g[v].push_back(u);
        for (int j = 0; j < 7; j++) {
            if (s[u][j] == '0' || s[v][j] == '0') continue;
            uf.unite(7*u+j,7*v+j);
        }
    }
    while(q--) {
        int t,x,y;cin >> t >> x >> y;
        x--;y--;
        if (t == 1) {
            s[x][y] = '1';
            if (s[x][(y+6)%7] == '1') uf.unite(7*x+y,7*x+(y+6)%7);
            if (s[x][(y+1)%7] == '1') uf.unite(7*x+y,7*x+(y+1)%7);
            for (int i = 0; i < g[x].size(); i++) {
                if (s[g[x][i]][y] == '0') continue;
                uf.unite(7*x+y,7*g[x][i]+y);
            }
        }
        else {
            cout << uf.size(7*x) << endl;
        }
    }
    return 0;
}
0