結果

問題 No.416 旅行会社
ユーザー peroonperoon
提出日時 2019-04-18 11:39:30
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 523 ms / 4,000 ms
コード長 2,908 bytes
コンパイル時間 2,095 ms
コンパイル使用メモリ 181,916 KB
実行使用メモリ 33,692 KB
最終ジャッジ日時 2023-08-21 10:23:17
合計ジャッジ時間 8,685 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 230 ms
19,596 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 7 ms
4,380 KB
testcase_09 AC 27 ms
4,864 KB
testcase_10 AC 257 ms
19,348 KB
testcase_11 AC 270 ms
24,188 KB
testcase_12 AC 274 ms
24,204 KB
testcase_13 AC 233 ms
24,308 KB
testcase_14 AC 513 ms
32,136 KB
testcase_15 AC 489 ms
32,864 KB
testcase_16 AC 502 ms
33,692 KB
testcase_17 AC 494 ms
32,580 KB
testcase_18 AC 523 ms
32,292 KB
testcase_19 AC 366 ms
21,968 KB
testcase_20 AC 361 ms
21,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")

const ll mod = 1e9 + 7;
const ll inf = 1e18;

struct UnionFind{
    vector<ll> parent;
    UnionFind(ll sz){
        parent.resize(sz);
        reset();
    }

    void reset(){
        FOR(i, 0, parent.size()){
            parent[i] = i;
        }
    }

    void unite(ll a, ll b){
        if(a>b){
            swap(a, b);
        }
        ll rootA = findRoot(a);
        ll rootB = findRoot(b);
        if(rootA>rootB){
            swap(rootA, rootB);
        }
        if(rootA==rootB){
            return;
        }else{
            // 小さい方を親にする
            parent[rootB] = rootA;
        }
    }

    ll findRoot(ll a){
        if(parent[a]==a){
            return a;
        }else{
            return parent[a] = findRoot(parent[a]);
        }
    }
};

vector<vector<ll> > G;
vector<ll> ans;

// 塗る
void dfs(ll i, ll v){
    ans[i] = v;
    for(ll to : G[i]){
        if(ans[to]==0) dfs(to, v);
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // input
    ll N, M, Q;
    cin >> N >> M >> Q;
    
    G.resize(N);
    ans.resize(N);

    vector<ll> A(M);
    vector<ll> B(M);
    FOR(i, 0, M){
        ll a, b;
        cin >> a >> b;
        a--;
        b--;
        A[i] = a;
        B[i] = b;
    }

    // 壊される辺
    vector<ll> C(Q);
    vector<ll> D(Q);
    map<pair<ll, ll>, ll> mp_break;
    FOR(i, 0, Q){
        ll c, d;
        cin >> c >> d;
        c--;
        d--;
        C[i] = c;
        D[i] = d;
        mp_break[make_pair(c, d)] = 1;
    }

    auto uf = UnionFind(N);

    // 辺をはる
    FOR(i, 0, M){
        ll a = A[i];
        ll b = B[i];
        auto p = make_pair(a, b);
        if(mp_break.count(p)>0){
            // 辺をはらない
        }else{
            uf.unite(a, b);

            G[a].push_back(b);
            G[b].push_back(a);
        }
    }

    // 壊した後もつながっている点
    FOR(i, 0, N){
        if(uf.findRoot(i)==0){
            ans[i] = -1;
        }
    }

    // 破壊を逆順に
    for(int i=Q-1; i>=0; i--){
        ll c = C[i];
        ll d = D[i];
        ll rootC = uf.findRoot(c);
        ll rootD = uf.findRoot(d);
        
        if(rootC==0 && rootD!=0){
            // 今回でつながる
            dfs(rootD, i+1);
        }
        if(rootD==0 && rootC!=0){
            // 今回でつながる
            dfs(rootC, i+1);
        }
        uf.unite(c, d);
        
        G[c].push_back(d);
        G[d].push_back(c);        
    }

    FOR(i, 1, N){
        p(ans[i]);
    }
    
    return 0;
}
0