結果

問題 No.416 旅行会社
ユーザー tempura_pptempura_pp
提出日時 2018-08-14 18:02:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2,597 ms / 4,000 ms
コード長 1,994 bytes
コンパイル時間 1,197 ms
コンパイル使用メモリ 106,932 KB
実行使用メモリ 19,380 KB
最終ジャッジ日時 2023-08-21 10:19:04
合計ジャッジ時間 23,124 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 712 ms
11,668 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 13 ms
4,376 KB
testcase_09 AC 70 ms
4,376 KB
testcase_10 AC 861 ms
11,752 KB
testcase_11 AC 860 ms
11,752 KB
testcase_12 AC 861 ms
11,728 KB
testcase_13 AC 592 ms
11,908 KB
testcase_14 AC 2,597 ms
19,052 KB
testcase_15 AC 2,509 ms
19,224 KB
testcase_16 AC 2,508 ms
19,380 KB
testcase_17 AC 2,463 ms
19,112 KB
testcase_18 AC 2,476 ms
19,228 KB
testcase_19 AC 1,487 ms
15,532 KB
testcase_20 AC 1,513 ms
15,460 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(inf,x,y))return -1;
        int ok=inf,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