結果

問題 No.416 旅行会社
ユーザー uenokuuenoku
提出日時 2017-01-13 23:12:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 971 ms / 4,000 ms
コード長 2,602 bytes
コンパイル時間 1,619 ms
コンパイル使用メモリ 115,724 KB
実行使用メモリ 53,756 KB
最終ジャッジ日時 2023-08-21 10:12:10
合計ジャッジ時間 11,634 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 354 ms
38,204 KB
testcase_01 AC 4 ms
8,820 KB
testcase_02 AC 3 ms
8,072 KB
testcase_03 AC 4 ms
8,464 KB
testcase_04 AC 3 ms
8,852 KB
testcase_05 AC 4 ms
8,120 KB
testcase_06 AC 4 ms
8,272 KB
testcase_07 AC 5 ms
8,596 KB
testcase_08 AC 11 ms
9,224 KB
testcase_09 AC 47 ms
11,476 KB
testcase_10 AC 431 ms
38,184 KB
testcase_11 AC 431 ms
38,192 KB
testcase_12 AC 438 ms
40,860 KB
testcase_13 AC 358 ms
38,236 KB
testcase_14 AC 971 ms
53,628 KB
testcase_15 AC 970 ms
53,312 KB
testcase_16 AC 970 ms
53,708 KB
testcase_17 AC 969 ms
53,756 KB
testcase_18 AC 968 ms
53,576 KB
testcase_19 AC 657 ms
43,552 KB
testcase_20 AC 665 ms
43,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "math.h"
#include <algorithm>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ifor(i, a, b) for (int i = (a); i < (b); i++)
#define rfor(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define SIZE 200005
#define UF_MAX 100005
using namespace std;
typedef long double ld;
typedef long long int lli;
const double eps = 1e-11;
int vex[4] = {1, 0, -1, 0};
int vey[4] = {0, 1, 0, -1};
lli MOD = 1000000007;
int turn = 0;
int ans[100005] = {};
#define UF_MAX 100005
struct UFind {
    int par[UF_MAX];
    int rank[UF_MAX];
    int count[UF_MAX];
    set<int> st[UF_MAX];  //parが全部持つ
    UFind(int n)
    {
        rep(i, n)
        {
            par[i] = i;
            rank[i] = 0;
            st[i].insert(i);
            count[i] = 1;
        }
    }
    int find(int x)
    {
        if (par[x] == x) {
            return x;
        } else {
            return par[x] = find(par[x]);
        }
    }
    void unite(int x, int y)
    {
        x = find(x);
        y = find(y);
        if (x == y)
            return;
        if (same(x, 0) && !same(y, 0)) {
            for (auto s : st[y])
                ans[s] = turn;
        } else if (!same(x, 0) && same(y, 0)) {
            for (auto s : st[x])
                ans[s] = turn;
        }
        if (st[x].size() > st[y].size()) {
            for (auto s : st[y])
                st[x].insert(s);

            par[y] = x;
        } else {
            for (auto s : st[x])
                st[y].insert(s);

            par[x] = y;
        }
    }
    bool same(int x, int y)
    {
        return find(x) == find(y);
    }
};

int main()
{
    map<int, map<int, int>> edge;
    int n, m, q;
    cin >> n >> m >> q;
    int a, b;
    UFind uf(n);
    rep(i, m)
    {
        cin >> a >> b;
        a--, b--;
        edge[a][b] = -1;
    }
    vector<pair<int, int>> query;
    rep(i, q)
    {
        cin >> a >> b;
        a--, b--;
        edge[a][b] = 1;
        edge[b][a] = 1;
        query.push_back(make_pair(a, b));
    }
    turn = -1;
    for (auto s : edge)
        for (auto t : s.second)
            if (t.second == -1)
                uf.unite(s.first, t.first);
    reverse(all(query));
    turn = q;
    rep(i, q)
    {
        uf.unite(query[i].first, query[i].second);
        turn--;
    }
    rep(i, n - 1)
    {
        cout << ans[i + 1] << endl;
    }
}
0