Submission #19536429


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>>;
template <class T = int>
using VVV = V<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);
}

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

int main(){
    LL(N,M);
    V<int> a(M),b(M);
    REP(i,M){
        cin >> a[i] >> b[i];
        a[i]--;
        b[i]--;
    }
    LL(Q);
    V<int> x(Q),y(Q);
    REP(i,Q){
        cin >> x[i] >> y[i];
        x[i]--;
        y[i]--;
    }
    V<int> ok(Q,M),ng(Q,-1);
    while(true){
        bool finish=true;
        VV<int> mid_ind(M);
        REP(i,Q){
            if(ok[i]-ng[i]>1){
                finish=false;
                int mid=(ok[i]+ng[i])/2;
                mid_ind[mid].push_back(i);
            }
        }
        if(finish) break;
        DisjointSet ds=DisjointSet(N);
        REP(i,M){
            ds.unite(a[i],b[i]);
            for(auto k:mid_ind[i]){
                if(ds.same(x[k],y[k])) ok[k]=i;
                else ng[k]=i;
            }
        }
    }
    REP(i,Q){
        if(ok[i]==M){
            cout << -1 << endl;
        }
        else{
            cout << ok[i]+1 << endl;
        }
    }
    return 0;
}

Submission Info

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

Compile Error

./Main.cpp:9: warning: ignoring #pragma region Macros [-Wunknown-pragmas]
    9 | #pragma region Macros
      | 
./Main.cpp:241: warning: ignoring #pragma endregion  [-Wunknown-pragmas]
  241 | #pragma endregion
      | 
./Main.cpp: In function ‘void scan(char*)’:
./Main.cpp:70:28: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   70 | 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 3636 KB
sample_02.txt AC 2 ms 3548 KB
sample_03.txt AC 2 ms 3492 KB
subtask_1_1.txt AC 2 ms 3584 KB
subtask_1_10.txt AC 31 ms 5060 KB
subtask_1_11.txt AC 62 ms 7400 KB
subtask_1_12.txt AC 10 ms 4460 KB
subtask_1_13.txt AC 9 ms 3816 KB
subtask_1_14.txt AC 3 ms 3528 KB
subtask_1_15.txt AC 26 ms 4304 KB
subtask_1_16.txt AC 8 ms 3780 KB
subtask_1_17.txt AC 9 ms 3852 KB
subtask_1_18.txt AC 2 ms 3616 KB
subtask_1_19.txt AC 14 ms 4428 KB
subtask_1_2.txt AC 2 ms 3548 KB
subtask_1_20.txt AC 2 ms 3668 KB
subtask_1_21.txt AC 6 ms 3684 KB
subtask_1_22.txt AC 8 ms 3976 KB
subtask_1_23.txt AC 40 ms 7912 KB
subtask_1_24.txt AC 47 ms 7864 KB
subtask_1_25.txt AC 54 ms 7896 KB
subtask_1_26.txt AC 67 ms 8300 KB
subtask_1_27.txt AC 153 ms 11200 KB
subtask_1_28.txt AC 145 ms 11204 KB
subtask_1_29.txt AC 40 ms 7924 KB
subtask_1_3.txt AC 4 ms 3460 KB
subtask_1_30.txt AC 49 ms 7904 KB
subtask_1_31.txt AC 52 ms 7984 KB
subtask_1_32.txt AC 69 ms 8236 KB
subtask_1_33.txt AC 140 ms 11212 KB
subtask_1_34.txt AC 138 ms 11204 KB
subtask_1_35.txt AC 41 ms 7800 KB
subtask_1_36.txt AC 45 ms 7964 KB
subtask_1_37.txt AC 52 ms 7988 KB
subtask_1_38.txt AC 67 ms 8248 KB
subtask_1_39.txt AC 129 ms 11348 KB
subtask_1_4.txt AC 21 ms 5216 KB
subtask_1_40.txt AC 134 ms 11252 KB
subtask_1_41.txt AC 30 ms 5448 KB
subtask_1_42.txt AC 37 ms 5452 KB
subtask_1_43.txt AC 96 ms 9472 KB
subtask_1_44.txt AC 129 ms 11228 KB
subtask_1_45.txt AC 23 ms 4728 KB
subtask_1_46.txt AC 33 ms 4736 KB
subtask_1_5.txt AC 28 ms 4980 KB
subtask_1_6.txt AC 5 ms 4352 KB
subtask_1_7.txt AC 2 ms 3708 KB
subtask_1_8.txt AC 3 ms 3736 KB
subtask_1_9.txt AC 13 ms 3964 KB