#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"
using ll = long long;
using ld = long double;
template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);}
const int N = 4e5 + 5;
int n, m, q, parQ[N];
vector<int> Edge[N], fPar[N], Quer[N];
namespace krt_mn{
int dsu[N], krt_sz, tin[N], tout[N], timerDFS;
vector<int> tree[N];
void init(){
krt_sz = n - 1;
FOR(i, 0, n - 1) dsu[i] = i;
}
int asc(int x){
return dsu[x] == x ? dsu[x] : dsu[x] = asc(dsu[x]);
}
void join(int u, int v){
u = asc(u), v = asc(v);
if(u != v){
++krt_sz;
// cout << "Tree Min: " << u <<" " << v <<" " << krt_sz << el;
dsu[u] = dsu[v] = dsu[krt_sz] = krt_sz;
tree[krt_sz].eb(u), tree[krt_sz].eb(v);
}
}
void dfs(int x, int p){
tin[x] = ++timerDFS;
for(int v : tree[x])if(v != p){
dfs(v, x);
}
tout[x] = ++timerDFS;
}
void get_order(){
dfs(krt_sz, -1);
}
}
namespace krt_mx{
int dsu[N], krt_sz;
set<int> val[N];
void init(){
krt_sz = n - 1;
FOR(i, 0, n - 1){
dsu[i] = i;
val[i].insert(krt_mn::tin[i]);
}
}
int asc(int x){
return x == dsu[x] ? x : dsu[x] = asc(dsu[x]);
}
void join(int u, int v){
u = asc(u), v = asc(v);
if(u != v){
++krt_sz;
//cout << "Tree Max: " << u <<" " << v <<" " << krt_sz << el;
dsu[u] = dsu[v] = dsu[krt_sz] = krt_sz;
// merge 2 set
if(sz(val[u]) < sz(val[v])) swap(u, v);
swap(val[krt_sz], val[u]);
for(int x : val[v]) val[krt_sz].insert(x);
}
}
int query(int node, int vMn){ // some node that inside both node in (tree max) and vMn in (tree min)
auto it = val[node].lower_bound(krt_mn::tin[vMn]); // S and E can be in the same component so we should lowerbound :(
if(it != val[node].end() && *it < krt_mn::tout[vMn]){
return 1;
}
return 0;
}
}
vector<int> check_validity(int _n, vector<int> X, vector<int> Y,
vector<int> S, vector<int> E, vector<int> L, vector<int> R){
n = _n, m = sz(X), q = sz(S);
FOR(i, 0, m - 1){
Edge[X[i]].eb(Y[i]);
Edge[Y[i]].eb(X[i]);
}
FOR(i, 0, q - 1){
Quer[L[i]].eb(i);
fPar[R[i]].eb(i);
}
vector<int> ans(q);
krt_mn::init();
FOR(i, 0, n - 1){
for(int v : Edge[i]){
if(v < i) krt_mn::join(i, v);
}
for(int id : fPar[i]){
E[id] = krt_mn::asc(E[id]);
}
}
krt_mn::get_order();
krt_mx::init();
ROF(i, n - 1, 0){
for(int v : Edge[i]){
if(v > i) krt_mx::join(i, v);
}
for(int id : Quer[i]){
S[id] = krt_mx::asc(S[id]);
ans[id] = krt_mx::query(S[id], E[id]);
}
}
return ans;
}
//#define name "InvMOD"
#ifdef name
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(name".INP", "r")){
freopen(name".INP","r",stdin);
freopen(name".OUT","w",stdout);
}
int n,m,q; cin >> n >> m >> q;
vector<vector<int>> E(2, vector<int>(m));
FOR(i, 0, 1){
FOR(j, 0, m - 1){
cin >> E[i][j];
}
}
vector<vector<int>> Q(4, vector<int>(q));
FOR(i, 0, 3){
FOR(j, 0, q - 1){
cin >> Q[i][j];
}
}
vector<int> answer = check_validity(n, E[0], E[1], Q[0], Q[1], Q[2], Q[3]);
for(int v : answer) cout << v <<" "; cout <<"\n";
return 0;
}
#endif // name
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |