Submission #7068142


Source Code Expand

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define sar(a,n) {cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl;}

using namespace std;

template<typename S,typename T>auto&operator<<(ostream&o,pair<S,T>p){return o<<"{"<<p.fi<<","<<p.se<<"}";}
template<typename T>auto&operator<<(ostream&o,set<T>s){for(auto&e:s)o<<e<<" ";return o;}
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,priority_queue<S,T,U>q){while(!q.empty())o<<q.top()<<" ",q.pop();return o;}
template<typename K,typename T>auto&operator<<(ostream&o,map<K,T>&m){for(auto&e:m)o<<e<<" ";return o;}
template<typename T>auto&operator<<(ostream&o,vector<T>v){for(auto&e:v)o<<e<<" ";return o;}
void ashow(){cout<<endl;}template<typename T,typename...A>void ashow(T t,A...a){cout<<t<<" ";ashow(a...);}
template<typename S,typename T,typename U>
struct TRI{S fi;T se;U th;TRI(){}TRI(S f,T s,U t):fi(f),se(s),th(t){}
bool operator<(const TRI&_)const{return(fi==_.fi)?((se==_.se)?(th<_.th):(se<_.se)):(fi<_.fi);}};
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,TRI<S,T,U>&t){return o<<"{"<<t.fi<<","<<t.se<<","<<t.th<<"}";}

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef TRI<int, int, int> tri;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<P> vp;
typedef vector<double> vd;
typedef vector<string> vs;

const int MAX_N = 100005;

class PartialPersistentUnionFind {
public:
    const int V;
    int last;
    vector<pair<int, int> > par;
    vector<vector<pair<int, int> > > sz;
    PartialPersistentUnionFind(int node_size)
        : V(node_size), last(-1), par(V), sz(V){
        for(int i = 0; i < V; ++i){
            par[i] = {1, i};
            sz[i].emplace_back(-1, 1);
        }
    }
    int find(const int u, const int _time){
        if(u == par[u].second || par[u].first > _time){
            return u;
        }else{
            return find(par[u].second, _time);
        }
    }
    bool unite(int u, int v, const int _time){
        assert(last <= _time);
        if(same(u, v, _time)) return false;
        u = find(u, _time), v = find(v, _time);
        if(par[u].first < par[v].first){
            swap(u, v);
        }
        par[v] = {_time, u};
        sz[u].emplace_back(_time, sz[u].back().second + sz[v].back().second);
        if(par[u].first == par[v].first){
            ++par[u].first;
        }
        return true;
    }
    bool same(const int u, const int v, const int _time){
        return find(u, _time) == find(v, _time);
    }
    int size(int u, const int _time){
        u = find(u, _time);
        return sz[u][static_cast<int>(upper_bound(sz[u].begin(), sz[u].end(),
            make_pair(_time, V+1)) - sz[u].begin()) - 1].second;
    }
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    PartialPersistentUnionFind uf(n);
    rep(i,m){
        int a,b;
        cin >> a >> b;
        uf.unite(a-1, b-1, i+1);
    }
    int q;
    cin >> q;
    rep(i,q){
        int x, y;
        cin >> x >> y;
        --x, --y;
        int l = 0, r = m+1;
        while(r-l>1){
            int mid = (l+r)/2;
            if(uf.same(x,y,mid)){
                r = mid;
            }else{
                l = mid;
            }
        }
        if(r == m+1){
            cout << "-1\n";
        }else{
            cout << r << "\n";
        }
    }
    return 0;
}

Submission Info

Submission Time
Task H - Union Sets
User kopricky
Language C++14 (GCC 5.4.1)
Score 600
Code Size 4094 Byte
Status AC
Exec Time 139 ms
Memory 8576 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 49
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All sample_01.txt, sample_02.txt, sample_03.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_12.txt, subtask_1_13.txt, subtask_1_14.txt, subtask_1_15.txt, subtask_1_16.txt, subtask_1_17.txt, subtask_1_18.txt, subtask_1_19.txt, subtask_1_2.txt, subtask_1_20.txt, subtask_1_21.txt, subtask_1_22.txt, subtask_1_23.txt, subtask_1_24.txt, subtask_1_25.txt, subtask_1_26.txt, subtask_1_27.txt, subtask_1_28.txt, subtask_1_29.txt, subtask_1_3.txt, subtask_1_30.txt, subtask_1_31.txt, subtask_1_32.txt, subtask_1_33.txt, subtask_1_34.txt, subtask_1_35.txt, subtask_1_36.txt, subtask_1_37.txt, subtask_1_38.txt, subtask_1_39.txt, subtask_1_4.txt, subtask_1_40.txt, subtask_1_41.txt, subtask_1_42.txt, subtask_1_43.txt, subtask_1_44.txt, subtask_1_45.txt, subtask_1_46.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt
Case Name Status Exec Time Memory
sample_01.txt AC 1 ms 256 KB
sample_02.txt AC 1 ms 256 KB
sample_03.txt AC 1 ms 256 KB
subtask_1_1.txt AC 1 ms 256 KB
subtask_1_10.txt AC 10 ms 256 KB
subtask_1_11.txt AC 47 ms 768 KB
subtask_1_12.txt AC 5 ms 3328 KB
subtask_1_13.txt AC 2 ms 256 KB
subtask_1_14.txt AC 1 ms 256 KB
subtask_1_15.txt AC 18 ms 384 KB
subtask_1_16.txt AC 2 ms 256 KB
subtask_1_17.txt AC 3 ms 256 KB
subtask_1_18.txt AC 1 ms 256 KB
subtask_1_19.txt AC 6 ms 2816 KB
subtask_1_2.txt AC 1 ms 256 KB
subtask_1_20.txt AC 1 ms 256 KB
subtask_1_21.txt AC 3 ms 256 KB
subtask_1_22.txt AC 3 ms 1792 KB
subtask_1_23.txt AC 27 ms 6784 KB
subtask_1_24.txt AC 28 ms 6784 KB
subtask_1_25.txt AC 29 ms 6784 KB
subtask_1_26.txt AC 34 ms 6912 KB
subtask_1_27.txt AC 118 ms 8320 KB
subtask_1_28.txt AC 119 ms 8320 KB
subtask_1_29.txt AC 27 ms 6784 KB
subtask_1_3.txt AC 1 ms 256 KB
subtask_1_30.txt AC 28 ms 6784 KB
subtask_1_31.txt AC 29 ms 6784 KB
subtask_1_32.txt AC 33 ms 6784 KB
subtask_1_33.txt AC 139 ms 8448 KB
subtask_1_34.txt AC 138 ms 8448 KB
subtask_1_35.txt AC 27 ms 6784 KB
subtask_1_36.txt AC 28 ms 6784 KB
subtask_1_37.txt AC 29 ms 6784 KB
subtask_1_38.txt AC 34 ms 7040 KB
subtask_1_39.txt AC 65 ms 8052 KB
subtask_1_4.txt AC 8 ms 4864 KB
subtask_1_40.txt AC 65 ms 8052 KB
subtask_1_41.txt AC 20 ms 512 KB
subtask_1_42.txt AC 25 ms 512 KB
subtask_1_43.txt AC 95 ms 5888 KB
subtask_1_44.txt AC 115 ms 8576 KB
subtask_1_45.txt AC 15 ms 512 KB
subtask_1_46.txt AC 26 ms 6784 KB
subtask_1_5.txt AC 8 ms 256 KB
subtask_1_6.txt AC 5 ms 3456 KB
subtask_1_7.txt AC 2 ms 640 KB
subtask_1_8.txt AC 2 ms 1024 KB
subtask_1_9.txt AC 5 ms 896 KB