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 <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
const int maxn = 15e4+5;
int p[maxn], ch[maxn];
struct dsu_rollback
{
int cnt;
dsu_rollback(int n)
{
for (int i = 0; i < n; i++) p[i] = i;
cnt = 0;
}
int root(int i) { return p[i] == i ? i : root(p[i]); }
void merge(int i, int j)
{
i = root(i), j = root(j);
if (i == j) return;
if (rand() & 1) swap(i, j);
p[j] = i;
ch[cnt++] = j;
}
void undo(int siz)
{
while (cnt > siz)
{
int v = ch[--cnt];
p[v] = v;
}
}
};
vector<int> ok, on;
vector<vector<int> > oa, ob, g; // vyskyty a-cok a vyskty b-cok
struct udaj
{
int l, r, u;
};
bool dandc(int l, int r, vector<udaj> &v, dsu_rollback &d)
{
int siz = d.cnt;
int m = (l + r) / 2;
vector<udaj> v2;
for (udaj u : v)
{
if (u.r < l || u.l > r) continue;
if (u.l <= l && r <= u.r)
{
on[u.u] = 1;
for (int i : g[u.u]) if (on[i]) d.merge(u.u, i);
continue;
}
v2.push_back(u);
}
bool ans = true;
if (l == r)
{
for (int x : oa[l]) ok[d.root(x)] = 1;
for (int x : ob[l]) if (!ok[d.root(x)]) ans = false;
for (int x : oa[l]) ok[d.root(x)] = 0;
}
else ans = dandc(l, m, v2, d) && dandc(m + 1, r, v2, d);
for (udaj u : v) if (u.l <= l && r <= u.r) on[u.u] = 0;
d.undo(siz);
return ans;
}
int solve()
{
int n, m;
scanf("%d%d", &n, &m);
oa.assign(n, {}), ob.assign(n, {}), g.assign(n, {}), ok.assign(n, 0), on.assign(n, 0);
vector<int> a(n), b(n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]), oa[--a[i]].push_back(i);
for (int i = 0; i < n; i++) scanf("%d", &b[i]), ob[--b[i]].push_back(i);
for (int i = 0, u, v; i < m; i++) scanf("%d%d", &u, &v), g[--u].push_back(--v), g[v].push_back(u);
vector<udaj> v;
for (int i = 0; i < n; i++)
{
if (a[i] < b[i]) return 0;
v.push_back({ b[i], a[i], i });
}
dsu_rollback d(n);
return dandc(0, n - 1, v, d);
}
int main()
{
int t;
scanf("%d", &t);
while (t--) printf("%d\n", solve());
return 0;
}
Compilation message (stderr)
colors.cpp: In function 'int solve()':
colors.cpp:76:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
colors.cpp:79:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | for (int i = 0; i < n; i++) scanf("%d", &a[i]), oa[--a[i]].push_back(i);
| ~~~~~^~~~~~~~~~~~~
colors.cpp:80:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | for (int i = 0; i < n; i++) scanf("%d", &b[i]), ob[--b[i]].push_back(i);
| ~~~~~^~~~~~~~~~~~~
colors.cpp:81:44: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | for (int i = 0, u, v; i < m; i++) scanf("%d%d", &u, &v), g[--u].push_back(--v), g[v].push_back(u);
| ~~~~~^~~~~~~~~~~~~~~~
colors.cpp: In function 'int main()':
colors.cpp:94:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | scanf("%d", &t);
| ~~~~~^~~~~~~~~~
# | 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... |
# | 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... |