結果

問題 No.1266 7 Colors
ユーザー kaikeykaikey
提出日時 2020-10-23 22:31:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 289 ms / 3,000 ms
コード長 3,088 bytes
コンパイル時間 1,733 ms
コンパイル使用メモリ 171,080 KB
実行使用メモリ 27,076 KB
最終ジャッジ日時 2023-09-28 16:37:40
合計ジャッジ時間 7,838 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,104 KB
testcase_01 AC 4 ms
7,128 KB
testcase_02 AC 4 ms
7,168 KB
testcase_03 AC 169 ms
12,876 KB
testcase_04 AC 266 ms
22,404 KB
testcase_05 AC 179 ms
13,268 KB
testcase_06 AC 283 ms
23,628 KB
testcase_07 AC 286 ms
24,900 KB
testcase_08 AC 275 ms
22,700 KB
testcase_09 AC 249 ms
20,412 KB
testcase_10 AC 246 ms
19,748 KB
testcase_11 AC 202 ms
14,500 KB
testcase_12 AC 219 ms
15,652 KB
testcase_13 AC 231 ms
18,692 KB
testcase_14 AC 185 ms
12,980 KB
testcase_15 AC 289 ms
25,348 KB
testcase_16 AC 221 ms
15,760 KB
testcase_17 AC 286 ms
24,880 KB
testcase_18 AC 144 ms
27,076 KB
testcase_19 AC 165 ms
24,948 KB
testcase_20 AC 164 ms
24,836 KB
testcase_21 AC 100 ms
9,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include<random>
using namespace std; typedef unsigned long long _ulong; typedef long long int lint; typedef pair<lint, lint> plint; typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(lint i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define endk '\n'
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
const lint MOD = 1e9 + 7, INF = 1e18;
lint dx[8] = { 0, -1, 1, 0, 1, -1, 1, -1 }, dy[8] = { 1, 0, 0, -1, -1, -1, 1, 1 };
typedef pair<long double, lint> Pa;
typedef pair<lint, plint> tlint;
struct edge {
	lint cost;
	lint u, v;
};

template< typename T >
class UnionFind {
public:
	vector <T> par; // 各元の親を表す配列
	vector <T> siz; // 素集合のサイズを表す配列(1 で初期化)

	// Constructor
	UnionFind(T sz_) : par(sz_), siz(sz_, 1ll) {
		for (T i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身
	}
	void init(T sz_) {
		par.resize(sz_);
		siz.assign(sz_, 1ll);  // resize だとなぜか初期化されなかった
		for (T i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身
	}

	// Member Function
	// Find
	T root(T x) { // 根の検索
		while (par[x] != x) {
			x = par[x] = par[par[x]]; // x の親の親を x の親とする
		}
		return x;
	}

	// Union(Unite, Merge)
	bool merge(T x, T y) {
		x = root(x);
		y = root(y);
		if (x == y) return false;
		// merge technique(データ構造をマージするテク.小を大にくっつける)
		if (siz[x] < siz[y]) swap(x, y);
		siz[x] += siz[y];
		par[y] = x;
		return true;
	}

	bool issame(T x, T y) { // 連結判定
		return root(x) == root(y);
	}

	T size(T x) { // 素集合のサイズ
		return siz[root(x)];
	}
};

lint N, M, Q, a, b;
vector<lint> to[100000];
lint color[100000][7];
int main() {
	cin >> N >> M >> Q;
	UnionFind<lint> tree(N * 7);
	REP(i, N) {
		string s;
		lint cnt = 0;
		cin >> s;
		REP(j, 7) {
			color[i][j] = s[j] - '0';
			cnt += (s[j] - '0');
		}
		REP(j, 7) {
			if (color[i][j] == 1 && color[i][(j + 1) % 7] == 1) {
				tree.merge(j * N + i, ((j + 1) % 7) * N + i);
			}
		}
	}
	REP(i, M) {
		cin >> a >> b; a--; b--;
		to[a].push_back(b);
		to[b].push_back(a);
		REP(j, 7) {
			if (color[a][j] == 1 && color[b][j] == 1) {
				tree.merge(j * N + a, j * N + b);
			}
		}
	}
	vector<lint> ans;
	REP(i, Q) {
		lint c, x, y;
		cin >> c >> x >> y; x--; y--;
		if (c == 1) {
			color[x][y] = 1;
			REP(j, 7) {
				if (color[x][j] == 1 && color[x][(j + 1) % 7] == 1) {
					tree.merge(j * N + x, ((j + 1) % 7) * N + x);
				}
			}
			for (lint nxt : to[x]) {
				if (color[nxt][y] == 1) {
					tree.merge(y* N + x, y* N + nxt);
				}
			}
		}
		else {
			ans.push_back(tree.size(x));
		}
	}
	REP(i, SZ(ans)) {
		cout << ans[i] << endk;
	}
}
0