結果
| 問題 |
No.416 旅行会社
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-09-04 17:54:03 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 387 ms / 4,000 ms |
| コード長 | 3,115 bytes |
| コンパイル時間 | 2,494 ms |
| コンパイル使用メモリ | 193,880 KB |
| 実行使用メモリ | 78,824 KB |
| 最終ジャッジ日時 | 2024-12-14 20:16:24 |
| 合計ジャッジ時間 | 7,410 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
typedef unsigned int uint;
typedef long long int ll;
typedef unsigned long long int ull;
#define debugv(v) printf("L%d %s => ",__LINE__,#v);for(auto e:v){cout<<e<<" ";}cout<<endl;
#define debugm(m) printf("L%d %s is..\n",__LINE__,#m);for(auto v:m){for(auto e:v){cout<<e<<" ";}cout<<endl;}
#define debuga(m,w) printf("L%d %s is => ",__LINE__,#m);for(int x=0;x<(w);x++){cout<<(m)[x]<<" ";}cout<<endl;
#define debugaa(m,w,h) printf("L%d %s is..\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){cout<<(m)[x][y]<<" ";}cout<<endl;}
#define ALL(v) (v).begin(),(v).end()
#define BIGINT 0x7FFFFFFF
#define E107 1000000007
#define TIME chrono::system_clock::now
#define MILLISEC(t) (chrono::duration_cast<chrono::milliseconds>(t).count())
template<typename T1,typename T2>
ostream& operator <<(ostream &o,const pair<T1,T2> p){o<<"("<<p.first<<":"<<p.second<<")";return o;}
struct QunionFind{
vector<deque<int>> toItems;
vector<int> data;
QunionFind(int size) : data(size, -1) {
toItems.resize(size);
for (int i=0;i<size;i++){
toItems[i].emplace_back(i);
}
}
bool unionSet(int x, int y) {
x = root(x); y = root(y);
if (x != y) {
if (data[y] < data[x]) swap(x, y);
toItems[x].insert(toItems[x].end(),ALL(toItems[y]));
data[x] += data[y]; data[y] = x;
}
return x != y;
}
inline bool findSet(int x, int y) {
return root(x) == root(y);
}
inline int root(int x) {
return data[x] < 0 ? x : data[x] = root(data[x]);
}
inline int size(int x) {
return -data[root(x)];
}
};
int m,n,q;
set<pair<int,int>> alive;
pair<int,int> bridge[200005];
pair<int,int> destroy[200005];
int order[200005];
int main(){
int i,j,k,l;
int a,b,c,d;
cin>>n>>m>>q;
// load
for (i=0;i<m;i++){
scanf("%d%d",&a,&b);
bridge[i].first = a; // a < b
bridge[i].second = b;
alive.emplace(a,b);
}
// 残る橋を求める
for (i=0;i<q;i++){
scanf("%d%d",&a,&b);
destroy[i].first = a;
destroy[i].second = b;
alive.erase(make_pair(a,b));
}
// UnionFind uf(n+1);
QunionFind qf(n+1);
// 残る橋からunionfindを構築
for (auto e:alive){
qf.unionSet(e.first,e.second);
}
for (auto e:qf.toItems[qf.root(1)]){
order[e] = -1;
}
// 最後に消した橋を追加する、その次の…
for (i=q-1;0<=i;i--){
a = destroy[i].first;
b = destroy[i].second;
if (qf.findSet(1,a)^qf.findSet(1,b)){
if (qf.findSet(1,a)){
for (auto e:qf.toItems[qf.root(b)]){
order[e] = i + 1;
}
}else{
for (auto e:qf.toItems[qf.root(a)]){
order[e] = i + 1;
}
}
}
qf.unionSet(a,b);
}
for (i=2;i<=n;i++){
printf("%d\n",order[i]);
}
return 0;
}