結果

問題 No.416 旅行会社
ユーザー kyuna
提出日時 2019-08-16 23:12:33
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 624 ms / 4,000 ms
コード長 1,702 bytes
コンパイル時間 1,110 ms
コンパイル使用メモリ 91,320 KB
実行使用メモリ 14,832 KB
最終ジャッジ日時 2024-12-14 20:37:39
合計ジャッジ時間 8,227 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
using namespace std;
struct UnionFind {
vector<int> data;
int sz;
vector<vector<int>> mem;
UnionFind(int sz) : data(sz, -1), sz(sz), mem(sz) {
for (int i = 0; i < sz; i++) init(i);
}
bool unionSet(int x, int y) {
if ((x = root(x)) == (y = root(y))) return false;
if (data[x] > data[y]) swap(x, y);
data[x] += data[y]; data[y] = x; sz--;
mem[x].insert(mem[x].end(), mem[y].begin(), mem[y].end());
mem[y].clear();
return true;
}
bool findSet(int x, int y) { return root(x) == root(y); }
vector<int>& operator[](int x) { return mem[root(x)]; }
void init(int x) { int r = root(x); mem[r] = {r}; }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
int size() { return sz; }
};
int main() {
int n, m, q; cin >> n >> m >> q;
set<pair<int, int>> edges;
for (int i = 0; i < m; i++) {
int a, b; cin >> a >> b; a--, b--;
edges.emplace(a, b);
}
vector<pair<int, int>> cd;
for (int i = 0; i < q; i++) {
int c, d; cin >> c >> d; c--, d--;
cd.emplace_back(c, d);
edges.erase({c, d});
}
UnionFind uf(n);
for (auto &p: edges) uf.unionSet(p.first, p.second);
vector<int> res(n, 0);
for (int x: uf[0]) res[x] = -1;
uf[0].clear();
for (int i = q - 1; i >= 0; i--) {
int c = cd[i].first, d = cd[i].second;
uf.unionSet(c, d);
for (int x: uf[0]) res[x] = i + 1;
uf[0].clear();
}
for (int i = 1; i < n; i++) cout << res[i] << endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0