Submission #18940724
Source Code Expand
Copy
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL,LL> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<char> VC;
typedef vector<VC> VVC;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
template<typename T>
using prque = priority_queue<T,vector<T>>;
template<typename T>
using prquer = priority_queue<T,vector<T>,greater<T>>;
#define umap unordered_map
#define uset unordered_set
#define umset unordered_multiset
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EB emplace_back
#define EF emplace_front
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define MP make_pair
#define MT make_tuple
#define SZ(a) (int)((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define SORTR(c) sort((c).rbegin(), (c).rend())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define LB lower_bound
#define UB upper_bound
#define NEXP(a) next_permutation((a).begin(),(a).end())
#define FI first
#define SE second
#define FOR(i,a,b) for(auto i = decltype(b){a}; i<(b); i++)
#define REP(i,n) FOR(i,0,n)
#define FORR(i,a,b) for(auto i=decltype(b){b-1}; i>=(a); i--)
#define REPR(i,n) FORR(i,0,n)
#define CFOR(i,a,b) for(auto i = decltype(b){a}; i<=(b); i++)
#define CREP(i,n) CFOR(i,0,n)
#define SREP(i,n) CFOR(i,1,n)
#define CFORR(i,a,b) for(auto i=decltype(b){b}; i>=(a); i--)
#define CREPR(i,n) CFORR(i,0,n)
#define SREPR(i,n) CFORR(i,1,n)
#define BFOR(bit,a,b) for(LL bit=(a); bit<(1ll<<(b)); bit++)
#define BREP(bit,n) BFOR(bit,0,n)
#define EACH(ptr,c) for(auto ptr=(c).begin(); ptr!=(c).end(); )
#define EACHR(ptr,c) for(auto ptr=(c).rbegin(); ptr!=(c).rend(); )
constexpr double EPS = 1e-10;
constexpr double PI = 3.141592653589793238462643383279;
constexpr int INF = numeric_limits<int>::max()/2;
constexpr LL LINF = numeric_limits<LL>::max()/3;
constexpr int RINF = numeric_limits<int>::min()/2;
constexpr LL RLINF = numeric_limits<LL>::min()/3;
constexpr LL MOD = 1e9+7;
constexpr LL MODD = 998244353;
constexpr int MAX = 510000;
template<class T> inline T sqr(T x) { return x*x; }
inline bool Eq(double a, double b) { return fabs(b - a) < EPS; }
template<class T> inline T CEIL(T x, T y) { return (x+y-1)/y; }
inline int PCnt(uint64_t x) { return __builtin_popcountll(x); }
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
inline LL toLL(string s) {LL v; istringstream sin(s);sin>>v;return v;}
inline double toDouble(string s) {double v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toStr(T x) {ostringstream sout;sout<<x;return sout.str();}
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 (b<a) { a=b; return 1; } return 0; }
template<typename T>
T Vsum(vector<T> &v){return reduce(v.begin(),v.end()); }
template<typename T>
T Vgcd(vector<T> &v){return reduce(v.begin(),v.end(),T(0),[](auto&&a,auto&&b){return gcd(a,b);}); }
template<typename T>
T Vlcm(vector<T> &v){return reduce(v.begin(),v.end(),T(1),[](auto&&a,auto&&b){return lcm(a,b);}); }
template<typename T> T Vmax(vector<T> &v){return *max_element(v.begin(),v.end()); }
template<typename T> T Vmin(vector<T> &v){return *min_element(v.begin(),v.end()); }
template<typename T> void Vadd(vector<T> &v, T a){for(auto&& x : v) x += a; }
template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>
auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...)); }
const vector<int> dx = {1,0,-1,0,1,1,-1,-1};
const vector<int> dy = {0,1,0,-1,1,-1,1,-1};
#define debug(x) cerr << #x <<" : "<< x << endl;
bool cYN(bool fl=true){cout << (fl?"Yes":"No") << endl; return fl; }
bool CYN(bool fl=true){cout << (fl?"YES":"NO") << endl; return fl; }
void error(bool fl=true){cout << -1 << endl; if(fl){ exit(0);} }
template<class T> void COUT(T&& t){ cout << t << endl; }
template<class T,class... Ts>
void COUT(T&& t,Ts&&... ts){ cout << t << " "; COUT(forward<Ts>(ts)...); }
template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
is >> p.first >> p.second; return is;
}
template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
os << p.first << " " << p.second; return os;
}
template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
for(T &in : v) is >> in;
return is;
}
template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
for(int i = 0; i < SZ(v); i++) os << v[i] << (i + 1 != SZ(v) ? " " : "");
return os;
}
struct PartiallyPersistentUF {
VI par, siz, last;
int now;
vector<VPII> hist;
PartiallyPersistentUF() {}
PartiallyPersistentUF(int n): par(n,-1),siz(n,1),last(n,INF),now(0) {
iota(ALL(par),0);
hist.assign(n,VPII(1,{0,1}));
}
void init(int n){
par.resize(n);
iota(ALL(par),0);
siz.assign(n,1);
last.assign(n,INF);
now = 0;
hist.assign(n,VPII(1,{0,1}));
}
int find(int t,int x) {
while(last.at(x)<=t) x = par.at(x);
return x;
}
pair<bool,int> unite(int x, int y){
x = find(now, x);
y = find(now, y);
now++;
if(x == y) return {false,now-1};
if(siz[x] < siz[y]){
swap(x,y);
}
siz[x] += siz[y];
par[y] = x;
last[y] = now;
hist.at(x).EB(now, siz[x]);
return {true, now-1};
}
bool same(int t, int x, int y){
return find(t, x) == find(t, y);
}
int size(int t, int x){
x = find(t, x);
return prev(partition_point(
ALL(hist.at(x)),
[t](PII &h){ return h.FI<=t;})
)->SE;
}
};
int main() {
// cin.tie(0);
// ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
int N, M;
cin >> N >> M;
PartiallyPersistentUF uf(N);
REP(i,M){
int a, b; cin >> a >> b; a--; b--;
uf.unite(a,b);
}
int Q; cin >> Q;
while(Q--){
int x, y; cin >> x >> y; x--; y--;
auto isok = [&](int t){
return uf.same(t, x, y);
};
int ok = M+1, ng = -1;
while(abs(ok-ng) > 1){
int wj = (ok + ng)/2;
if(isok(wj)) ok = wj;
else ng = wj;
}
int res = ok; if(res > M) res = -1;
COUT(res);
}
return 0;
}
/*
制約は見ましたか?
実行時間制限は見ましたか?
サンプルは試しましたか?
cat in.txt | .\a.exe > out.txt
C++になっていますか?
//*/
Submission Info
Submission Time |
|
Task |
H - Union Sets |
User |
Suu0313 |
Language |
C++ (GCC 9.2.1) |
Score |
600 |
Code Size |
6939 Byte |
Status |
AC |
Exec Time |
287 ms |
Memory |
11280 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 |
18 ms |
3572 KB |
sample_02.txt |
AC |
2 ms |
3436 KB |
sample_03.txt |
AC |
2 ms |
3464 KB |
subtask_1_1.txt |
AC |
2 ms |
3480 KB |
subtask_1_10.txt |
AC |
28 ms |
3508 KB |
subtask_1_11.txt |
AC |
153 ms |
3604 KB |
subtask_1_12.txt |
AC |
13 ms |
6336 KB |
subtask_1_13.txt |
AC |
12 ms |
3580 KB |
subtask_1_14.txt |
AC |
3 ms |
3456 KB |
subtask_1_15.txt |
AC |
62 ms |
3576 KB |
subtask_1_16.txt |
AC |
8 ms |
3488 KB |
subtask_1_17.txt |
AC |
25 ms |
3576 KB |
subtask_1_18.txt |
AC |
3 ms |
3508 KB |
subtask_1_19.txt |
AC |
15 ms |
5728 KB |
subtask_1_2.txt |
AC |
2 ms |
3468 KB |
subtask_1_20.txt |
AC |
3 ms |
3580 KB |
subtask_1_21.txt |
AC |
20 ms |
3520 KB |
subtask_1_22.txt |
AC |
6 ms |
4700 KB |
subtask_1_23.txt |
AC |
194 ms |
9716 KB |
subtask_1_24.txt |
AC |
193 ms |
9632 KB |
subtask_1_25.txt |
AC |
194 ms |
9772 KB |
subtask_1_26.txt |
AC |
201 ms |
9644 KB |
subtask_1_27.txt |
AC |
277 ms |
11092 KB |
subtask_1_28.txt |
AC |
278 ms |
10948 KB |
subtask_1_29.txt |
AC |
197 ms |
9644 KB |
subtask_1_3.txt |
AC |
3 ms |
3492 KB |
subtask_1_30.txt |
AC |
192 ms |
9728 KB |
subtask_1_31.txt |
AC |
194 ms |
9696 KB |
subtask_1_32.txt |
AC |
203 ms |
9632 KB |
subtask_1_33.txt |
AC |
284 ms |
11224 KB |
subtask_1_34.txt |
AC |
281 ms |
11280 KB |
subtask_1_35.txt |
AC |
192 ms |
9692 KB |
subtask_1_36.txt |
AC |
203 ms |
9632 KB |
subtask_1_37.txt |
AC |
191 ms |
9692 KB |
subtask_1_38.txt |
AC |
198 ms |
9944 KB |
subtask_1_39.txt |
AC |
240 ms |
10808 KB |
subtask_1_4.txt |
AC |
18 ms |
7784 KB |
subtask_1_40.txt |
AC |
240 ms |
10784 KB |
subtask_1_41.txt |
AC |
164 ms |
3460 KB |
subtask_1_42.txt |
AC |
174 ms |
3472 KB |
subtask_1_43.txt |
AC |
267 ms |
8516 KB |
subtask_1_44.txt |
AC |
287 ms |
11272 KB |
subtask_1_45.txt |
AC |
172 ms |
3428 KB |
subtask_1_46.txt |
AC |
188 ms |
9772 KB |
subtask_1_5.txt |
AC |
27 ms |
3584 KB |
subtask_1_6.txt |
AC |
18 ms |
6668 KB |
subtask_1_7.txt |
AC |
4 ms |
3676 KB |
subtask_1_8.txt |
AC |
5 ms |
4064 KB |
subtask_1_9.txt |
AC |
45 ms |
3824 KB |