結果

問題 No.416 旅行会社
ユーザー ctyl_0ctyl_0
提出日時 2016-10-18 11:13:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 288 ms / 4,000 ms
コード長 2,464 bytes
コンパイル時間 2,116 ms
コンパイル使用メモリ 108,704 KB
実行使用メモリ 14,736 KB
最終ジャッジ日時 2023-08-21 10:07:30
合計ジャッジ時間 5,851 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
10,684 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 12 ms
4,384 KB
testcase_10 AC 97 ms
10,976 KB
testcase_11 AC 97 ms
10,976 KB
testcase_12 AC 99 ms
10,924 KB
testcase_13 AC 81 ms
10,756 KB
testcase_14 AC 279 ms
13,808 KB
testcase_15 AC 281 ms
13,860 KB
testcase_16 AC 288 ms
14,012 KB
testcase_17 AC 284 ms
13,812 KB
testcase_18 AC 287 ms
13,864 KB
testcase_19 AC 194 ms
14,736 KB
testcase_20 AC 188 ms
14,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>
#define repd(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T>
inline void output(T a, int p = 0) {
    if(p) cout << fixed << setprecision(p)  << a << "\n";
    else cout << a << "\n";
}
// end of template

// general merge technique O(NlogN)
class GeneralMerge {
public:
    vector<int> I; // index
    vector<vector<int>> G; // group
    GeneralMerge(int N): I(N), G(N) {
        rep(i, N) I[i] = i;
        rep(i, N) G[i].pb(i);
    }
    
    void merge(int u, int v){ // #G[u] >= #G[v]のとき、vをuにマージする
        if(same(u, v)) return; // すでに同じグループならマージしない
        int iu = I[u], iv = I[v];
        if(G[iu].size() < G[iv].size()) swap(iu, iv);
        for(int w: G[iv]){
            I[w] = iu;
        }
        G[iu].insert(G[iu].end(), all(G[iv]));
        G[iv].clear();
    }
    
    bool same(int a, int b){
        return I[a] == I[b];
    }
    
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    // source code
    int N, M, Q;
    cin >> N >> M >> Q;
    set<pair<int, int>> E;
    rep(i, M){
        int a, b;
        cin >> a >> b;
        a--, b--;
        E.insert(mp(a, b));
    }
    
    vector<pair<int, int>> C(Q);
    rep(i, Q){
        int a, b;
        cin >> a >> b;
        a--, b--;
        C[i] = mp(a, b);
        E.erase(C[i]);
    }
    
    GeneralMerge A(N);
    for(auto it = E.begin(); it != E.end(); it++){
        A.merge(it->first, it->second);
    }
    
    vector<int> ret(N, -1);
    rep(i, N){
        if(!A.same(0, i)) ret[i] = 0;
    }
    
    for(int i = Q - 1; i >= 0; i--){
        int a = C[i].first, b = C[i].second;
        if(A.same(0, a) && !A.same(0, b)){
            for(int c: A.G[A.I[b]]){
                ret[c] = i + 1;
            }
        }
        else if(!A.same(0, a) && A.same(0, b)){
            for(int c: A.G[A.I[a]]){
                ret[c] = i + 1;
            }
        }
        A.merge(a, b);
    }
    
    repd(i, 1, N) output(ret[i]);

    
    
    
    return 0;
}
0