結果
| 問題 |
No.416 旅行会社
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-08-30 15:30:42 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 593 ms / 4,000 ms |
| コード長 | 2,406 bytes |
| コンパイル時間 | 2,046 ms |
| コンパイル使用メモリ | 187,032 KB |
| 実行使用メモリ | 45,824 KB |
| 最終ジャッジ日時 | 2024-12-14 20:14:14 |
| 合計ジャッジ時間 | 9,154 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 |
ソースコード
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
#define REP(i,a,b) for(i=a;i<b;++i)
#define rep(i,n) REP(i,0,n)
#define ll long long
#define ull unsigned ll
typedef long double ld;
#define ALL(a) (a).begin(),(a).end()
#define ifnot(a) if(not a)
#define dump(x) cerr << #x << " = " << (x) << endl
using namespace std;
// #define int ll
bool test = 0;
int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
#define INF (1 << 28)
ull mod = (int)1e9 + 7;
//.....................
#define MAX (int)1e6 + 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>> alive;
pair<int, int> broken[MAX];
int ans[MAX];
UnionFind uf(MAX);
vector<int> item2group_num(MAX);
vector<vector<int>> group_num2item(MAX);
int main() {
int i, j, k;
cin >> N >> M >> Q;
rep(i, M) {
int a, b;
cin >> a >> b;
a--; b--;
alive.insert({ a,b });
}
rep(i, Q) {
cin >> broken[i].first >> broken[i].second;
broken[i].first--; broken[i].second--;
alive.erase({ broken[i] });
}
{
auto itr = alive.begin();
for (; itr != alive.end(); ++itr) {
uf.unite(itr->first, itr->second);
}
}
rep(i, N) {
item2group_num[i] = uf.root(i);
group_num2item[uf.root(i)].push_back(i);
}
rep(i, N) {
if (uf.same(0, i)) {
ans[i] = -1;
}
}
// O(NQ) ->
for (i = Q - 1; i > -1; --i) {
int x = broken[i].first;
x = uf.root(x);
int y = broken[i].second;
y = uf.root(y);
if (uf.same(x, y))
continue;
// x,yは別グループ
if (uf.same(0, y)) {
swap(x, y);
}
// yは0と同じグループじゃない
if (uf.same(0, x)) {
for (auto j : group_num2item[y]) {
ans[j] = i + 1;
}
}
if (uf.size(x) < uf.size(y)) {
swap(x, y);
}
// uf.size(x) >= uf.size(y)
for (auto j : group_num2item[y]) {
item2group_num[j] = x;
}
group_num2item[x].insert(group_num2item[x].end(),
ALL(group_num2item[y]));
group_num2item[y].clear();
uf.unite(x, y);
}
for (i = 1; i < N; ++i) {
cout << ans[i] << endl;
}
return 0;
}