結果

問題 No.416 旅行会社
ユーザー mayoko_mayoko_
提出日時 2016-08-26 22:52:25
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 446 ms / 4,000 ms
コード長 2,348 bytes
コンパイル時間 1,471 ms
コンパイル使用メモリ 108,848 KB
実行使用メモリ 26,240 KB
最終ジャッジ日時 2023-08-21 09:40:14
合計ジャッジ時間 6,950 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 207 ms
20,116 KB
testcase_01 AC 5 ms
11,752 KB
testcase_02 AC 5 ms
11,620 KB
testcase_03 AC 5 ms
11,712 KB
testcase_04 AC 5 ms
11,624 KB
testcase_05 AC 5 ms
11,564 KB
testcase_06 AC 5 ms
11,700 KB
testcase_07 AC 6 ms
11,664 KB
testcase_08 AC 9 ms
11,828 KB
testcase_09 AC 29 ms
12,524 KB
testcase_10 AC 231 ms
20,044 KB
testcase_11 AC 235 ms
20,100 KB
testcase_12 AC 244 ms
20,108 KB
testcase_13 AC 209 ms
20,096 KB
testcase_14 AC 444 ms
25,988 KB
testcase_15 AC 438 ms
26,240 KB
testcase_16 AC 437 ms
26,104 KB
testcase_17 AC 439 ms
26,088 KB
testcase_18 AC 446 ms
25,944 KB
testcase_19 AC 308 ms
20,440 KB
testcase_20 AC 304 ms
20,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>
#include<random>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

const int MAXM = 200200;
pii edge[MAXM], del[MAXM];

int ans[MAXM];
int e2g[MAXM];
vi 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<pii> 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] << endl;
    return 0;
}
0