結果

問題 No.1266 7 Colors
ユーザー auauaauaua
提出日時 2020-10-23 22:38:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 176 ms / 3,000 ms
コード長 2,355 bytes
コンパイル時間 1,810 ms
コンパイル使用メモリ 173,320 KB
実行使用メモリ 18,036 KB
最終ジャッジ日時 2023-09-28 16:46:02
合計ジャッジ時間 6,527 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 86 ms
7,352 KB
testcase_04 AC 168 ms
14,076 KB
testcase_05 AC 97 ms
7,916 KB
testcase_06 AC 167 ms
15,064 KB
testcase_07 AC 176 ms
16,660 KB
testcase_08 AC 167 ms
14,340 KB
testcase_09 AC 150 ms
12,028 KB
testcase_10 AC 139 ms
11,260 KB
testcase_11 AC 105 ms
8,920 KB
testcase_12 AC 118 ms
9,404 KB
testcase_13 AC 128 ms
10,192 KB
testcase_14 AC 95 ms
7,876 KB
testcase_15 AC 173 ms
16,932 KB
testcase_16 AC 121 ms
9,672 KB
testcase_17 AC 169 ms
16,148 KB
testcase_18 AC 70 ms
18,036 KB
testcase_19 AC 73 ms
17,204 KB
testcase_20 AC 72 ms
17,172 KB
testcase_21 AC 42 ms
4,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef complex<double> comp;
const ll INF=1e9+7;
const ll inf=INF*INF;
const ll MOD=1e9+7;
const ll mod=MOD;
const int MAX=200010;

//Union-Find木

struct UnionFind {
    vector< int > data;

    UnionFind() = default;

    explicit UnionFind(size_t sz) : data(sz, -1) {}

    bool unite(int x, int y) {
        x = find(x), y = find(y);
        if(x == y) return false;
        if(data[x] > data[y]) swap(x, y);
        data[x] += data[y];
        data[y] = x;
        return true;
    }

    int find(int k) {
        if(data[k] < 0) return (k);
        return data[k] = find(data[k]);
    }

    int size(int k) {
        return -data[find(k)];
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }
};

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n,m,q;cin>>n>>m>>q;
    UnionFind uf(n*7);
    vector<string>s(n);
    rep(i,n)cin>>s[i];
    vector<vector<ll> >G(n);

    rep(i,m){
        ll u,v;cin>>u>>v;
        u--;v--;
        G[u].pb(v);
        G[v].pb(u);
        rep(col,7){
            if(s[u][col]=='1'&&s[v][col]=='1')uf.unite(7*u+col,7*v+col);
        }
    }
    rep(i,n){
        rep(col,7){
            if(s[i][col]=='1'&&s[i][(col+1)%7]=='1'){
                uf.unite(i*7+col,i*7+(col+1)%7);
            }
        }
    }
    while(q--){
        ll t;cin>>t;
        if(t==1){
            ll x,y;cin>>x>>y;
            x--;
            y--;
            s[x][y]='1';
            if(s[x][(y+6)%7]=='1'){
                uf.unite(x*7+y,x*7+(y+6)%7);
            }
            if(s[x][(y+1)%7]=='1'){
                uf.unite(x*7+y,x*7+(y+1)%7);
            }
            for(auto e:G[x]){
                if(s[e][y]=='1'){
                    uf.unite(x*7+y,e*7+y);
                }
            }
        }else{
            ll x,y;cin>>x>>y;
            x--;y--;
            cout<<uf.size(x*7)<<endl;
        }
    }
}
0