結果
| 問題 | No.1266 7 Colors |
| コンテスト | |
| ユーザー |
siman
|
| 提出日時 | 2021-03-22 18:09:42 |
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 364 ms / 3,000 ms |
| コード長 | 2,702 bytes |
| コンパイル時間 | 3,268 ms |
| コンパイル使用メモリ | 140,088 KB |
| 実行使用メモリ | 21,124 KB |
| 最終ジャッジ日時 | 2024-11-24 05:03:02 |
| 合計ジャッジ時間 | 11,742 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
const int MAX_V = 100001;
const int MAX_N = 1000000;
vector<int> parent;
vector<int> _rank;
vector<int> _size;
class UnionFind {
public:
UnionFind(int n) {
for (int i = 0; i < n; ++i) {
parent.push_back(i);
_rank.push_back(0);
_size.push_back(1);
}
}
int find(int x) {
if (parent[x] == x) {
return x;
} else {
return parent[x] = find(parent[x]);
}
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
if (_rank[x] < _rank[y]) {
parent[x] = y;
_size[y] += _size[x];
} else {
parent[y] = x;
_size[x] += _size[y];
if (_rank[x] == _rank[y]) ++_rank[x];
}
}
bool same(int x, int y) {
return find(x) == find(y);
}
int size(int x) {
return _size[find(x)];
}
};
int calcId(int v, int color) {
return MAX_V * color + v;
}
int S[MAX_V];
vector<int> E[MAX_V];
UnionFind uf(MAX_N);
void connect(int u, int v) {
for (int c = 0; c < 7; ++c) {
if ((S[u] >> c & 1) == 0) continue;
if ((S[v] >> c & 1) == 0) continue;
int id1 = calcId(u, c);
int id2 = calcId(v, c);
uf.unite(id1, id2);
}
}
int main() {
int N, M, Q;
cin >> N >> M >> Q;
for (int i = 0; i < N; ++i) {
string s;
cin >> s;
int mask = 0;
for (int c = 0; c < 7; ++c) {
if (s[c] == '0') continue;
mask |= (1 << c);
int id = calcId(i, c);
int bc = (c + 6) % 7;
int ac = (c + 1) % 7;
if (s[bc] == '1') {
int bid = calcId(i, bc);
uf.unite(id, bid);
}
if (s[ac] == '1') {
int aid = calcId(i, ac);
uf.unite(id, aid);
}
}
S[i] = mask;
}
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);
connect(u, v);
}
int t, x, y;
for (int i = 0; i < Q; ++i) {
cin >> t >> x >> y;
--x;
if (t == 1) {
int c = y - 1;
int id = calcId(x, c);
S[x] |= 1 << c;
int bc = (c + 6) % 7;
int ac = (c + 1) % 7;
if (S[x] >> bc & 1) {
int bid = calcId(x, bc);
uf.unite(id, bid);
}
if (S[x] >> ac & 1) {
int aid = calcId(x, ac);
uf.unite(id, aid);
}
for (int i = 0; i < E[x].size(); ++i) {
int z = E[x][i];
connect(x, z);
}
} else {
int id = calcId(x, 0);
cout << uf.size(id) << endl;
}
}
return 0;
}
siman