結果
| 問題 |
No.416 旅行会社
|
| ユーザー |
HAHAHA
|
| 提出日時 | 2016-09-01 11:59:28 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 288 ms / 4,000 ms |
| コード長 | 1,727 bytes |
| コンパイル時間 | 1,648 ms |
| コンパイル使用メモリ | 179,248 KB |
| 実行使用メモリ | 14,784 KB |
| 最終ジャッジ日時 | 2024-12-14 20:14:20 |
| 合計ジャッジ時間 | 5,401 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:29:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
29 | scanf("%d%d", &A, &B), -- A, -- B;
| ~~~~~^~~~~~~~~~~~~~~~
main.cpp:36:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
36 | scanf("%d%d", &A, &B), -- A, -- B;
| ~~~~~^~~~~~~~~~~~~~~~
ソースコード
#include<bits/stdc++.h>
using namespace std;
// not...
struct UnionFind {
vector<int> data;
void init(int n) { data.assign(n, -1); }
bool unionSet(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 findSet(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 main() {
int N; int M; int Q;
while(~scanf("%d%d%d", &N, &M, &Q)) {
set<pair<int ,int> > edges;
for(int i=0;i<M;i++){
int A; int B;
scanf("%d%d", &A, &B), -- A, -- B;
if(A > B)swap(A, B);
edges.insert(make_pair(A, B));
}
vector<pair<int, int> > queries(Q);
for(int i=0;i<Q;i++){
int A; int B;
scanf("%d%d", &A, &B), -- A, -- B;
if(A > B)swap(A, B);
edges.erase(make_pair(A, B));
queries[i] = make_pair(A, B);
}
UnionFind uf;
uf.init(N);
for(auto e : edges)
uf.unionSet(e.first, e.second);
vector<int> ans(N, 0);
for(int i=0;i<N;i++) if(uf.findSet(i, 0))
ans[i] = -1;
vector<vector<int>> as(N);
for(int i=0;i<N;i++)
as[uf.root(i)].push_back(i);
for(int i = Q - 1; i >= 0; -- i) {
auto e = queries[i];
int x = e.first, y = e.second;
x = uf.root(x), y = uf.root(y);
if(x == y) continue;
if(uf.findSet(0, y))
swap(x, y);
vector<int> &v = as[x], &w = as[y];
if(uf.findSet(0, x)) {
for(int j : w)
ans[j] = i + 1;
}
if(v.size() < w.size())
v.swap(w);
v.insert(v.end(), w.begin(), w.end());
w.clear();
uf.unionSet(x, y);
as[uf.root(x)].swap(v);
}
for(int i = 1; i < N; ++ i)
printf("%d\n", ans[i]);
}
return 0;
}
HAHAHA