#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
struct dsu_rollback
{
vector<int> p, s, ch;
dsu_rollback(int n)
{
p.resize(n), s.resize(n, 1);
for (int i = 0; i < n; i++) p[i] = i;
}
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 (s[i] < s[j]) swap(i, j);
p[j] = i, s[i] += s[j], ch.push_back(j);
}
void undo(int siz)
{
while (ch.size() > siz)
{
int v = ch.back(); ch.pop_back();
s[p[v]] -= s[v], 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.ch.size();
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;
return ans;
}
int solve()
{
int n, m;
cin >> 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++) cin >> a[i], oa[--a[i]].push_back(i);
for (int i = 0; i < n; i++) cin >> b[i], ob[--b[i]].push_back(i);
for (int i = 0, u, v; i < m; i++) cin >> 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()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) cout << solve() << "\n";
return 0;
}
Compilation message
colors.cpp: In member function 'void dsu_rollback::undo(int)':
colors.cpp:29:26: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
29 | while (ch.size() > siz)
| ~~~~~~~~~~^~~~~
colors.cpp: In function 'bool dandc(int, int, std::vector<udaj>&, dsu_rollback&)':
colors.cpp:44:9: warning: unused variable 'siz' [-Wunused-variable]
44 | int siz = d.ch.size();
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
476 KB |
Output is correct |
2 |
Correct |
20 ms |
344 KB |
Output is correct |
3 |
Correct |
3 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
73 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
73 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
476 KB |
Output is correct |
2 |
Correct |
20 ms |
344 KB |
Output is correct |
3 |
Correct |
3 ms |
768 KB |
Output is correct |
4 |
Incorrect |
73 ms |
344 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
133 ms |
496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
476 KB |
Output is correct |
2 |
Correct |
20 ms |
344 KB |
Output is correct |
3 |
Correct |
3 ms |
768 KB |
Output is correct |
4 |
Correct |
43 ms |
344 KB |
Output is correct |
5 |
Incorrect |
73 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |