結果

問題 No.416 旅行会社
ユーザー pekempeypekempey
提出日時 2016-10-29 06:23:11
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 87 ms / 4,000 ms
コード長 1,809 bytes
コンパイル時間 1,438 ms
コンパイル使用メモリ 147,860 KB
実行使用メモリ 11,172 KB
最終ジャッジ日時 2023-08-21 10:07:24
合計ジャッジ時間 3,672 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
10,320 KB
testcase_01 AC 3 ms
7,780 KB
testcase_02 AC 2 ms
7,520 KB
testcase_03 AC 3 ms
7,512 KB
testcase_04 AC 2 ms
7,516 KB
testcase_05 AC 2 ms
7,468 KB
testcase_06 AC 2 ms
7,588 KB
testcase_07 AC 2 ms
7,476 KB
testcase_08 AC 3 ms
7,536 KB
testcase_09 AC 6 ms
7,960 KB
testcase_10 AC 28 ms
10,472 KB
testcase_11 AC 26 ms
10,288 KB
testcase_12 AC 27 ms
10,284 KB
testcase_13 AC 19 ms
10,460 KB
testcase_14 AC 87 ms
11,004 KB
testcase_15 AC 87 ms
10,960 KB
testcase_16 AC 87 ms
10,952 KB
testcase_17 AC 87 ms
11,004 KB
testcase_18 AC 87 ms
11,172 KB
testcase_19 AC 53 ms
10,544 KB
testcase_20 AC 53 ms
10,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct edge { int u, v; };
struct node { int val; node *next; };

const int N = 1e5;
const int M = 2e5;
int n, m, q, par[N], c[M], d[M], ans[M];
bool dead[M];
edge g[M];
node body[N], *head[N], *tail[N];

bool operator<(const edge &a, const edge &b) {
    return a.u != b.u ? a.u < b.u : a.v < b.v;
}

int find(int x) {
    return par[x] = (par[x] == x ? x : find(par[x]));
}

void unite(int x, int y, int t) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (x > y) swap(x, y);
    par[y] = x;
    if (x == 0) {
        for (node *it = head[y]; it; it = it->next) ans[it->val] = t;
    } else {
        tail[x]->next = head[y];
        tail[x] = tail[y];
    }
}

#define getchar getchar_unlocked
#define putchar putchar_unlocked

int in() {
    int n, c;
    while ((c = getchar()) < '0') if (c == EOF) return -1;
    n = c - '0';
    while ((c = getchar()) >= '0') n = n * 10 + c - '0';
    return n;
}

void out(int n) {
    if (n < 0) putchar('-'), n *= -1;
    int res[11], i = 0;
    do { res[i++] = n % 10, n /= 10; } while (n);
    while (i) putchar(res[--i] + '0');
    putchar('\n');
}

int main() {
    cin >> n >> m >> q;
    for (int i = 0; i < n; i++) {
        par[i] = body[i].val = i;
        head[i] = tail[i] = &body[i];
    }
    for (int i = 0; i < m; i++) g[i].u = in() - 1, g[i].v = in() - 1;
    sort(g, g + m);
    for (int i = 0; i < q; i++) {
        c[i] = in() - 1;
        d[i] = in() - 1;
        int k = lower_bound(g, g + m, (edge){ c[i], d[i] }) - g;
        if (k < m && g[k].u == c[i] && g[k].v == d[i]) dead[k] = true;
    }
    for (int i = 0; i < m; i++) if (!dead[i]) unite(g[i].u, g[i].v, -1);
    for (int i = q - 1; i >= 0; i--) unite(c[i], d[i], i + 1);
    for (int i = 1; i < n; i++) out(ans[i]);
}
0