Submission #7160216
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;
template<typename T> class LinkCutTree {
public:
inline static T opr1(const T arg1, const T arg2){
return max(arg1, arg2);
}
struct node {
int id, al_id;
T val, al;
node *left, *right, *par;
bool rev;
node(){}
node(int _id, T _val) : id(_id), al_id(_id), val(_val), al(_val),
left(nullptr), right(nullptr), par(nullptr), rev(false){}
inline bool isRoot() const {
return (!par) || (par->left != this && par->right != this);
}
void push(){
if(!rev) return;
swap(left, right);
if(left) left->rev = !(left->rev);
if(right) right->rev = !(right->rev);
rev = false;
}
void eval(){
al_id = id, al = val;
if(left){
left->push();
if(al < left->al){
al = left->al, al_id = left->al_id;
}
}
if(right){
right->push();
if(al < right->al){
al = right->al, al_id = right->al_id;
}
}
}
};
private:
void rotate(node* u, bool right){
node *p = u->par, *g = p->par;
if(right){
if((p->left = u->right)) u->right->par = p;
u->right = p, p->par = u;
}else{
if((p->right = u->left)) u->left->par = p;
u->left = p, p->par = u;
}
p->eval(), u->eval(), u->par = g;
if(!g) return;
if(g->left == p) g->left = u;
if(g->right == p) g->right = u;
g->eval();
}
void splay(node* u){
while(!(u->isRoot())){
node *p = u->par, *gp = p->par;
if(p->isRoot()){ // zig
p->push(), u->push();
rotate(u, (u == p->left));
}else{
gp->push(), p->push(), u->push();
bool flag = (u == p->left);
if((u == p->left) == (p == gp->left)){
rotate(p, flag), rotate(u, flag);
}else{
rotate(u, flag), rotate(u, !flag);
}
}
}
u->push();
}
node* access(node* u){
node* last = nullptr;
for(node* v = u; v; v = v->par){
splay(v);
v->right = last;
v->eval();
last = v;
}
splay(u);
return last;
}
void evert(node* u){
access(u);
u->rev = !(u->rev), u->push();
}
bool connected(node* u, node* v){
access(u), access(v);
return u->par;
}
void link(node* u, node* v){
evert(u), u->par = v;
}
void cut(node* u){
access(u);
u->left->par = nullptr, u->left = nullptr, u->eval();
}
void cut(node* u, node* v){
access(u), access(v);
if(u->isRoot()) u->par = nullptr;
else v->left->par = nullptr, v->left = nullptr, v->eval();
}
pair<T, int> query(node* u, node* v){
evert(u), access(v);
return make_pair(v->al, v->al_id);
}
public:
const int V;
node** arr;
LinkCutTree(const int node_size) : V(node_size){
arr = new node*[V];
for(int i = 0; i < V; i++){
arr[i] = new node(-1, numeric_limits<T>::min());
}
}
// ~LinkCutTree(){
// for(int i = 0; i < V; ++i){
// delete arr[i];
// }
// delete[] arr;
// }
static node *make_node(){ return new node(); }
bool connected(int id1, int id2){ return connected(arr[id1], arr[id2]); }
void link(int ver_id, node *edge){ return link(arr[ver_id], edge); }
void cut(int ver_id, node *edge){ return cut(arr[ver_id], edge); }
pair<T, int> query(int id1, int id2){ return query(arr[id1], arr[id2]); }
};
class RetroactiveUnionFind {
private:
const unsigned int V;
LinkCutTree<int> lc;
vector<LinkCutTree<int>::node> edge;
vector<pair<int, int> > nodes;
unsigned int cur_pos;
public:
RetroactiveUnionFind(unsigned int node_size)
: V(node_size), lc(V), edge(V - 1), nodes(V - 1), cur_pos(0u){}
bool unite(const int id1, const int id2, int _time){
unsigned int pos = cur_pos;
if(cur_pos == V - 1 || lc.connected(id1, id2)){
auto res = lc.query(id1, id2);
if(res.first <= _time) return false;
pair<int, int> p = nodes[pos = res.second];
lc.cut(p.first, &edge[pos]), lc.cut(p.second, &edge[pos]);
}else{
++cur_pos;
}
edge[pos] = LinkCutTree<int>::node(pos, _time);
lc.link(id1, &edge[pos]), lc.link(id2, &edge[pos]);
nodes[pos] = {id1, id2};
return true;
}
bool same(const int id1, const int id2, int _time){
if(!lc.connected(id1, id2)) return false;
return lc.query(id1, id2).first <= _time;
}
};
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
RetroactiveUnionFind 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 |
7811 Byte |
Status |
AC |
Exec Time |
438 ms |
Memory |
13440 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
600 / 600 |
Status |
|
|
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 |
33 ms |
256 KB |
subtask_1_11.txt |
AC |
229 ms |
896 KB |
subtask_1_12.txt |
AC |
4 ms |
4096 KB |
subtask_1_13.txt |
AC |
5 ms |
256 KB |
subtask_1_14.txt |
AC |
1 ms |
256 KB |
subtask_1_15.txt |
AC |
52 ms |
512 KB |
subtask_1_16.txt |
AC |
5 ms |
256 KB |
subtask_1_17.txt |
AC |
8 ms |
256 KB |
subtask_1_18.txt |
AC |
2 ms |
256 KB |
subtask_1_19.txt |
AC |
5 ms |
3712 KB |
subtask_1_2.txt |
AC |
1 ms |
256 KB |
subtask_1_20.txt |
AC |
2 ms |
256 KB |
subtask_1_21.txt |
AC |
6 ms |
256 KB |
subtask_1_22.txt |
AC |
3 ms |
2304 KB |
subtask_1_23.txt |
AC |
34 ms |
8320 KB |
subtask_1_24.txt |
AC |
41 ms |
8448 KB |
subtask_1_25.txt |
AC |
46 ms |
8448 KB |
subtask_1_26.txt |
AC |
57 ms |
8832 KB |
subtask_1_27.txt |
AC |
429 ms |
13312 KB |
subtask_1_28.txt |
AC |
438 ms |
13312 KB |
subtask_1_29.txt |
AC |
35 ms |
8320 KB |
subtask_1_3.txt |
AC |
1 ms |
256 KB |
subtask_1_30.txt |
AC |
42 ms |
8320 KB |
subtask_1_31.txt |
AC |
46 ms |
8448 KB |
subtask_1_32.txt |
AC |
58 ms |
8832 KB |
subtask_1_33.txt |
AC |
420 ms |
13312 KB |
subtask_1_34.txt |
AC |
430 ms |
13440 KB |
subtask_1_35.txt |
AC |
34 ms |
8320 KB |
subtask_1_36.txt |
AC |
41 ms |
8320 KB |
subtask_1_37.txt |
AC |
47 ms |
8448 KB |
subtask_1_38.txt |
AC |
64 ms |
10880 KB |
subtask_1_39.txt |
AC |
202 ms |
13312 KB |
subtask_1_4.txt |
AC |
8 ms |
6272 KB |
subtask_1_40.txt |
AC |
194 ms |
13312 KB |
subtask_1_41.txt |
AC |
27 ms |
512 KB |
subtask_1_42.txt |
AC |
73 ms |
512 KB |
subtask_1_43.txt |
AC |
275 ms |
9088 KB |
subtask_1_44.txt |
AC |
310 ms |
13312 KB |
subtask_1_45.txt |
AC |
16 ms |
512 KB |
subtask_1_46.txt |
AC |
25 ms |
8320 KB |
subtask_1_5.txt |
AC |
16 ms |
256 KB |
subtask_1_6.txt |
AC |
4 ms |
4352 KB |
subtask_1_7.txt |
AC |
2 ms |
768 KB |
subtask_1_8.txt |
AC |
2 ms |
1280 KB |
subtask_1_9.txt |
AC |
5 ms |
1024 KB |