結果

問題 No.416 旅行会社
ユーザー TAISA_TAISA_
提出日時 2020-04-19 16:29:28
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 313 ms / 4,000 ms
コード長 2,376 bytes
コンパイル時間 3,234 ms
コンパイル使用メモリ 211,940 KB
実行使用メモリ 17,564 KB
最終ジャッジ日時 2023-08-21 10:33:48
合計ジャッジ時間 7,492 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
10,956 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 13 ms
4,416 KB
testcase_10 AC 98 ms
10,784 KB
testcase_11 AC 99 ms
12,996 KB
testcase_12 AC 101 ms
12,760 KB
testcase_13 AC 86 ms
10,876 KB
testcase_14 AC 306 ms
17,536 KB
testcase_15 AC 310 ms
17,564 KB
testcase_16 AC 312 ms
17,476 KB
testcase_17 AC 311 ms
17,484 KB
testcase_18 AC 313 ms
17,500 KB
testcase_19 AC 205 ms
17,112 KB
testcase_20 AC 211 ms
17,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
#define pb push_back
#define eb emplace_back
#define fs first
#define sc second
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T>
using V = vector<T>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll MOD = 998244353LL;
constexpr int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
template <class T>
void chmin(T &a, T b) { a = min(a, b); }
template <class T>
void chmax(T &a, T b) { a = max(a, b); }
void debug() { cerr << "ok" << endl; }
template <class T>
void printv(const vector<T> &v) {
    for (int i = 0; i < v.size(); i++) cout << v[i] << (i + 1 == v.size() ? '\n' : ' ');
}
struct QuickFind {
    int n;
    vector<vector<int>> vs;
    vector<int> root;
    QuickFind(int n) : n(n), vs(n), root(n) {
        iota(root.begin(), root.end(), 0);
        for (int i = 0; i < n; i++) {
            vs[i].emplace_back(i);
        }
    }
    inline int size(int u) {
        return vs[root[u]].size();
    }
    bool unite(int u, int v) {
        u = root[u], v = root[v];
        if (u == v) return false;
        if (size(u) < size(v)) swap(u, v);
        for (auto &e : vs[v]) {
            vs[u].emplace_back(e);
            root[e] = u;
        }
        return true;
    }
    inline int same(int u, int v) {
        return root[u] == root[v];
    }
    vector<int> &elements(int u) {
        return vs[root[u]];
    }
};
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, m, q;
    cin >> n >> m >> q;
    set<P> st;
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        --a;
        --b;
        st.insert(P(a, b));
    }
    V<int> c(q), d(q), res(n);
    for (int i = 0; i < q; i++) {
        cin >> c[i] >> d[i];
        --c[i];
        --d[i];
        st.erase(P(c[i], d[i]));
    }
    QuickFind uf(n);
    for (auto &e : st) {
        uf.unite(e.first, e.second);
    }
    for (auto &e : uf.elements(0)) {
        res[e] = -1;
    }
    for (int i = q - 1; i >= 0; i--) {
        if (uf.same(c[i], d[i])) continue;
        if (!uf.same(c[i], 0)) swap(c[i], d[i]);
        if (uf.same(c[i], 0)) {
            for (auto &e : uf.elements(d[i])) {
                res[e] = i + 1;
            }
        }
        uf.unite(c[i], d[i]);
    }
    for (int i = 1; i < n; i++) {
        cout << res[i] << '\n';
    }
}
0