#include <bits/stdc++.h>
#define FR(i, N) for (int i = 0; i < int(N); i++)
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;
const int MAXN = (int)(150000) + 5;
int timer = 0;
int N, M;
int a[MAXN];
int b[MAXN];
int par[MAXN];
int rk[MAXN];
int cur[MAXN];
pair<int, int> edges[MAXN + 50000];
vector<int> A[MAXN];
vector<int> B[MAXN];
deque<pair<int, int>> last;
vector<int> seg[4*MAXN];
bool good = true;
int find(int x) {
if (par[x] == x) {
return x;
}
else {
return find(par[x]);
}
}
void merge(int x, int y) {
int px = find(x);
int py = find(y);
if (px != py) {
if (rk[px] < rk[py]) {
swap(px, py);
}
rk[px] += rk[py];
par[py] = px;
last.push_back({px, py});
}
last.push_back({-1, -1});
}
void add(int l, int r, int i, int n = 1, int tl = 0, int tr = N-1) {
if (l > tr || r < tl) {
return;
}
else if (l <= tl && tr <= r) {
seg[n].push_back(i);
}
else {
int tm = (tl + tr)/2;
add(l, r, i, 2*n, tl, tm);
add(l, r, i, 2*n+1, tm+1, tr);
}
}
void undo() {
auto lst = last.back();
last.pop_back();
if (lst.first != -1) {
rk[lst.first] -= rk[lst.second];
par[lst.second] = lst.second;
}
}
void go(int n = 1, int tl = 0, int tr = N-1) {
for (int e : seg[n]) {
merge(edges[e].first, edges[e].second);
}
if (tl == tr) {
timer++;
for (int ai : A[tl]) {
cur[find(ai)] = timer;
}
for (int bi : B[tl]) {
if (cur[find(bi)] != timer) {
good = false;
}
}
}
else {
int tm = (tl + tr)/2;
go(2*n, tl, tm);
go(2*n+1, tm+1, tr);
}
FR(iter, seg[n].size()) {
undo();
}
}
void reset() {
good = true;
FR(i, N) {
par[i] = i;
rk[i] = 1;
A[i].clear();
B[i].clear();
}
FR(i, 4*N) {
seg[i].clear();
}
}
int solve() {
reset();
cin >> N >> M;
FR(i, N) {
cin >> a[i];
a[i]--;
A[a[i]].push_back(i);
}
FR(i, N) {
cin >> b[i];
b[i]--;
B[b[i]].push_back(i);
}
for (int i = 0; i < M; i++) {
cin >> edges[i].first >> edges[i].second;
edges[i].first--;
edges[i].second--;
int a1 = a[edges[i].first];
int b1 = b[edges[i].first];
int a2 = a[edges[i].second];
int b2 = b[edges[i].second];
add(max(a1, a2), min(b1, b2), i);
}
go();
return good;
}
int main() {
cin.tie(0);
cin.sync_with_stdio(0);
int T;
cin >> T;
while (T-->0) {
cout << solve() << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
21996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
21996 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
69 ms |
21996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
69 ms |
21996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
21996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
120 ms |
21484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
38 ms |
21484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
21996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |