結果

問題 No.416 旅行会社
ユーザー snrnsidysnrnsidy
提出日時 2021-04-25 17:00:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 291 ms / 4,000 ms
コード長 2,263 bytes
コンパイル時間 1,360 ms
コンパイル使用メモリ 141,260 KB
実行使用メモリ 26,204 KB
最終ジャッジ日時 2023-08-21 10:45:24
合計ジャッジ時間 5,472 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
20,116 KB
testcase_01 AC 4 ms
11,612 KB
testcase_02 AC 5 ms
11,508 KB
testcase_03 AC 5 ms
11,536 KB
testcase_04 AC 5 ms
11,608 KB
testcase_05 AC 5 ms
11,732 KB
testcase_06 AC 5 ms
11,512 KB
testcase_07 AC 5 ms
11,580 KB
testcase_08 AC 7 ms
11,740 KB
testcase_09 AC 15 ms
12,444 KB
testcase_10 AC 109 ms
20,092 KB
testcase_11 AC 110 ms
20,048 KB
testcase_12 AC 114 ms
20,048 KB
testcase_13 AC 89 ms
20,016 KB
testcase_14 AC 291 ms
25,888 KB
testcase_15 AC 282 ms
26,204 KB
testcase_16 AC 283 ms
25,944 KB
testcase_17 AC 281 ms
25,968 KB
testcase_18 AC 284 ms
25,892 KB
testcase_19 AC 165 ms
20,392 KB
testcase_20 AC 171 ms
20,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional> 
#include <iomanip>
#include <unordered_map>
#include <memory.h>
#include <unordered_set>
#include <fstream>
#include <random>

using namespace std;

const int MAXM = 200200;
pair<int,int> edge[MAXM], del[MAXM];

int ans[MAXM];
int e2g[MAXM];
vector<int> g2e[MAXM];

bool isSame(int x, int y) {
    return e2g[x] == e2g[y];
}

void unite(int x, int y) {
    if (isSame(x, y)) return;
    int gx = e2g[x], gy = e2g[y];
    if (g2e[gx].size() < g2e[gy].size()) {
        swap(x, y);
        swap(gx, gy);
    }
    for (int el : g2e[gy]) e2g[el] = gx;
    g2e[gx].insert(g2e[gx].end(), g2e[gy].begin(), g2e[gy].end());
    g2e[gy].clear();
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M, Q;
    cin >> N >> M >> Q;
    for (int i = 0; i < M; i++) {
        cin >> edge[i].first >> edge[i].second;
        edge[i].first--; edge[i].second--;
    }
    set<pair<int,int>> S;
    for (int i = 0; i < Q; i++) {
        cin >> del[i].first >> del[i].second;
        del[i].first--; del[i].second--;
        S.insert(del[i]);
    }
    for (int i = 0; i < N; i++) {
        e2g[i] = i;
        g2e[i].push_back(i);
    }
    for (int i = 0; i < M; i++) {
        if (S.find(edge[i]) == S.end()) {
            unite(edge[i].first, edge[i].second);
        }
    }
    {
        int g = e2g[0];
        for (int el : g2e[g])
            ans[el] = -1;
    }
    for (int i = Q - 1; i >= 0; i--) {
        int a = del[i].first, b = del[i].second;
        if (!isSame(a, b)) {
            if (!isSame(0, a) && !isSame(0, b)) {
                unite(a, b);
            }
            else {
                if (isSame(0, b)) {
                    swap(a, b);
                }
                int gb = e2g[b];
                for (int el : g2e[gb])
                    ans[el] = i + 1;
                unite(a, b);
            }
        }
    }
    for (int i = 1; i < N; i++)
        cout << ans[i] << '\n';
    return 0;
}
0