結果

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

ソースコード

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

#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <bitset>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define ALL(v) v.begin(),v.end()
template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; }
template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; }
#define DEBUG_VLL(vec) REP(sz, vec.size()) std::cerr<<vec[sz]<<(sz==vec.size()-1?'\n':' ');
const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;
class UnionFind {
public:
int n;
std::vector<int> par;
UnionFind(int n) : n(n), par(n, -1) {}
//
int find(int x) {
if (par[x] < 0) return x;
return par[x] = find(par[x]);
}
// x y
bool same(int x, int y) {
return find(x) == find(y);
}
// x
int size(int x) {
return -par[find(x)];
}
// x y (merge true )
bool unite(int x, int y) {
int px = find(x), py = find(y);
if (px == py) return false;
if (par[px] > par[py]) swap(px, py);
par[px] += par[py];
par[py] = px;
return true;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m, q; cin >> n >> m >> q;
V<string> s(n);
UnionFind uni(7 * n);
for (int i = 0; i < n; i++) {
cin >> s[i];
for (int ci = 0; ci < 7; ci++) {
if (s[i][ci] == '1' and s[i][(ci + 1) % 7] == '1') {
uni.unite(ci * n + i, ((ci + 1) % 7) * n + i);
}
if (s[i][ci] == '1' and s[i][(ci + 6) % 7] == '1') {
uni.unite(ci * n + i, ((ci + 6) % 7) * n + i);
}
}
}
V< V<int> > nocol(n);
for (int i = 0; i < m; i++) {
int u, v; cin >> u >> v;
u--, v--;
nocol[u].emplace_back(v), nocol[v].emplace_back(u);
for (int ci = 0; ci < 7; ci++) {
if (s[u][ci] == '1' and s[v][ci] == '1') uni.unite(n * ci + u, n * ci + v);
}
}
for (int _q = 0; _q < q; _q++) {
int t, x, y; cin >> t >> x >> y;
x--, y--;
if (t == 1) {
s[x][y] = '1';
if (s[x][(y + 1) % 7] == '1') {
uni.unite(y * n + x, ((y + 1) % 7) * n + x);
}
if (s[x][(y + 6) % 7] == '1') {
uni.unite(y * n + x, ((y + 6) % 7) * n + x);
}
for (int e: nocol[x]) {
if (s[e][y] == '1') uni.unite(n * y + x, n * y + e);
}
} else {
cout << uni.size(x) << '\n';
}
}
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0