結果

問題 No.1266 7 Colors
ユーザー IKyoproIKyopro
提出日時 2020-10-23 22:23:15
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 188 ms / 3,000 ms
コード長 2,155 bytes
コンパイル時間 2,568 ms
コンパイル使用メモリ 207,196 KB
実行使用メモリ 17,500 KB
最終ジャッジ日時 2023-09-28 16:19:22
合計ジャッジ時間 6,360 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,380 KB
testcase_03 AC 84 ms
5,948 KB
testcase_04 AC 163 ms
13,320 KB
testcase_05 AC 89 ms
6,464 KB
testcase_06 AC 165 ms
14,124 KB
testcase_07 AC 178 ms
15,672 KB
testcase_08 AC 161 ms
13,400 KB
testcase_09 AC 139 ms
11,028 KB
testcase_10 AC 126 ms
10,248 KB
testcase_11 AC 98 ms
7,532 KB
testcase_12 AC 105 ms
8,384 KB
testcase_13 AC 118 ms
9,112 KB
testcase_14 AC 89 ms
6,464 KB
testcase_15 AC 188 ms
15,928 KB
testcase_16 AC 112 ms
8,596 KB
testcase_17 AC 162 ms
15,388 KB
testcase_18 AC 72 ms
17,500 KB
testcase_19 AC 77 ms
17,240 KB
testcase_20 AC 77 ms
17,472 KB
testcase_21 AC 41 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template<class T> bool chmin(T& a,T b){if(a>b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b){if(a<b) {a = b; return true;} return false;}
#define all(x) (x).begin(),(x).end()
#define debug(x)  cerr << #x << " = " << (x) << endl;

class UnionFind{
private:
    vec<int> p,s;
    int cnt;
public:
    UnionFind(){}
    UnionFind(int N):cnt(N),s(N,1),p(N){
        iota(p.begin(),p.end(),0);
    }
    int find(int x){
        if(p[x]==x) return x;
        else return p[x] = find(p[x]);
    }
    void unite(int x,int y){
        x = find(x); y = find(y);
        if(x==y) return;
        if(s[x]>s[y]) swap(x,y);
        p[x] = y;
        s[y] += s[x];
        cnt--;
    }
    bool is_same_set(int x,int y) {return find(x)==find(y);}
    int size(int x) {return s[find(x)];}
    int compnents_number(){return cnt;}
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,M,Q;
    cin >> N >> M >> Q;
    UnionFind uf(7*N);
    
    auto node = [&](int v,int c){
        return c*N+v;
    };

    vec<string> S(N);

    auto update_color = [&](int i){
        for(int j=0;j<7;j++){
            for(int k=1;k<=1;k+=2){
                int c = (j+k+7)%7;
                if(S[i][j]=='1' && S[i][c]=='1') uf.unite(node(i,j),node(i,c));
            }
        }
    };

    for(int i=0;i<N;i++){
        cin >> S[i];
        update_color(i);
    }
    vvec<int> g(N);
    for(int i=0;i<M;i++){
        int a,b;
        cin >> a >> b;
        a--; b--;
        g[a].push_back(b);
        g[b].push_back(a);
        for(int j=0;j<7;j++) if(S[a][j]=='1' && S[b][j]=='1') uf.unite(node(a,j),node(b,j));
    }

    auto update = [&](int v,int c){
        S[v][c] = '1';
        update_color(v);
        for(auto& u:g[v]) if(S[v][c]=='1' && S[u][c]=='1') uf.unite(node(v,c),node(u,c));
    };

    while(Q--){
        int t,x,y;
        cin >> t >> x >> y;
        x--,y--;
        if(t==1) update(x,y);
        else cout << uf.size(node(x,0)) << "\n";
    }
}
0