結果
| 問題 |
No.1266 7 Colors
|
| コンテスト | |
| ユーザー |
naribow
|
| 提出日時 | 2020-10-24 00:37:42 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 3,640 bytes |
| コンパイル時間 | 2,725 ms |
| コンパイル使用メモリ | 204,356 KB |
| 最終ジャッジ日時 | 2025-01-15 14:47:00 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 RE * 2 |
| other | AC * 3 WA * 16 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
typedef unsigned long long ull;
typedef long double ldouble;
const ll INF = 1e18;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
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;
}
// union by size + path having
class UnionFind {
public:
//素集合の要素の親(木構造の根)を配列*3で保持している.初期状態では頂点間の辺は存在せず,孤立した状態のため,自分自身を根として初期化している(par:
//parent の略).
vector<ll> par;
// 各集合(木)の大きさ.根をたどるとその木の大きさが返されるように実装されている.この配列を用意しておくとある頂点の連結成分の大きさを簡単に求められる
vector<ll> siz;
// Constructor
UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身
}
void init(ll sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった
for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身
}
// Member Function
// Find
ll root(ll x) { // 根の検索
while (par[x] != x) {
x = par[x] = par[par[x]]; // x の親の親を x の親とする
}
return x;
}
// Union(Unite, Merge)
bool merge(ll x, ll 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(ll x, ll y) { // 連結判定
return root(x) == root(y);
}
ll size(ll x) { // 素集合のサイズ
return siz[root(x)];
}
};
typedef pair<int, int> P;
int main() {
int n, m, q;
cin >> n >> m >> q;
vector<bool> s(n*7);
vector<vector<int> > G(n);
UnionFind uf(n*7);
rep(i, n) {
string str;
cin >> str;
rep(j, 7) {
if(str[j] == '1') s[i*7+j] = true;
}
rep(j, 7) {
if(s[i*7+j] && s[i*7+(j+1)%7]) {
uf.merge(i*7+j, i*7+(j+1)%7);
}
}
}
rep(i, m) {
int u, v;
cin >> u >> v;
u--; v--;
G[u].emplace_back(v);
G[v].emplace_back(u);
rep(j, 7) {
if(s[u*7+j] && s[v*7+j]) {
uf.merge(u*7+j, v*7+j);
}
}
}
rep(i, q) {
int query;
cin >> query;
int x, y;
cin >> x >> y;
if(query == 1) {
// 都市xに色y
y--;
x--;
s[x*7+y] = true;
rep(j, G[x].size()) {
int z = G[x][j];
if(s[z*7 + y]) {
uf.merge(z*7 + y, x*7 + y);
}
}
if(s[x*7 + (y-1+7)%7]) {
uf.merge(x * 7 + (y - 1 + 7) % 7, x * 7 + y);
}
if(s[x*7 + (y+1)%7]) {
uf.merge(x*7 + (y+1)%7, x*7 + y);
}
}
else {
cout << uf.size(x*7) << endl;
}
}
}
naribow