結果

問題 No.416 旅行会社
ユーザー ShibuyapShibuyap
提出日時 2020-07-26 17:49:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 893 ms / 4,000 ms
コード長 2,444 bytes
コンパイル時間 3,588 ms
コンパイル使用メモリ 213,456 KB
実行使用メモリ 38,816 KB
最終ジャッジ日時 2023-08-21 10:37:20
合計ジャッジ時間 11,894 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 341 ms
23,168 KB
testcase_01 AC 4 ms
5,732 KB
testcase_02 AC 4 ms
5,744 KB
testcase_03 AC 4 ms
5,736 KB
testcase_04 AC 4 ms
5,864 KB
testcase_05 AC 4 ms
5,864 KB
testcase_06 AC 5 ms
5,756 KB
testcase_07 AC 6 ms
5,856 KB
testcase_08 AC 12 ms
6,172 KB
testcase_09 AC 44 ms
7,568 KB
testcase_10 AC 363 ms
23,456 KB
testcase_11 AC 352 ms
22,516 KB
testcase_12 AC 358 ms
22,684 KB
testcase_13 AC 328 ms
22,528 KB
testcase_14 AC 824 ms
38,680 KB
testcase_15 AC 868 ms
38,644 KB
testcase_16 AC 835 ms
38,516 KB
testcase_17 AC 893 ms
38,700 KB
testcase_18 AC 872 ms
38,816 KB
testcase_19 AC 535 ms
26,072 KB
testcase_20 AC 528 ms
25,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}

const int MAX_N = 200100;
int par[MAX_N]; // 親
int rank_[MAX_N]; // 木の深さ
int cnt_[MAX_N]; // 属する頂点の個数(親のみ正しい)

// n要素で初期化
void UFinit(){
    for(int i=0;i<MAX_N;i++){
        par[i] = i;
        rank_[i] = 0;
        cnt_[i] = 1;
    }
}

// 木の根を求める
int find(int x){
    if(par[x] == x){
        return x;
    }else{
        return par[x] = find(par[x]);
    }
}

// xとyの属する集合を併合
void unite(int x, int y){
    x = find(x);
    y = find(y);
    if(x == y) return;

    if(rank_[x] < rank_[y]){
        par[x] = y;
        cnt_[y] += cnt_[x];
    }else{
        par[y] = x;
        cnt_[x] += cnt_[y];
        if(rank_[x] == rank_[y]) rank_[x]++;
    }
}

// xとyが同じ集合に属するか否か
bool same(int x, int y){
    return find(x) == find(y);
}


int main() {
    UFinit();
    int n, m, q;
    cin >> n >> m >> q;
    int a[m], b[m], c[q], d[q];
    rep(i,m){
        cin >> a[i] >> b[i];
        a[i]--; b[i]--;
    }
    rep(i,q){
        cin >> c[i] >> d[i];
        c[i]--; d[i]--;
    }
    map<P,int> mp;
    rep(i,m)mp[P(a[i],b[i])] = q + 1;
    rep(i,q)mp[P(c[i],d[i])] = i + 1;
    vector<P> G[q+2];
    for(auto x : mp) G[x.second].push_back(x.first);
    int l[n] = {}, r[n];
    rep(i,n){
        r[i] = q+1;
    }
    rep(_,30){
        vector<int> v[q+2];
        int cnt = 0;
        rep(i,n){
            if(l[i]==r[i])continue;
            int mid = (l[i]+r[i]) / 2 + 1;
            v[mid].push_back(i);
            cnt++;
        }
        if(cnt==0)break;
        UFinit();
        int ite = 0;
        drep(i,q+2){
            rep(j,G[i].size()){
                unite(G[i][j].first, G[i][j].second);
            }
            rep(j,v[i].size()){
                if(same(v[i][j], 0)){
                    l[v[i][j]] = i;
                }else{
                    r[v[i][j]] = i-1;
                }
            }
        }
    }
    srep(i,1,n){
        if(l[i]==0){
            cout << 0 << endl;
        }else if(l[i]==q+1){
            cout << -1 << endl;
        }else{
            cout << l[i] << endl;
        }
    }
    return 0;
}

0