結果
問題 | No.1266 7 Colors |
ユーザー |
![]() |
提出日時 | 2020-10-23 22:38:38 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 225 ms / 3,000 ms |
コード長 | 2,066 bytes |
コンパイル時間 | 923 ms |
コンパイル使用メモリ | 88,908 KB |
実行使用メモリ | 14,964 KB |
最終ジャッジ日時 | 2024-07-21 11:29:30 |
合計ジャッジ時間 | 5,968 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
#include <iostream> #include <algorithm> #include <iomanip> #include <vector> #include <queue> #include <set> #include <map> using namespace std; typedef long long ll; struct UnionFind { vector<int> data; UnionFind(int size) : data(size, -1) { } bool unionSet(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { return -data[root(x)]; } }; int n, m, q; string s[100000]; vector<int> G[100000]; int to_idx(int i, int col){ return i*7+col; } UnionFind uf(0); void input(){ cin >> n >> m >> q; uf = UnionFind(n*7); for(int i = 0; i < n; i++) cin >> s[i]; for(int i = 0; i < m; i++){ int u, v; cin >> u >> v; u--; v--; G[u].push_back(v); G[v].push_back(u); for(int col = 0; col < 7; col++){ if(s[u][col] == '1' && s[v][col] == '1'){ uf.unionSet(to_idx(u, col), to_idx(v, col)); } } } for(int i = 0; i < n; i++){ for(int j = 0; j < 7; j++){ if(s[i][j] == '1' && s[i][(j+1)%7] == '1'){ uf.unionSet(to_idx(i, j), to_idx(i, (j+1)%7)); } } } } void solve(){ int t, x, y; cin >> t >> x >> y; x--; y--; if(t == 1){ int nx = (y+1)%7; int pre = (y+6)%7; s[x][y] = '1'; if(s[x][pre] == '1') uf.unionSet(to_idx(x, pre), to_idx(x, y)); if(s[x][nx] == '1') uf.unionSet(to_idx(x, nx), to_idx(x, y)); for(int to : G[x]){ if(s[to][y] == '1'){ uf.unionSet(to_idx(x, y), to_idx(to, y)); } } }else{ cout << uf.size(to_idx(x, 0)) << endl; } } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; input(); for(int i = 0; i < q; i++) solve(); }