結果
問題 | No.1266 7 Colors |
ユーザー |
|
提出日時 | 2020-10-24 00:19:51 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 342 ms / 3,000 ms |
コード長 | 2,065 bytes |
コンパイル時間 | 1,268 ms |
コンパイル使用メモリ | 121,340 KB |
最終ジャッジ日時 | 2025-01-15 14:41:06 |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
#include <algorithm> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <tuple> #include <vector> using namespace std; using ll = int64_t; #define rep(i, j, n) for (int i = j; i < (n); ++i) #define rrep(i, j, n) for (int i = (n)-1; j <= i; --i) template <typename T> std::ostream &operator<<(std::ostream &os, std::vector<T> &a) { os << "{"; for (size_t i = 0; i < a.size(); ++i) os << (i > 0 ? "," : "") << a[i]; return os << "}"; } [[maybe_unused]] constexpr ll MOD = 1000000007; [[maybe_unused]] constexpr int INF = 0x3f3f3f3f; [[maybe_unused]] constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL; class disjoint_set { public: disjoint_set(int n) : par(n, -1) {} void merge(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; } bool same(int x, int y) { return root(x) == root(y); } int root(int x) { if (par[x] < 0) return x; return par[x] = root(par[x]); } int sizeOf(int x) { return -par[root(x)]; } private: vector<int> par; }; int main() { int n, m, q; cin >> n >> m >> q; // 頂点iのj色はi * 7 + jで vector<string> s(n); vector<vector<int>> g(n); disjoint_set ds(n * 7); rep(i, 0, n) { cin >> s[i]; rep(j, 0, 7) if (s[i][j] == '1' && s[i][(j + 1) % 7] == '1') ds.merge(i * 7 + j, i * 7 + (j + 1) % 7); } rep(i, 0, m) { int u, v; cin >> u >> v; --u, --v; g[u].push_back(v); g[v].push_back(u); rep(j, 0, 7) if (s[u][j] == '1' && s[v][j] == '1') ds.merge(u * 7 + j, v * 7 + j); } rep(i, 0, q) { int t, x, y; cin >> t >> x >> y; --x, --y; if (t == 1) { s[x][y] = '1'; rep(j, 0, 7) if (s[x][j] == '1' && s[x][(j + 1) % 7] == '1') ds.merge(x * 7 + j, x * 7 + (j + 1) % 7); for (int nx : g[x]) if (s[nx][y] == '1') ds.merge(x * 7 + y, nx * 7 + y); } else cout << ds.sizeOf(x * 7) << '\n'; } return 0; }