結果

問題 No.1266 7 Colors
ユーザー oteraotera
提出日時 2020-10-24 00:49:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 277 ms / 3,000 ms
コード長 3,730 bytes
コンパイル時間 1,537 ms
コンパイル使用メモリ 138,284 KB
実行使用メモリ 23,500 KB
最終ジャッジ日時 2023-09-28 19:49:41
合計ジャッジ時間 7,343 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 185 ms
8,052 KB
testcase_04 AC 261 ms
17,704 KB
testcase_05 AC 186 ms
9,032 KB
testcase_06 AC 269 ms
19,216 KB
testcase_07 AC 277 ms
21,592 KB
testcase_08 AC 256 ms
18,232 KB
testcase_09 AC 236 ms
14,816 KB
testcase_10 AC 239 ms
13,564 KB
testcase_11 AC 199 ms
10,124 KB
testcase_12 AC 211 ms
11,412 KB
testcase_13 AC 220 ms
12,292 KB
testcase_14 AC 184 ms
8,824 KB
testcase_15 AC 276 ms
21,780 KB
testcase_16 AC 218 ms
11,440 KB
testcase_17 AC 275 ms
21,008 KB
testcase_18 AC 202 ms
23,500 KB
testcase_19 AC 81 ms
22,656 KB
testcase_20 AC 81 ms
22,580 KB
testcase_21 AC 166 ms
4,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *    author:  otera    
**/
#include<iostream>
#include<string> 
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm> 
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<array>
using namespace std;

#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

struct UnionFind {
    vector<int> par, w;
    
    UnionFind(int n) : par(n, -1), w(n, 0) { }
    void init(int n) { par.assign(n, -1); w.assign(n, 0); }
    
    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    
    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) {
            ++w[x];
            return false;
        }
        if (par[x] > par[y]) swap(x, y); // merge technique
        par[x] += par[y];
        par[y] = x;
        w[x] += w[y];
        ++w[x];
        return true;
    }
    
    int size(int x) {
        return -par[root(x)];
    }
 
    int wei(int x) {
        return w[root(x)];
    }
};

void solve() {
	int n, m, q; cin >> n >> m >> q;
    UnionFind uf(7 * n);
    vector<string> s(n);
    rep(i, n) {
        cin >> s[i];
        rep(j, 7) {
            if(s[i][j] == '1' and s[i][(j + 1) % 7] == '1') {
                uf.merge(i + j * n, i + (j + 1) % 7 * n);
            }
            if(s[i][j] == '1' and s[i][(j + 6) % 7] == '1') {
                uf.merge(i + j * n, i + (j + 6) % 7 * n);
            }
        }
    }
    vector<vector<int>> g(n, vector<int>());
    rep(i, m) {
        int u, v; cin >> u >> v; -- u, -- v;
        g[u].push_back(v);
        g[v].push_back(u);
        rep(j, 7) {
            if(s[u][j] == '1' and s[v][j] == '1') {
                uf.merge(u + j * n, v + j * n);
            }
        }
    }
    rep(_, q) {
        int t; cin >> t;
        if(t == 1) {
            int x, y; cin >> x >> y; -- x, -- y;
            s[x][y] = '1';
            if(s[x][(y + 1) % 7] == '1') {
                uf.merge(x + y * n, x + ((y + 1) % 7) * n);
            }
            if(s[x][(y - 1 + 7) % 7] == '1') {
                uf.merge(x + y * n, x + ((y + 6) % 7) * n);
            }
            for(int nx: g[x]) {
                if(s[nx][y] == '1') {
                    uf.merge(x + y * n, nx + y * n);
                }
            }
        } else {
            int x, y; cin >> x >> y; -- x;
            cout << uf.size(x) << endl;
        }
    }
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}
0