結果
| 問題 |
No.416 旅行会社
|
| コンテスト | |
| ユーザー |
waidotto
|
| 提出日時 | 2016-08-27 01:13:04 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,969 bytes |
| コンパイル時間 | 2,317 ms |
| コンパイル使用メモリ | 177,436 KB |
| 実行使用メモリ | 15,192 KB |
| 最終ジャッジ日時 | 2024-11-08 18:31:49 |
| 合計ジャッジ時間 | 13,395 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | TLE * 1 -- * 20 |
ソースコード
#include <bits/stdc++.h>
class UnionFindTree {
public:
UnionFindTree(int n) {
rank = std::vector<int>(n, 0);
for(int i = 0; i < n; ++i) {
parent.push_back(i);
}
}
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;
} else {
parent[y] = x;
if(rank[x] == rank[y]) ++rank[x];
}
}
bool same(int x, int y) {
return find(x) == find(y);
}
private:
std::vector<int> parent;
std::vector<int> rank;
};
int main(void) {
int N, M, Q;
std::cin >> N >> M >> Q;
UnionFindTree tree(N + 1);
std::set<std::pair<int, int>> AB;
std::vector<std::pair<int, int>> CD;
std::set<int> unused_nodes;
std::vector<int> answer(N + 1, 0);
for(int i = 0; i < M; ++i) {
int A, B;
std::cin >> A >> B;
AB.insert(std::make_pair(A, B));
}
for(int i = 0; i < Q; ++i) {
int C, D;
std::cin >> C >> D;
CD.push_back(std::make_pair(C, D));
AB.erase(std::make_pair(C, D));
}
for(auto it = AB.begin(); it != AB.end(); ++it) {
tree.unite(it->first, it->second);
}
for(int i = 2; i <= N; ++i) {
unused_nodes.insert(i);
}
for(auto it = unused_nodes.begin(); it != unused_nodes.end(); ) {
if(tree.same(1, *it)) {
answer[*it] = -1;
unused_nodes.erase(it++);
} else {
++it;
}
}
/*for(int j = 2; j <= N; ++j) {
if(tree.same(1, j)) {
answer[j] = -1;
}
}*/
for(int i = Q; 1 <= i; --i) {
tree.unite(CD[i - 1].first, CD[i - 1].second);
for(auto it = unused_nodes.begin(); it != unused_nodes.end(); ) {
if(tree.same(1, *it)) {
answer[*it] = i;
unused_nodes.erase(it++);
} else {
++it;
}
}
/*for(int j = 2; j <= N; ++j) {
if(tree.same(1, j)) {
if(answer[j] == 0) {
answer[j] = i;
}
}
}*/
}
for(int i = 2; i <= N; ++i) {
std::cout << answer[i] << '\n';
}
return 0;
}
waidotto