結果
| 問題 |
No.416 旅行会社
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-10-30 13:28:17 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 587 ms / 4,000 ms |
| コード長 | 2,051 bytes |
| コンパイル時間 | 2,271 ms |
| コンパイル使用メモリ | 189,288 KB |
| 実行使用メモリ | 20,224 KB |
| 最終ジャッジ日時 | 2024-12-14 20:20:07 |
| 合計ジャッジ時間 | 9,043 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 |
ソースコード
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
#define REP(i,a,b) for(int i=a;i<b;++i)
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define ALL(a) begin(a),end(a)
#define ifnot(a) if(not (a))
#define dump(x) cerr << #x << " = " << (x) << endl
using namespace std;
// #define int ll
#ifdef _MSC_VER
const bool test = true;
#else
const bool test = false;
#endif
int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
#define INF (1 << 28)
ull mod = (int)1e9 + 7;
//.....................
const int MAX = (int)2e5 + 5;
struct UnionFind {
vector<int> data;
UnionFind(int size) : data(size, -1) { }
bool unite(int x, int y) {
x = root(x); y = root(y);
if (x != y) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y]; data[y] = x;
}
return x != y;
}
bool same(int x, int y) {
return root(x) == root(y);
}
int root(int x) {
return data[x] < 0 ? x : data[x] = root(data[x]);
}
int size(int x) {
return -data[root(x)];
}
};
int N, M, Q;
set<pair<int, int>> bridge;
pair<int, int> broken[MAX];
int ans[MAX];
UnionFind uf(MAX);
vector<vector<int>> g2i(MAX);
int main() {
int i, j, k;
cin >> N >> M >> Q;
rep(i, M) {
int a, b;
cin >> a >> b;
a--; b--;
bridge.insert({ a,b });
}
rep(i, Q) {
int c, d;
cin >> c >> d;
c--; d--;
broken[i] = { c,d };
}
for (auto c : broken) {
bridge.erase(c);
}
auto alive = bridge;
for (auto c : alive) {
uf.unite(c.first, c.second);
}
rep(i, N) {
g2i[uf.root(i)].push_back(i);
}
rep(i, N) {
if (uf.same(i, 0)) {
ans[i] = -1;
}
}
for (i = Q - 1; i > -1; i--) {
int x = uf.root(broken[i].first);
int y = uf.root(broken[i].second);
if (uf.same(x, y)) continue;
if (uf.same(0, y)) swap(x, y);
if (uf.same(0, x)) {
for (auto v : g2i[y]) {
ans[v] = i + 1; // ?
}
}
if (uf.size(x) < uf.size(y)) swap(x, y);
g2i[x].insert(g2i[x].end(), ALL(g2i[y]));
uf.unite(x, y);
}
REP(i, 1, N) {
cout << ans[i] << endl;
}
return 0;
}