結果
問題 | No.1266 7 Colors |
ユーザー |
![]() |
提出日時 | 2020-10-23 22:18:21 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 153 ms / 3,000 ms |
コード長 | 3,600 bytes |
コンパイル時間 | 2,354 ms |
コンパイル使用メモリ | 202,868 KB |
最終ジャッジ日時 | 2025-01-15 13:17:31 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
#include <bits/stdc++.h>using namespace std;#include <algorithm>#include <cassert>#include <vector>namespace atcoder {// Implement (union by size) + (path compression)// Reference:// Zvi Galil and Giuseppe F. Italiano,// Data structures and algorithms for disjoint set union problemsstruct dsu {public:dsu() : _n(0) {}dsu(int n) : _n(n), parent_or_size(n, -1) {}int merge(int a, int b) {assert(0 <= a && a < _n);assert(0 <= b && b < _n);int x = leader(a), y = leader(b);if (x == y) return x;if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);parent_or_size[x] += parent_or_size[y];parent_or_size[y] = x;return x;}bool same(int a, int b) {assert(0 <= a && a < _n);assert(0 <= b && b < _n);return leader(a) == leader(b);}int leader(int a) {assert(0 <= a && a < _n);if (parent_or_size[a] < 0) return a;return parent_or_size[a] = leader(parent_or_size[a]);}int size(int a) {assert(0 <= a && a < _n);return -parent_or_size[leader(a)];}std::vector<std::vector<int>> groups() {std::vector<int> leader_buf(_n), group_size(_n);for (int i = 0; i < _n; i++) {leader_buf[i] = leader(i);group_size[leader_buf[i]]++;}std::vector<std::vector<int>> result(_n);for (int i = 0; i < _n; i++) {result[i].reserve(group_size[i]);}for (int i = 0; i < _n; i++) {result[leader_buf[i]].push_back(i);}result.erase(std::remove_if(result.begin(), result.end(),[&](const std::vector<int>& v) { return v.empty(); }),result.end());return result;}private:int _n;// root node: -1 * component size// otherwise: parentstd::vector<int> parent_or_size;};} // namespace atcoderusing namespace atcoder;using lint = long long int;using P = pair<int, int>;using PL = pair<lint, lint>;#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)#define IFOR(i, begin, end) for(int 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 ALL(a) (a).begin(),(a).end()constexpr int MOD = 1000000007;constexpr lint B1 = 1532834020;constexpr lint M1 = 2147482409;constexpr lint B2 = 1388622299;constexpr lint M2 = 2147478017;constexpr int INF = 2147483647;void yes(bool expr) {cout << (expr ? "Yes" : "No") << "\n";}template<class T>void chmax(T &a, const T &b) { if (a<b) a=b; }template<class T>void chmin(T &a, const T &b) { if (b<a) a=b; }int main(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int N, M, Q;cin >> N >> M >> Q;dsu d(N*7);vector<string> s(N);REP(i, N) {cin >> s[i];REP(j, 7) {if(s[i][j] == '1' && s[i][(j+1)%7] == '1') d.merge(j*N+i, (j+1)%7*N+i);}}vector<vector<int>> nbr(N);REP(i, M) {int u, v;cin >> u >> v;u--; v--;nbr[u].push_back(v);nbr[v].push_back(u);REP(j, 7) if(s[u][j] == '1' && s[v][j] == '1') d.merge(j*N+u, j*N+v);}REP(q, Q) {int m, x, y;cin >> m >> x >> y;x--; y--;if(m == 1) {s[x][y] = '1';if(s[x][(y+1)%7] == '1') d.merge(y*N+x, (y+1)%7*N+x);if(s[x][(y+6)%7] == '1') d.merge(y*N+x, (y+6)%7*N+x);for(int z : nbr[x]) if(s[z][y] == '1') d.merge(y*N+x, y*N+z);} else {cout << d.size(x) << "\n";}}}