結果

問題 No.416 旅行会社
ユーザー chakkuchakku
提出日時 2016-09-03 21:34:27
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 620 ms / 4,000 ms
コード長 2,799 bytes
コンパイル時間 2,417 ms
コンパイル使用メモリ 165,376 KB
実行使用メモリ 15,212 KB
最終ジャッジ日時 2023-08-21 10:04:53
合計ジャッジ時間 9,498 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 277 ms
11,060 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 8 ms
4,376 KB
testcase_09 AC 32 ms
4,380 KB
testcase_10 AC 307 ms
11,184 KB
testcase_11 AC 300 ms
11,340 KB
testcase_12 AC 307 ms
11,304 KB
testcase_13 AC 273 ms
11,100 KB
testcase_14 AC 602 ms
13,880 KB
testcase_15 AC 620 ms
13,864 KB
testcase_16 AC 610 ms
14,188 KB
testcase_17 AC 618 ms
13,868 KB
testcase_18 AC 616 ms
13,924 KB
testcase_19 AC 447 ms
15,104 KB
testcase_20 AC 456 ms
15,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<29
#define LINF LLONG_MAX/3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(),(v).end()
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

struct UnionFind{/*{{{*/
    vector<int> rank,parent;
    vector<vector<int>> groupe;
    UnionFind(int n) : rank(n) , parent(n) , groupe(n)
    {
        rank.assign(n,0);
        parent.assign(n,0);
        for(int i=0;i<n;i++){
            parent[i]=i;
            rank[i]=0;
            groupe[i].push_back(i);
        }
    }

    int find(int x){
        if(parent[x]==x) return x;
        else return parent[x]=find(parent[x]);
    }

    void unite(int x,int y){
        x=find(x);
        y=find(y);
        if(x==y) return;
        if(rank[x]<rank[y]){
            parent[x]=y;
            groupe[y].insert(groupe[y].end(),groupe[x].begin(),groupe[x].end());
            groupe[x].clear();
        }else{
            parent[y]=x;
            groupe[x].insert(groupe[x].end(),groupe[y].begin(),groupe[y].end());
            groupe[y].clear();
            if(rank[x]==rank[y]) rank[x]++;
        }
    }

    bool same(int x,int y){
        return find(x)==find(y);
    }

    void show(){
        for(int i=0;i<groupe.size();i++){
            cout << i << " : " << groupe[i] << endl;
        }
    }
};/*}}}*/

int N,M,Q;
set<pii> g;
int C[200001],D[200001];

int main(){
    cin>>N>>M>>Q;
    rep(i,M){
        int a,b;cin>>a>>b;
        if(a>b) swap(a,b);
        g.insert(MP(a-1,b-1));
    }
    rep(i,Q){
        cin>>C[i]>>D[i];
        C[i]--,D[i]--;
        if(C[i]>D[i]) swap(C[i],D[i]);
        g.erase(MP(C[i],D[i]));
    }

    UnionFind uf(N);
    for(auto t:g){
        int u=t.first;
        int v=t.second;
        uf.unite(u,v);
    }

    vector<int> ans(N,0);
    for(int i=0;i<N;i++){
        if(uf.same(0,i)) ans[i]=-1;
    }

    for(int i=Q-1;i>=0;i--){
        int u=C[i],v=D[i];
        u=uf.find(u);
        v=uf.find(v);
        if(u>v) swap(u,v);
        if(uf.same(0,v)) swap(u,v);

        if(!uf.same(0,u)){
            uf.unite(u,v);
        }else{
            if(uf.same(0,v)) continue;
            for(int j=0;j<uf.groupe[v].size();j++){
                ans[uf.groupe[v][j]]=i+1;
            }
            uf.unite(u,v);
        }
    }

    for(int i=1;i<N;i++){
        cout << ans[i] << endl;
    }
}
0