結果
| 問題 | No.1266 7 Colors |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-10-30 19:20:05 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 250 ms / 3,000 ms |
| コード長 | 2,112 bytes |
| コンパイル時間 | 2,097 ms |
| コンパイル使用メモリ | 202,744 KB |
| 最終ジャッジ日時 | 2025-01-15 16:33:49 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,b,e) for (int i=(int)(b); i<(int)(e); i++)
#define ALL(x) (x).begin(), (x).end()
const double PI = acos(-1);
class UFT {
int n;
int cnt; // number of connected components
vector<int> par;
vector<int> rank;
vector<int> sz; // size of each component
public:
UFT(int n) : n(n), cnt(n), par(n), rank(n), sz(n) {
for (int i = 0; i < n; i++) {
par[i] = i;
sz[i] = 1;
}
}
int find(int x) {
return par[x] == x ? x : par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
--cnt;
if (rank[x] < rank[y]) {
par[x] = y;
sz[y] += sz[x];
} else {
par[y] = x;
sz[x] += sz[y];
if (rank[x] == rank[y])
++rank[x];
}
}
bool same(int x, int y) {
return find(x) == find(y);
}
int compCnt() {
return cnt;
}
int size(int x) {
return sz[find(x)];
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
UFT tree(7 * n);
vector<string> color(n);
REP (i, n) {
cin >> color[i];
REP (j, 7) {
int k = (j + 1) % 7;
if (color[i][j] == '1' && color[i][k] == '1') {
tree.unite(7*i+j, 7*i+k);
}
}
}
vector<vector<int> > edges(n);
REP (i, m) {
int u, v;
cin >> u >> v;
--u, --v;
edges[u].push_back(v);
edges[v].push_back(u);
REP (j, 7) {
if (color[u][j] == '1' && color[v][j] == '1') {
tree.unite(7*u+j, 7*v+j);
}
}
}
REP (_, q) {
int t, x, y;
cin >> t >> x >> y;
--x, --y;
if (t == 1) {
color[x][y] = '1';
if (color[x][(y+1)%7] == '1')
tree.unite(7*x+y, 7*x+(y+1)%7);
if (color[x][(y+6)%7] == '1')
tree.unite(7*x+y, 7*x+(y+6)%7);
for (int v: edges[x]) {
if (color[v][y] == '1')
tree.unite(7*x+y, 7*v+y);
}
} else {
cout << tree.size(7*x) << endl;
}
}
return 0;
}