結果

問題 No.416 旅行会社
ユーザー ctyl_0ctyl_0
提出日時 2016-10-18 11:05:08
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 2,468 bytes
コンパイル時間 1,255 ms
コンパイル使用メモリ 109,536 KB
実行使用メモリ 813,812 KB
最終ジャッジ日時 2023-08-14 14:19:59
合計ジャッジ時間 9,277 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 555 ms
10,760 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 5 ms
5,636 KB
testcase_09 AC 54 ms
49,004 KB
testcase_10 AC 565 ms
10,920 KB
testcase_11 MLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

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(u, v);
        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 G[I[a]] == G[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