This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
#include "werewolf.h"
struct Dsu {
vector <int> p, cp;
int n;
int get(int u) {
return (u == cp[u] ? u : cp[u] = get(cp[u]));
}
void join(int u, int v) {
u = get(u);
v = get(v);
if (u != v) {
int x = n++;
p.push_back(x);
cp.push_back(x);
p[u] = p[v] = cp[u] = cp[v] = x;
}
}
Dsu(int nn) {
n = nn;
p.resize(n), cp.resize(n);
for (int i = 0; i < n; ++i) {
p[i] = i;
cp[i] = i;
}
}
};
std::vector<int> check_validity(int n, std::vector<int> x, std::vector<int> y, std::vector<int> s, std::vector<int> e, std::vector<int> l, std::vector<int> r) {
vector <vector <int>> g(n);
for (int i = 0; i < (int)x.size(); ++i) {
g[x[i]].push_back(y[i]);
g[y[i]].push_back(x[i]);
}
int q = s.size();
vector <vector <int>> who_l(n), who_r(n);
for (int i = 0; i < n; ++i) {
who_l[l[i]].push_back(i);
who_r[r[i]].push_back(i);
}
vector <int> where_st(q), where_en(q);
Dsu st(n), en(n);
for (int i = n - 1; i >= 0; --i) {
for (auto v : g[i]) {
if (v >= i) {
st.join(i, v);
}
}
for (auto id : who_l[i]) {
where_st[id] = st.get(s[id]);
}
}
for (int i = 0; i < n; ++i) {
for (auto v : g[i]) {
if (v <= i) {
en.join(i, v);
}
}
for (auto id : who_r[i]) {
where_en[id] = en.get(e[id]);
}
}
vector <vector <bool>> is_s_par(st.n, vector <bool> (n)), is_e_par(en.n, vector <bool> (n));
for (int i = 0; i < n; ++i) {
int u = i;
while (true) {
is_s_par[u][i] = 1;
int p = st.p[u];
if (p == u) break;
u = p;
}
}
for (int i = 0; i < n; ++i) {
int u = i;
while (true) {
is_e_par[u][i] = 1;
int p = en.p[u];
if (p == u) break;
u = p;
}
}
vector <int> ans(q);
for (int i = 0; i < q; ++i) {
for (int j = 0; j < n; ++j) {
if (is_s_par[where_st[i]][j] && is_e_par[where_en[i]][j]) {
ans[i] = 1;
}
}
}
return ans;
}
#ifdef LOCAL
namespace {
int read_int() {
int x;
cin >> x;
return x;
}
} // namespace
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int N = read_int();
int M = read_int();
int Q = read_int();
std::vector<int> X(M), Y(M), S(Q), E(Q), L(Q), R(Q);
for (int j = 0; j < M; ++j) {
X[j] = read_int();
Y[j] = read_int();
}
for (int i = 0; i < Q; ++i) {
S[i] = read_int();
E[i] = read_int();
L[i] = read_int();
R[i] = read_int();
}
std::vector<int> A = check_validity(N, X, Y, S, E, L, R);
for (size_t i = 0; i < A.size(); ++i) {
printf("%d\n", A[i]);
}
}
#endif
# | 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... |