結果

問題 No.416 旅行会社
ユーザー hedwig100hedwig100
提出日時 2020-08-17 13:59:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,014 bytes
コンパイル時間 2,058 ms
コンパイル使用メモリ 175,816 KB
実行使用メモリ 23,724 KB
最終ジャッジ日時 2024-04-19 18:49:53
合計ジャッジ時間 6,328 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
16,352 KB
testcase_01 AC 4 ms
6,944 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 97 ms
16,320 KB
testcase_11 AC 103 ms
16,964 KB
testcase_12 AC 106 ms
16,832 KB
testcase_13 AC 84 ms
16,956 KB
testcase_14 AC 250 ms
23,168 KB
testcase_15 AC 236 ms
23,488 KB
testcase_16 AC 238 ms
23,724 KB
testcase_17 AC 259 ms
23,508 KB
testcase_18 AC 236 ms
23,424 KB
testcase_19 WA -
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i ++)
#define irep(i,n) for (int i = (int)(n) - 1;i >= 0;--i)
using namespace std;
using ll = long long;
using PL = pair<ll,ll>;
using P = pair<int,int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007;// = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;

vector<int> parents(100010,-1);
vector<vector<int>> child(100010);
vector<int> when(100010,-1);
int t = -1;

int find(int i) {
    if (parents[i] < 0) return i;
    return parents[i] = find(parents[i]);
}

void unite(int x,int y) {
    x = find(x); y = find(y);
    if (x == y) return;
    if (x == 0) {
        for (int c:child[y]) {
            when[c] = t;
            child[0].push_back(c);
        }
        parents[x] += parents[y];
        parents[y] = x;
    } else if (y == 0) {
        for (int c:child[x]) {
            when[c] = t;
            child[0].push_back(c);
        }
        parents[y] += parents[x];
        parents[x] = y;
    } else {
        if (parents[x] > parents[y]) swap(x,y);
        for (int c:child[y]) {
            child[x].push_back(c);
        }
        parents[x] += parents[y];
        parents[y] = x;
    }
}


int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N,M,Q; cin >> N >> M >> Q;

    rep(i,N) child[i].push_back(i);
    vector<P> bridge(M),broken(Q);
    set<P> st;
    rep(i,M) {
        cin >> bridge[i].first >> bridge[i].second;
        bridge[i].first--; bridge[i].second--;
    }
    rep(i,Q) {
        cin >> broken[i].first >> broken[i].second;
        broken[i].first--; broken[i].second--;
        st.insert(broken[i]);
    }
    
    rep(i,M) {
        if (!st.count(bridge[i])) unite(bridge[i].first,bridge[i].second);
    }
    
    t = Q;
    for (;t >= 1;--t) {
        unite(broken[t - 1].first,broken[t - 1].second);
    }

    for (int i = 1;i < N;++i) cout << when[i] << '\n';
    return 0;
}
0