結果

問題 No.1266 7 Colors
ユーザー naribow
提出日時 2020-10-24 00:42:44
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 354 ms / 3,000 ms
コード長 3,657 bytes
コンパイル時間 2,330 ms
コンパイル使用メモリ 203,796 KB
最終ジャッジ日時 2025-01-15 14:47:30
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#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:
//
        *3par:
//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) {
// xy
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 {
x--;
cout << uf.size(x*7) << endl;
}
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0