結果

問題 No.416 旅行会社
ユーザー maimai
提出日時 2016-09-04 17:54:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 372 ms / 4,000 ms
コード長 3,115 bytes
コンパイル時間 2,383 ms
コンパイル使用メモリ 189,660 KB
実行使用メモリ 77,700 KB
最終ジャッジ日時 2023-08-21 10:04:43
合計ジャッジ時間 7,567 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 149 ms
72,948 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 5 ms
4,768 KB
testcase_09 AC 19 ms
10,636 KB
testcase_10 AC 161 ms
73,128 KB
testcase_11 AC 165 ms
73,204 KB
testcase_12 AC 166 ms
73,328 KB
testcase_13 AC 145 ms
73,132 KB
testcase_14 AC 363 ms
74,720 KB
testcase_15 AC 355 ms
74,788 KB
testcase_16 AC 356 ms
74,832 KB
testcase_17 AC 359 ms
74,836 KB
testcase_18 AC 372 ms
74,788 KB
testcase_19 AC 267 ms
77,632 KB
testcase_20 AC 266 ms
77,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0