結果

問題 No.416 旅行会社
ユーザー hedwig100hedwig100
提出日時 2020-08-17 14:01:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 316 ms / 4,000 ms
コード長 2,130 bytes
コンパイル時間 2,686 ms
コンパイル使用メモリ 177,696 KB
実行使用メモリ 23,484 KB
最終ジャッジ日時 2023-08-21 10:37:28
合計ジャッジ時間 6,231 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
16,248 KB
testcase_01 AC 4 ms
6,128 KB
testcase_02 AC 4 ms
6,140 KB
testcase_03 AC 3 ms
5,900 KB
testcase_04 AC 4 ms
6,100 KB
testcase_05 AC 4 ms
5,960 KB
testcase_06 AC 4 ms
6,036 KB
testcase_07 AC 4 ms
6,140 KB
testcase_08 AC 5 ms
6,328 KB
testcase_09 AC 14 ms
7,120 KB
testcase_10 AC 110 ms
16,220 KB
testcase_11 AC 111 ms
16,552 KB
testcase_12 AC 118 ms
16,604 KB
testcase_13 AC 89 ms
16,548 KB
testcase_14 AC 316 ms
23,148 KB
testcase_15 AC 296 ms
23,484 KB
testcase_16 AC 304 ms
23,404 KB
testcase_17 AC 303 ms
23,220 KB
testcase_18 AC 297 ms
23,284 KB
testcase_19 AC 175 ms
16,880 KB
testcase_20 AC 174 ms
16,556 KB
権限があれば一括ダウンロードができます

ソースコード

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;
    }
}

bool same(int i,int j) {return find(i) == find(j);}


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) {
        if (same(i,0)) cout << when[i] << '\n';
        else cout << 0 << '\n';
    }
    return 0;
}
0