結果

問題 No.1266 7 Colors
ユーザー mtsdmtsd
提出日時 2020-10-23 22:22:54
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 342 ms / 3,000 ms
コード長 2,864 bytes
コンパイル時間 1,339 ms
コンパイル使用メモリ 123,672 KB
実行使用メモリ 20,276 KB
最終ジャッジ日時 2023-09-28 16:18:25
合計ジャッジ時間 8,430 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 243 ms
6,172 KB
testcase_04 AC 324 ms
14,884 KB
testcase_05 AC 251 ms
7,140 KB
testcase_06 AC 328 ms
16,184 KB
testcase_07 AC 340 ms
17,988 KB
testcase_08 AC 318 ms
15,132 KB
testcase_09 AC 305 ms
12,304 KB
testcase_10 AC 302 ms
11,300 KB
testcase_11 AC 265 ms
8,200 KB
testcase_12 AC 278 ms
9,128 KB
testcase_13 AC 283 ms
10,240 KB
testcase_14 AC 250 ms
7,136 KB
testcase_15 AC 342 ms
18,492 KB
testcase_16 AC 279 ms
9,664 KB
testcase_17 AC 341 ms
17,776 KB
testcase_18 AC 271 ms
20,276 KB
testcase_19 AC 162 ms
19,768 KB
testcase_20 AC 162 ms
19,892 KB
testcase_21 AC 223 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}
 
template<class T> inline bool chmax(T &a, T b){
    if(a<b){
        a = b;
        return true;
    }
    return false;
}

template<class T> inline bool chmin(T &a, T b){
    if(a>b){
        a = b;
        return true;
    }
    return false;
}
class UnionFind {
private:
    int sz;
    vector<int> par, size_,res;
public:
    UnionFind(){}
    UnionFind(int node_size) : sz(node_size), par(sz), size_(sz, 1),res(sz,0){
        iota(par.begin(), par.end(), 0);
    }
    int find(int x){
        if(par[x] == x) return x;
        else return par[x] = find(par[x]);
    }
    void unite(int x,int y){
        x = find(x), y = find(y);
        if(x == y) return;
        if(size_[x] < size_[y]) swap(x,y);
        par[y] = x;
        size_[x] += size_[y];
    }
    
    int size(int x){
        x = find(x);
        return size_[x];
    }
    bool same(int x,int y){
        return find(x) == find(y);
    }
};
vector<vector<int> > g;
int main(){
    int n,m,q;
    cin >> n >> m >> q;
    UnionFind uf(n*7);
    vector<string> s(n);
    rep(i,n){
        cin >> s[i];
        rep(j,7){
            if(s[i][j]=='1'&&s[i][(j+1)%7]=='1'){
                uf.unite(7*i+j,7*i+(j+1)%7);
            }
        }
    }
    g.resize(n);
    rep(i,m){
        int a,b;
        cin >> a >> b;
        a--;b--;
        g[a].push_back(b);
        g[b].push_back(a);
        rep(j,7){
            if(s[a][j]=='1'&&s[b][j]=='1'){
                uf.unite(7*a+j,7*b+j);
            }
        }
    }
    rep(i,q){
        int c,a,b;
        cin >> c >> a >> b;
        if(c==1){
            a--;b--;
            s[a][b] = '1';
            if(s[a][(b+6)%7]=='1'){
                uf.unite(7*a+(b+6)%7,7*a+b);
            }
            if(s[a][(b+1)%7]=='1'){
                uf.unite(7*a+(b+1)%7,7*a+b);
            }
            for(auto& x:g[a]){
                if(s[x][b]=='1'){
                    uf.unite(7*a+b,7*x+b);
                }
            }
        }else{
            a--;
            cout << uf.size(a*7) << "\n";
        }
    }
    return 0;
}
0