結果

問題 No.416 旅行会社
ユーザー Sooh317Sooh317
提出日時 2021-12-06 18:40:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 436 ms / 4,000 ms
コード長 5,098 bytes
コンパイル時間 5,395 ms
コンパイル使用メモリ 271,412 KB
実行使用メモリ 23,332 KB
最終ジャッジ日時 2023-08-21 10:46:32
合計ジャッジ時間 11,009 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 220 ms
16,816 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 6 ms
4,376 KB
testcase_09 AC 27 ms
4,732 KB
testcase_10 AC 242 ms
16,892 KB
testcase_11 AC 240 ms
17,072 KB
testcase_12 AC 245 ms
16,596 KB
testcase_13 AC 221 ms
16,816 KB
testcase_14 AC 431 ms
23,144 KB
testcase_15 AC 430 ms
23,332 KB
testcase_16 AC 432 ms
23,132 KB
testcase_17 AC 436 ms
23,148 KB
testcase_18 AC 433 ms
23,292 KB
testcase_19 AC 298 ms
16,664 KB
testcase_20 AC 303 ms
16,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *    author:  Sooh
 *    created: 05.12.2021 10:58:35
**/
#include<bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
//using mint = modint1000000007;
#endif
#pragma region template
using ll = long long;
template <class T> using V = vector<T>;
#define rep(i,n) for(int i=0;i<n;++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#ifdef LOCAL
#define debug(var)  do{std::cerr << "\033[1;36m" << #var << ": \033[0m";view(var);std::cerr << std::endl;}while(0)
template<typename T> void view(T e){std::cerr << e;}
template<typename T, typename K> void view(pair<T, K> e){std::cerr << "("; view(e.fi); std::cerr << ", "; view(e.se); std::cerr << ")";}
template<typename T> void view(const set<T> &st){ std::cerr << "\n";for(const auto& e : st){view(e); std::cerr << " ";}}
template<typename T> void view(const multiset<T> &st){ std::cerr << "\n";for(const auto& e : st){view(e); std::cerr << " ";}}
template<typename T, typename K> void view(const map<T, K> &mp){ std::cerr << "\n";for(const auto& [k, v]: mp){std::cerr << "("; view(k); std::cerr << ", "; view(v); std::cerr << ") ";}}
template<typename T, typename K> void view(const unordered_map<T, K> &mp){ std::cerr << "\n";for(const auto& [k, v]: mp){std::cerr << "("; view(k); std::cerr << ", "; view(v); std::cerr << ") ";}}
template<typename T> void view(const std::vector<T>& v){std::cerr << "\n";for(const auto& e : v){ view(e); std::cerr << " "; }}
template<typename T> void view(const std::vector<std::vector<T> >& vv){std::cerr << "\n";int cnt = 0;for(const auto& v : vv){cerr << cnt << "th : "; view(v); cnt++; std::cerr << std::endl;}}
#else 
#define debug(var) 0
#endif
ll power(ll a, ll p){ll ret = 1; while(p){if(p & 1){ret = ret * a;} a = a * a; p >>= 1;} return ret;}
ll modpow(ll a, ll p, ll mod){ll ret = 1; while(p){assert(a < mod); if(p & 1){ret = ret * a % mod;} a = a * a % mod; p >>= 1;} return ret;}
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0; while (b) {ll t = a / b ;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
template<class T, class K>bool chmax(T &a, const K b) { if (a<b) { a=b; return 1; } return 0; }
template<class T, class K>bool chmin(T &a, const K b) { if (b<a) { a=b; return 1; } return 0; }
#pragma endregion
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
const int inf = 1001001001;
const ll INF = 1001001001001001001ll;
//const double pi = acos(-1);
const ll mod = 998244353;
//const ll mod = 1000000007;

struct Partially_Persistent_Union_Find{
private:
    // tree : {size of this tree (if this is root) / parent (otherwize), time when this node became a child}
    std::vector<std::pair<int, int>> tree;
    // siz : for recording the size of a tree at the time T
    std::vector<std::vector<std::pair<int, int>>> siz;
    int count;
public:
    Partially_Persistent_Union_Find(int n):
        tree(n, {1, std::numeric_limits<int>::max()}), 
        siz(n, std::vector<std::pair<int, int>>(1, {0, 1})), 
        count(0){}
    
    int leader(const int t, int x){
        while(tree[x].second <= t) x = tree[x].first;
        return x;
    }

    // you can get time if you change the return value
    int merge(int x, int y){
        ++count;
        x = leader(count, x);
        y = leader(count, y);
        if(x == y) return count;
        if(tree[x].first < tree[y].first) std::swap(x, y);
        tree[x].first += tree[y].first;
        siz[x].emplace_back(count, tree[x].first);
        tree[y] = std::pair<int, int>(x, count);
        return count;
    }

    bool same(const int t, int x, int y){return leader(t, x) == leader(t, y);}

    int size(const int t, int x){
        x = leader(t, x);
        auto it = std::lower_bound(siz[x].begin(), siz[x].end(), std::pair<int, int>(t + 1, 0));
        return (--it)->second;
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(20);
    int n, m, q; cin >> n >> m >> q;
    V<int> a(m), b(m), c(q), d(q);
    rep(i,m) cin >> a[i] >> b[i], --a[i], --b[i];
    set<pair<int, int>> st;
    rep(i,q){
        cin >> c[i] >> d[i];
        --c[i], --d[i];
        st.insert({c[i], d[i]});
    }
    Partially_Persistent_Union_Find uf(n);
    int base = m - q;
    rep(i,m){
        if(st.count({a[i], b[i]})) continue;
        uf.merge(a[i], b[i]);
    }
    for(int i = q - 1; i >= 0; i--){
        uf.merge(c[i], d[i]);
    }
    V<int> ans(n);
    debug(base);
    for(int i = 1; i < n; i++){
        if(uf.same(base, 0, i)){
            ans[i] = -1;
            continue;
        }
        int ok = m + 1, ng = base;
        while(abs(ok - ng) > 1){
            int mid = (ok + ng) / 2;
            if(uf.same(mid, 0, i)) ok = mid;
            else ng = mid;
        }
        ok -= base;
        ans[i] = (ok == m + 1 - base ? 0 : q - ok + 1);
    }
    for(int i = 1; i < n; i++) cout << ans[i] << endl;
}
0