結果

問題 No.416 旅行会社
ユーザー xuzijian629xuzijian629
提出日時 2018-10-30 15:52:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 649 ms / 4,000 ms
コード長 2,079 bytes
コンパイル時間 3,458 ms
コンパイル使用メモリ 221,860 KB
実行使用メモリ 24,192 KB
最終ジャッジ日時 2023-08-21 10:21:26
合計ジャッジ時間 10,923 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 299 ms
14,688 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 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,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 8 ms
4,376 KB
testcase_09 AC 34 ms
4,472 KB
testcase_10 AC 326 ms
14,896 KB
testcase_11 AC 324 ms
14,748 KB
testcase_12 AC 325 ms
14,880 KB
testcase_13 AC 290 ms
14,748 KB
testcase_14 AC 649 ms
23,828 KB
testcase_15 AC 632 ms
24,176 KB
testcase_16 AC 627 ms
24,192 KB
testcase_17 AC 648 ms
23,940 KB
testcase_18 AC 641 ms
23,932 KB
testcase_19 AC 457 ms
17,552 KB
testcase_20 AC 459 ms
17,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;

class ppUF {
    vi rank, par, time;
    const int INF = 1e9;
public:
    ppUF(int n) {
        rank.resize(n);
        par.resize(n, -1);
        time.resize(n, INF);
    }
    int find(int t, int x) {
        if (time[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 (rank[x] > rank[y]) {
            par[y] = x;
            time[y] = t;
        } else {
            par[x] = y;
            time[x] = t;
            if (rank[x] == rank[y]) {
                rank[y]++;
            }
        }
        return true;
    }
    
    bool same(int t, int x, int y) {
        return find(t, x) == find(t, y);
    }
};

int main() {
    int n, m, q;
    cin >> n >> m >> q;
    using ii = pair<int, int>;
    map<ii, int> ord;
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        ord[ii(a, b)] = 1e9;
    }
    vector<ii> qs;
    for (int i = 0; i < q; i++) {
        int c, d;
        cin >> c >> d;
        c--, d--;
        qs.push_back(ii(c, d));
    }
    reverse(qs.begin(), qs.end());
    for (int i = 0; i < q; i++) {
        ord[qs[i]] = i + 1;
    }
    
    ppUF uf(n);
    vector<pair<int, ii>> vs;
    for (auto& p: ord) {
        if (p.second == 1e9) {
            uf.unite(0, p.first.first, p.first.second);
        } else {
            vs.push_back(make_pair(p.second, p.first));
        }
    }
    
    sort(vs.begin(), vs.end());
    for (auto& v: vs) {
        uf.unite(v.first, v.second.first, v.second.second);
    }
    
    for (int i = 1; i < n; i++) {
        int l = -1, r = q + 1;
        while (l < r - 1) {
            int mid = (l + r) / 2;
            if (uf.same(mid, 0, i)) {
                r = mid;
            } else {
                l = mid;
            }
        }

        cout << (l == -1 ? -1 : (l == q ? 0 : q - l)) << endl;
    }
}
0