結果
| 問題 |
No.1266 7 Colors
|
| コンテスト | |
| ユーザー |
SSRS
|
| 提出日時 | 2020-10-23 22:25:15 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 369 ms / 3,000 ms |
| コード長 | 1,962 bytes |
| コンパイル時間 | 1,826 ms |
| コンパイル使用メモリ | 175,440 KB |
| 実行使用メモリ | 18,864 KB |
| 最終ジャッジ日時 | 2024-07-21 11:08:07 |
| 合計ジャッジ時間 | 9,230 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
struct unionfind{
vector<int> p, sz;
unionfind(int N){
p = vector<int>(N, -1);
sz = vector<int>(N, 1);
}
int root(int x){
if (p[x] == -1){
return x;
} else {
p[x] = root(p[x]);
return p[x];
}
}
bool same(int x, int y){
return root(x) == root(y);
}
void unite(int x, int y){
x = root(x);
y = root(y);
if (x != y){
p[x] = y;
sz[y] += sz[x];
}
}
int size(int x){
return sz[root(x)];
}
};
int next(int x){
return (x + 1) % 7;
}
int prev(int x){
return (x + 6) % 7;
}
int main(){
int N, M, Q;
cin >> N >> M >> Q;
vector<string> s(N);
for (int i = 0; i < N; i++){
cin >> s[i];
}
vector<vector<int>> E(N);
for (int i = 0; i < M; i++){
int u, v;
cin >> u >> v;
u--;
v--;
E[u].push_back(v);
E[v].push_back(u);
}
unionfind UF(N * 7);
for (int i = 0; i < N; i++){
for (int j = 0; j < 7; j++){
if (s[i][j] == '1' && s[i][next(j)] == '1'){
int u = i * 7 + j;
int v = i * 7 + next(j);
UF.unite(u, v);
}
}
}
for (int i = 0; i < N; i++){
for (int j : E[i]){
for (int k = 0; k < 7; k++){
if (s[i][k] == '1' && s[j][k] == '1'){
UF.unite(i * 7 + k, j * 7 + k);
}
}
}
}
for (int i = 0; i < Q; i++){
int t, x, y;
cin >> t >> x >> y;
if (t == 1){
x--;
y--;
s[x][y] = '1';
if (s[x][prev(y)] == '1'){
int u = x * 7 + prev(y);
int v = x * 7 + y;
UF.unite(u, v);
}
if (s[x][next(y)] == '1'){
int u = x * 7 + y;
int v = x * 7 + next(y);
UF.unite(u, v);
}
for (int j : E[x]){
if (s[j][y] == '1'){
int u = x * 7 + y;
int v = j * 7 + y;
UF.unite(u, v);
}
}
}
if (t == 2){
x--;
cout << UF.size(x * 7) << endl;
}
}
}
SSRS