Submission #19537038


Source Code Expand

Copy
#pragma GCC optimize("O3")

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif

#pragma region Macros

#define FOR(i, l, r) for(int i = (l) ;i < (r) ;i++)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n+1)
#define RFOR(i, l, r) for(int i = (l) ; i >= (r) ; i--)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n , 1)
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = int>
using V = vector<T>;
template <class T = int>
using VV = V<V<T>>;
#define ll long long
using ld = long double;
#define pll pair<ll,ll>
#define pii pair<int,int>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))

#define VEC(type, name, size) \
	V<type> name(size);       \
	INPUT(name)
#define VVEC(type, name, h, w)    \
	VV<type> name(h, V<type>(w)); \
	INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define LL(...)     \
	ll __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define STR(...)        \
	string __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define CHAR(...)     \
	char __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define DOUBLE(...)     \
	DOUBLE __VA_ARGS__; \
	INPUT(__VA_ARGS__)
#define LD(...)     \
	LD __VA_ARGS__; \
	INPUT(__VA_ARGS__)

void scan(int &a) { cin >> a;}
void scan(long long &a) { cin >> a;}
void scan(char &a) { cin >> a;}
void scan(double &a) { cin >> a;}
void scan(long double &a) { cin >> a;}
void scan(char a[]) { scanf("%s", a);}
void scan(string &a) { cin >> a;}
template <class T>
void scan(V<T> &);
template <class T, class L>
void scan(pair<T, L> &);
template <class T>
void scan(V<T> &a){
	for(auto &i : a) scan(i);
}
template <class T, class L>
void scan(pair<T, L> &p){
	scan(p.first);
	scan(p.second);
}
template <class T>
void scan(T &a) { cin >> a;}
void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail){
	scan(head);
	INPUT(tail...);
}
template <class T>
inline void print(T x) { cout << x << '\n'; }
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 <class T>
ostream &operator<<(ostream &os, const V<T> &v) {
	REP(i, SZ(v)) {
		if(i) os << " ";
		os << v[i];
	}
	return os;
}
//debug
template <typename T>
void view(const V<T> &v) {
	cerr << "{ ";
	for(const auto &e : v) {
		cerr << e << ", ";
	}
	cerr << "\b\b }";
}
template <typename T>
void view(const VV<T> &vv) {
	cerr << "{\n";
	for(const auto &v : vv) {
		cerr << "\t";
		view(v);
		cerr << "\n";
	}
	cerr << "}";
}
template <typename T, typename U>
void view(const V<pair<T, U>> &v) {
	cerr << "{\n";
	for(const auto &c : v) cerr << "\t(" << c.first << ", " << c.second << ")\n";
	cerr << "}";
}
template <typename T, typename U>
void view(const map<T, U> &m) {
	cerr << "{\n";
	for(auto &t : m) cerr << "\t[" << t.first << "] : " << t.second << "\n";
	cerr << "}";
}
template <typename T, typename U>
void view(const pair<T, U> &p) { cerr << "(" << p.first << ", " << p.second << ")"; }
template <typename T>
void view(const set<T> &s) {
	cerr << "{ ";
	for(auto &t : s) {
		view(t);
		cerr << ", ";
	}
	cerr << "\b\b }";
}
template <typename T>
void view(T e) { cerr << e; }
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	view(H);
	cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                           \
	do {                                                     \
		cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                              \
		cerr << "\b\b]\n";                                   \
	} while(0)
