結果

問題 No.416 旅行会社
ユーザー rapurasurapurasu
提出日時 2016-08-27 00:09:01
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,238 bytes
コンパイル時間 1,596 ms
コンパイル使用メモリ 173,896 KB
実行使用メモリ 20,116 KB
最終ジャッジ日時 2024-04-26 04:15:26
合計ジャッジ時間 12,414 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)

int par1[200001];
int rank1[200001];
//n要素で初期化
void init(int n){
   for(int i=0;i<n;i++){
       par1[i]=i;
       rank1[i]=0;
   }
}
//木の根を求める
int find(int x){
    if(par1[x] ==x){
         return x;
    }else{
         return par1[x]=find(par1[x]);
    }
}
//xとyの属する集合を併合
void unite(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y) return;

    if(rank1[x]<rank1[y]){
         par1[x]=y;
    }else{
         par1[y]=x;
         rank1[x]++;
    }
}
//xとyが同じ集合に属するか否か
bool same(int x,int y){
     return find(x)==find(y);
}

int ans[200001];
int N,M,Q;
typedef pair<int,int>P;
vector<P>v;
vector<P>v2;
vector<P>b2;
map<P,int>m;
int main(){
        REP(i,200001){
            ans[i]=0;
        }
	init(200001);
	cin>>N>>M>>Q;
        REP(i,M){
            int a,b;
            cin>>a>>b;
            v.push_back(P(a-1,b-1));
        }
        REP(i,Q){
            int c,d;
            cin>>c>>d;
            m[P(c-1,d-1)]=0;
            b2.push_back(P(c-1,d-1));
        }
        REP(i,v.size()){
            if(m.find(v[i])==m.end()){
               v2.push_back(v[i]);
            }
        }

        REP(i,v2.size()){
            unite(v2[i].first,v2[i].second);
        }
        REP(i,N){
            if(same(0,i)){
               ans[i]=-1;
            }
        }
        long long count=Q;
        for(int i=b2.size()-1;i>=0;i--){
            P p=b2[i];
            unite(p.first,p.second);
            if(same(p.first,0)&&ans[p.first]==0){
               REP(i,N){
                   if(same(i,0)&&ans[i]==0){
                       ans[i]=count;
                   }
               }

            }
            if(same(p.second,0)&&ans[p.second]==0){
               REP(i,N){
                 if(same(i,0)&&ans[i]==0){
                     ans[i]=count;
                 }
               }
            }
            count--;
        }
        for(int i=1;i<N;i++){
            cout<<ans[i]<<endl;
        }
	return(0);
}
0