結果
| 問題 |
No.416 旅行会社
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-08-26 23:26:46 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,435 bytes |
| コンパイル時間 | 2,081 ms |
| コンパイル使用メモリ | 179,216 KB |
| 実行使用メモリ | 19,620 KB |
| 最終ジャッジ日時 | 2024-11-08 09:53:09 |
| 合計ジャッジ時間 | 12,844 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | TLE * 1 -- * 20 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
struct Union_Find {
//各要素が属する集合の代表(根)を管理する
//もし、要素xが根であればdata[x]は負の値を取り、-data[x]はxが属する集合の大きさに等しい
vector<int> data;
Union_Find(int size) : data(size, -1) {}
bool Union(int x, int y) {
x = Find(x);
y = Find(y);
bool is_union = (x != y);
if (is_union) {
if (data[x] > data[y]) swap(x, y);
data[x] += data[y];
data[y] = x;
}
return is_union;
}
int Find(int x) {
if (data[x] < 0) { //要素xが根である
return x;
} else {
data[x] = Find(data[x]); //data[x]がxの属する集合の根でない場合、根になるよう更新される
return data[x];
}
}
bool same(int x, int y) {
return Find(x) == Find(y);
}
int size(int x) {
return -data[Find(x)];
}
};
void setter(vector<int>& ans, int z, int v, int n, Union_Find& uf, vector<bool>& memo) {
for (int i = 1; i < n; i++) {
if (memo[i]) continue;
if (uf.same(i, z)) {
ans[i] = v;
memo[i] = true;
}
}
}
int main() {
int n, m, q;
scanf("%d %d %d", &n, &m, &q);
vector<P> e;
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d %d", &a, &b);
a--;
b--;
e.emplace_back(a, b);
}
vector<P> query;
for (int i = 0; i < q; i++) {
int c, d;
scanf("%d %d", &c, &d);
c--;
d--;
query.emplace_back(c, d);
e.erase(find(e.begin(), e.end(), P(c, d)));
}
Union_Find uf(n);
for (auto i = e.begin(), f = e.end(); i != f; ++i) {
uf.Union((*i).first, (*i).second);
}
vector<int> ans(n, -1);
for (int i = 1; i < n; i++) {
if (!uf.same(0, i)) ans[i] = 0;
}
vector<bool> memo(n, false);
memo[0] = true;
for (int i = q - 1; i >= 0; i--) {
int x = query[i].first, y = query[i].second;
if (uf.same(x, y)) continue;
if (uf.same(0, x)) setter(ans, y, i + 1, n, uf, memo);
else if (uf.same(0, y)) setter(ans, x, i + 1, n, uf, memo);
uf.Union(x, y);
}
for (int i = 1; i < n; i++) {
printf("%d\n", ans[i]);
}
return 0;
}