結果

問題 No.416 旅行会社
ユーザー tempura_pptempura_pp
提出日時 2019-06-29 15:11:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,647 ms / 4,000 ms
コード長 2,000 bytes
コンパイル時間 2,041 ms
コンパイル使用メモリ 113,452 KB
実行使用メモリ 19,164 KB
最終ジャッジ日時 2023-08-21 10:26:44
合計ジャッジ時間 24,113 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 701 ms
11,696 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 12 ms
4,380 KB
testcase_09 AC 67 ms
4,384 KB
testcase_10 AC 847 ms
11,676 KB
testcase_11 AC 857 ms
11,636 KB
testcase_12 AC 848 ms
11,668 KB
testcase_13 AC 584 ms
11,672 KB
testcase_14 AC 2,579 ms
19,024 KB
testcase_15 AC 2,594 ms
19,164 KB
testcase_16 AC 2,613 ms
19,084 KB
testcase_17 AC 2,647 ms
19,072 KB
testcase_18 AC 2,457 ms
19,100 KB
testcase_19 AC 1,464 ms
15,504 KB
testcase_20 AC 1,458 ms
15,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;
int dx[4]={1,0,-1,0} , dy[4]={0,1,0,-1} ;
struct ppUF{
    vector<int> ra,par,ti;
    ppUF(int n){
        ra.resize(n,0);
        ti.resize(n,inf+1);
        par.resize(n,-1);
    }
    int find(int t,int x){
        if(ti[x]>t)return x;
        return find(t,par[x]);
    }

    bool unite(int t,int x,int y){
        x=find(t,x);y=find(t,y);
        if(x==y)return false;
        if(ra[x]>ra[y]){
            par[y]=x;
            ti[y]=t;
        }
        else{
            par[x]=y;
            ti[x]=t;
            if(ra[x]==ra[y])ra[y]++;
        }
        return true;
    }

    bool same(int t,int x,int y){
        return find(t,x)==find(t,y);
    }
    int time(int x,int y){
        if(!same(202020,x,y))return -1;
        int ok=202020,ng=-1,mid=0;
        while(ok-ng>1){
            mid=(ok+ng)/2;
            if(same(mid,x,y))ok=mid;
            else ng=mid;
        }
        return ok;
    }
};
int main(){
    int n,m,q;
    cin>>n>>m>>q;
    map<pint,int> mp;
    vector<int> ord;
    rep(i,m)ord.push_back(i);
    pint v[m];
    rep(i,m)cin>>v[i].first>>v[i].second;
    rep(i,q){
        int x,y;
        cin>>x>>y;
        mp[{x,y}]=q-i;
    }
    sort(ord.begin(),ord.end(),[&](int a,int b){return mp[v[a]]<mp[v[b]];});
    ppUF uf(n+1);
    for(auto idx:ord){
        uf.unite(mp[v[idx]], v[idx].first, v[idx].second);
    }
    rep(i,n-1){
        int ret=uf.time(1, i+2);
        if(ret==-1)cout<<0<<endl;
        else if(ret==0)cout<<-1<<endl;
        else cout<<q+1-ret<<endl;
    }

   return 0;
}
0