結果

問題 No.416 旅行会社
ユーザー maimai
提出日時 2016-09-04 12:07:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,878 bytes
コンパイル時間 2,125 ms
コンパイル使用メモリ 192,072 KB
実行使用メモリ 78,732 KB
最終ジャッジ日時 2024-04-27 16:27:43
合計ジャッジ時間 8,698 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 160 ms
74,168 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 199 ms
74,256 KB
testcase_11 AC 210 ms
73,396 KB
testcase_12 AC 191 ms
73,384 KB
testcase_13 AC 157 ms
73,396 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

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 UnionFind {
    vector<int> data;
    UnionFind(int size) : data(size, -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)];
    }
};

struct QuickFind{
    vector<int> toGroup;
    vector<deque<int>> toItems;
    QuickFind(int size){
        toGroup.resize(size);
        toItems.resize(size);
        for (int i=0;i<size;i++){
            toGroup[i] = i;
            toItems[i].emplace_back(i);
        }
    }
    bool unionSet(int x,int y){
        bool r = toGroup[x] != toGroup[y];
        
        if (r){
            if (toItems[toGroup[x]].size() <= toItems[toGroup[y]].size()){
                for (auto e:toItems[toGroup[x]]){
                    toGroup[e]=toGroup[y];
                }
                toItems[toGroup[y]].insert(toItems[toGroup[y]].end(),ALL(toItems[x]));
            }else{
                for (auto e:toItems[toGroup[y]]){
                    toGroup[e]=toGroup[x];
                }
                toItems[toGroup[x]].insert(toItems[toGroup[x]].end(),ALL(toItems[y]));
            }
        }
        return r;
    }
    bool findSet(int x,int y){
        return toGroup[x] == toGroup[y];
    }
};

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);
    QuickFind qf(n+1);
    
    // 残る橋からunionfindを構築
    for (auto e:alive){
        qf.unionSet(e.first,e.second);
    }
    
    
    for (auto e:qf.toItems[qf.toGroup[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.toGroup[b]]){
                    order[e] = i + 1;
                }
            }else{
                for (auto e:qf.toItems[qf.toGroup[a]]){
                    order[e] = i + 1;
                }
            }
        }
        qf.unionSet(a,b);
    }
    
    for (i=2;i<=n;i++){
        printf("%d\n",order[i]);
    }

    return 0;
}
0