Submission #19279231


Source Code Expand

Copy
#define _DEBUG
#include "bits/stdc++.h"
#include <atcoder/all>
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,a5,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#define debug_6(x1,x2,x3,x4,x5,x6) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<", "#x6<<": "<<x6<<endl
#ifdef _DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_6,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(index,num) for(int index=0;index<(int)num;index++)
#define rep1(index,num) for(int index=1;index<=(int)num;index++)
#define brep(index,num) for(int index=(int)num-1;index>=0;index--)
#define brep1(index,num) for(int index=(int)num;index>0;index--)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-7
#define mp(a1,a2) make_pair(a1,a2)
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
#define puba(a) emplace_back(a)
typedef long long ll;
typedef long double ld;
using namespace std;
using namespace atcoder;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
ll INFl=(ll)1e+18+1;
int INF=1e+9+1;
struct PartiallyPersistentUnionFind {
    vector<int> par; // par[x]: x が根のときは x を含むグループのサイズ (の -1 倍)、そうでないときは親ノード
    vector<int> last; // last[x] : x が最後に「根」ではなくなった瞬間の時刻
    vector<vector<pint> > history; // history[x] := x の親ノードが変わるたびに (その時刻, 新たな親) を push していく

    PartiallyPersistentUnionFind(int n) : par(n, -1), last(n, -1), history(n) {
        for (auto &vec : history) vec.emplace_back(-1, -1);
    }
    void init(int n) {
        par.assign(n, -1); last.assign(n, -1); history.assign(n, vector<pint>());
        for (auto &vec : history) vec.emplace_back(-1, -1);
    }

    int root(int t, int x) {
        if (last[x] == -1 || t < last[x]) return x;
        return root(t, par[x]);
    }

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

    bool unite(int t, int x, int y) {
        x = root(t, x); y = root(t, y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y); // merge technique
        par[x] += par[y];
        par[y] = x;
        last[y] = t;
        history[x].emplace_back(t, par[x]);
        return true;
    }

    int size(int t, int x) {
        x = root(t, x);
        return -prev(lower_bound(history[x].begin(), history[x].end(), make_pair(t, 0)))->second;
    }
};

int main(){
	int N,M;
	scan(N>>M);
    PartiallyPersistentUnionFind uf(N);
	rep(i,M){
        int a,b;
        scan(a>>b);
        a--; b--;
        uf.unite(i+1,a,b);
	}
	int Q;
	scan(Q);
	rep(q,Q){
		int x,y;
        scan(x>>y);
        x--; y--;
        if(!uf.same(M+1,x,y)) prin(-1);
        else{
            int tmin=0,tmax=M+1;
            while(tmin+1<tmax){
                int tmid=(tmin+tmax)/2;
                if(uf.same(tmid,x,y)) tmax=tmid;
                else tmin=tmid;
            }
            prin(tmax);
        }
	}
	return 0;
}

Submission Info

Submission Time
Task H - Union Sets
User gojira_ku
Language C++ (GCC 9.2.1)
Score 600
Code Size 3957 Byte
Status AC
Exec Time 305 ms
Memory 11084 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 8 ms 3496 KB
sample_02.txt AC 2 ms 3368 KB
sample_03.txt AC 2 ms 3400 KB
subtask_1_1.txt AC 2 ms 3360 KB
subtask_1_10.txt AC 37 ms 3360 KB
subtask_1_11.txt AC 154 ms 3712 KB
subtask_1_12.txt AC 14 ms 6260 KB
subtask_1_13.txt AC 6 ms 3360 KB
subtask_1_14.txt AC 3 ms 3512 KB
subtask_1_15.txt AC 65 ms 3580 KB
subtask_1_16.txt AC 12 ms 3384 KB
subtask_1_17.txt AC 25 ms 3364 KB
subtask_1_18.txt AC 3 ms 3404 KB
subtask_1_19.txt AC 18 ms 5804 KB
subtask_1_2.txt AC 2 ms 3364 KB
subtask_1_20.txt AC 3 ms 3456 KB
subtask_1_21.txt AC 19 ms 3452 KB
subtask_1_22.txt AC 6 ms 4684 KB
subtask_1_23.txt AC 193 ms 9468 KB
subtask_1_24.txt AC 191 ms 9472 KB
subtask_1_25.txt AC 190 ms 9436 KB
subtask_1_26.txt AC 196 ms 9440 KB
subtask_1_27.txt AC 276 ms 10836 KB
subtask_1_28.txt AC 277 ms 10660 KB
subtask_1_29.txt AC 187 ms 9428 KB
subtask_1_3.txt AC 6 ms 3360 KB
subtask_1_30.txt AC 190 ms 9432 KB
subtask_1_31.txt AC 192 ms 9468 KB
subtask_1_32.txt AC 196 ms 9336 KB
subtask_1_33.txt AC 284 ms 11084 KB
subtask_1_34.txt AC 297 ms 10920 KB
subtask_1_35.txt AC 191 ms 9440 KB
subtask_1_36.txt AC 190 ms 9444 KB
subtask_1_37.txt AC 189 ms 9440 KB
subtask_1_38.txt AC 195 ms 9596 KB
subtask_1_39.txt AC 235 ms 10384 KB
subtask_1_4.txt AC 26 ms 7556 KB
subtask_1_40.txt AC 241 ms 10372 KB
subtask_1_41.txt AC 164 ms 3352 KB
subtask_1_42.txt AC 174 ms 3452 KB
subtask_1_43.txt AC 266 ms 8152 KB
subtask_1_44.txt AC 305 ms 10660 KB
subtask_1_45.txt AC 164 ms 3384 KB
subtask_1_46.txt AC 190 ms 9436 KB
subtask_1_5.txt AC 32 ms 3468 KB
subtask_1_6.txt AC 13 ms 6260 KB
subtask_1_7.txt AC 3 ms 3612 KB
subtask_1_8.txt AC 7 ms 3972 KB
subtask_1_9.txt AC 43 ms 3848 KB