#else
#define debug(...) (void(0))
#endif
template <class T>
V<T> press(V<T> &x) {
	V<T> res = x;
	sort(all(res));
	res.erase(unique(all(res)), res.end());
	REP(i, SZ(x)) {
		x[i] = lower_bound(all(res), x[i]) - res.begin();
	}
	return res;
}
template <class T>
inline bool chmin(T &a, T b) {
	if(a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b) {
	if(a < b) {
		a = b;
		return true;
	}
	return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; }
inline void err(bool b = true) {
	if(b) {
		cout << -1 << '\n';
		exit(0);
	}
}
template <class T>
inline void fin(bool b = true, T e = 0) {
	if(b) {
		cout << e << '\n';
		exit(0);
	}
}
template <class T>
T divup(T x, T y) { return (x + (y - 1)) / y; }
template <typename T>
T pow(T a, long long n, T e = 1) {
	T ret = e;
	while(n) {
		if(n & 1) ret *= a;
		a *= a;
		n >>= 1;
	}
	return ret;
}
struct iofast {
	iofast() {
		ios_base::sync_with_stdio(false);
		cin.tie(nullptr);
		cout << fixed << setprecision(15);
	}
} iofast_;
const int inf = 1e9;
const ll INF = 1e18;
#pragma endregion


inline int topbit(unsigned long long x){
	return x?63-__builtin_clzll(x):-1;
}

inline int popcount(unsigned long long x){
	return __builtin_popcountll(x);
}

inline int parity(unsigned long long x){//popcount%2
	return __builtin_parity(x);
}

ll now=0;
class PPUF{
    public:
    vector<ll> rank,p,sz,time;
    PPUF(){}
    PPUF(ll size){ //作られうる木の頂点数の最大値を入れる。
        rank.resize(size,0);
        p.resize(size,0);
        sz.resize(size,1);
        time.resize(size,INF);
        REP(i,size) makeSet(i);
    }
    void makeSet(ll x){ //xだけが属する木を作る。
        p[x]=x;
        rank[x]=0;
    }
    bool same(ll x,ll y,ll t){ //xとyが同じ木に属するかどうか
        return findSet(x,t)==findSet(y,t);
    }
    void unite(ll x, ll y){
        ++now;
        link(findSet(x,now),findSet(y,now));
    }
    void link(ll x, ll y){ //rankが大きい方の根に小さい方の根をつける。
        if(x==y) return ;
        if(sz[x]<sz[y]) swap(x,y);
        sz[x]+=sz[y];
        p[y]=x;
        time[y]=now;
        return ;
    }
    ll findSet(ll x,ll t){ //xが属する木の根の番号を返す
        if(time[x]>t){
            return x;
        }
        else{
            return findSet(p[x],t);
        }
    }
};

int main(){
    LL(N,M);
    V<int> a(M),b(M);
    PPUF pf=PPUF(N);
    REP(i,M){
        cin >> a[i] >> b[i];
        a[i]--;
        b[i]--;
        if(!pf.same(a[i],b[i],now)) pf.unite(a[i],b[i]);
        else now++;
    }
    LL(Q);
    V<int> x(Q),y(Q);
    REP(i,Q){
        cin >> x[i] >> y[i];
        x[i]--;
        y[i]--;
        ll ng=0,ok=M+1;
        while(ok-ng>1){
            ll mid=(ok+ng)/2;
            if(pf.same(x[i],y[i],mid)) ok=mid;
            else ng=mid;
        }
        if(ok==M+1) cout << -1 << endl;
        else cout << ok << endl;
    }
}

Submission Info

Submission Time
Task H - Union Sets
User unos
Language C++ (GCC 9.2.1)
Score 600
Code Size 7601 Byte
Status AC
Exec Time 94 ms
Memory 7892 KB

Compile Error

./Main.cpp:10: warning: ignoring #pragma region Macros [-Wunknown-pragmas]
   10 | #pragma region Macros
      | 
./Main.cpp:240: warning: ignoring #pragma endregion  [-Wunknown-pragmas]
  240 | #pragma endregion
      | 
./Main.cpp: In function ‘void scan(char*)’:
./Main.cpp:69:28: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   69 | void scan(char a[]) { scanf("%s", a);}
      |                       ~~~~~^~~~~~~~~

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 3576 KB
sample_02.txt AC 3 ms 3620 KB
sample_03.txt AC 3 ms 3528 KB
subtask_1_1.txt AC 2 ms 3644 KB
subtask_1_10.txt AC 23 ms 3684 KB
subtask_1_11.txt AC 46 ms 4492 KB
subtask_1_12.txt AC 5 ms 4688 KB
subtask_1_13.txt AC 9 ms 3668 KB
subtask_1_14.txt AC 3 ms 3632 KB
subtask_1_15.txt AC 20 ms 3644 KB
subtask_1_16.txt AC 6 ms 3532 KB
subtask_1_17.txt AC 6 ms 3684 KB
subtask_1_18.txt AC 3 ms 3620 KB
subtask_1_19.txt AC 7 ms 4384 KB
subtask_1_2.txt AC 2 ms 3472 KB
subtask_1_20.txt AC 2 ms 3536 KB
subtask_1_21.txt AC 3 ms 3572 KB
subtask_1_22.txt AC 3 ms 4044 KB
subtask_1_23.txt AC 41 ms 7024 KB
subtask_1_24.txt AC 41 ms 7128 KB
subtask_1_25.txt AC 35 ms 6992 KB
subtask_1_26.txt AC 42 ms 7096 KB
subtask_1_27.txt AC 78 ms 7892 KB
subtask_1_28.txt AC 81 ms 7892 KB
subtask_1_29.txt AC 36 ms 7096 KB
subtask_1_3.txt AC 4 ms 3580 KB
subtask_1_30.txt AC 39 ms 7000 KB
subtask_1_31.txt AC 37 ms 7024 KB
subtask_1_32.txt AC 43 ms 7148 KB
subtask_1_33.txt AC 82 ms 7792 KB
subtask_1_34.txt AC 82 ms 7892 KB
subtask_1_35.txt AC 36 ms 7124 KB
subtask_1_36.txt AC 37 ms 7168 KB
subtask_1_37.txt AC 38 ms 7164 KB
subtask_1_38.txt AC 42 ms 7168 KB
subtask_1_39.txt AC 64 ms 7888 KB
subtask_1_4.txt AC 11 ms 5496 KB
subtask_1_40.txt AC 62 ms 7856 KB
subtask_1_41.txt AC 28 ms 3996 KB
subtask_1_42.txt AC 36 ms 3908 KB
subtask_1_43.txt AC 82 ms 6612 KB
subtask_1_44.txt AC 94 ms 7836 KB
subtask_1_45.txt AC 29 ms 3924 KB
subtask_1_46.txt AC 34 ms 7024 KB
subtask_1_5.txt AC 13 ms 3696 KB
subtask_1_6.txt AC 4 ms 4764 KB
subtask_1_7.txt AC 2 ms 3796 KB
subtask_1_8.txt AC 3 ms 3976 KB
subtask_1_9.txt AC 13 ms 3820 KB