結果

問題 No.416 旅行会社
ユーザー はむこはむこ
提出日時 2017-08-16 11:09:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 648 ms / 4,000 ms
コード長 2,791 bytes
コンパイル時間 1,783 ms
コンパイル使用メモリ 167,196 KB
実行使用メモリ 33,012 KB
最終ジャッジ日時 2023-08-21 10:13:06
合計ジャッジ時間 8,989 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 236 ms
19,892 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 7 ms
4,544 KB
testcase_09 AC 31 ms
5,792 KB
testcase_10 AC 289 ms
19,996 KB
testcase_11 AC 286 ms
22,908 KB
testcase_12 AC 300 ms
23,096 KB
testcase_13 AC 234 ms
22,964 KB
testcase_14 AC 621 ms
31,936 KB
testcase_15 AC 624 ms
32,280 KB
testcase_16 AC 648 ms
33,012 KB
testcase_17 AC 629 ms
32,512 KB
testcase_18 AC 633 ms
32,188 KB
testcase_19 AC 497 ms
25,424 KB
testcase_20 AC 507 ms
24,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)
#define repi(i,a,b) for(long long i = (long long)(a); i < (long long)(b); i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define ZERO(a) memset(a,0,sizeof(a))

using ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using P = pair<ll, ll>;
using vi = vector<int>; using vvi = vector<vi>; vll conv(vi& v) { vll r(v.size()); rep(i, v.size()) r[i] = v[i]; return r; }

size_t random_seed; 
struct timeval start; double sec() { struct timeval tv; gettimeofday(&tv, NULL); return (tv.tv_sec - start.tv_sec) + (tv.tv_usec - start.tv_usec) * 1e-6; }
struct init_{init_(){ gettimeofday(&start, NULL); ios::sync_with_stdio(false); cin.tie(0); srand((unsigned int)time(NULL)); random_seed = RAND_MAX / 2 + rand() / 2; }} init__;

struct UnionFind {
    vector<int> data;
    UnionFind(int size) : data(size, -1) { }
    // x, yをマージ, O(A^-1)
    bool unite(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
            if (data[y] < data[x]) swap(x, y);
            data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    // x, yが同じ集合なら1, O(A^-1)
    bool find(int x, int y) {
        return root(x) == root(y);
    }
    // xの根を探す。同じ集合なら同じ根が帰る, O(A^-1)
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
};

#define MAXN 100010
ll ret[MAXN];

vvll g;
void dfs(ll v, ll c) {
    if (ret[v] != -2) return;
    ret[v] = c;
    for (auto x : g[v]) 
        dfs(x, c);
}

int main(void) {
    ll n, m, q; cin >> n >> m >> q;
    g=vvll(n);
    rep(i, MAXN) ret[i] = -2;
    
    vvll g_all(n);
    set<P> edges;
    rep(i, m) {
        ll u, v; cin>>u>>v;u--,v--;
        g_all[u].pb(v), g_all[v].pb(u), edges.insert(P(u, v));
    }
    vll c(q),d(q);
    rep(i, q) {
        cin >> c[i] >> d[i]; c[i]--, d[i]--;
        edges.erase(P(c[i], d[i]));
    }
    reverse(all(c)), reverse(all(d));

    for (auto x : edges) 
        g[x.fi].pb(x.se), g[x.se].pb(x.fi);

    {
        UnionFind uf(n);
        rep(i, n) for (auto u : g_all[i]) uf.unite(i, u);
        rep(i, n) if (uf.find(0, i) == 0) ret[i] = 0;
    }

    UnionFind uf(n);
    for (auto x : edges) uf.unite(x.fi, x.se);
    rep(i, n) if (uf.find(0, i) == 1) 
        ret[i] = -1;

    rep(i, q) {
        ll u = c[i], v = d[i];
        if (uf.find(0, u) ^ uf.find(0, v)) {
            if (!uf.find(0, u) && uf.find(0, v)) 
                swap(u, v);
            dfs(v, q-i);
        }
        uf.unite(u, v), g[v].pb(u), g[u].pb(v);
    }

    repi(i, 1, n) 
        cout << ret[i] << endl;

    return 0;
}
0