#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <unordered_set>
#include <unordered_map>
#include <stdio.h>
#include <bitset>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ld long double
const ll maxn = 2e5 + 1, maxm = 3e5 + 1;
const ll mod = 1e9 + 7, cmod = 998244353, inf = 1e9, blcok = 400, p2 = 31;
const ld eps = 1e-9;
int t;
int n, m;
int a[maxn], b[maxn], is[maxn], blck[maxn];
int u[maxn], v[maxn];
struct dsu{
int p[maxn], sz[maxn], si[maxn];
stack<pair<int, int>> st;
void init(){
for (int i = 1; i <= n; ++i){
p[i] = i, sz[i] = 1;
si[i] = 0;
}
while (st.size() > 0){
st.pop();
}
}
int get(int v){
if (v == p[v])
return v;
return get(p[v]);
}
void unite(int a, int b, int type){
a = get(a), b = get(b);
if (a == b)
return;
if (sz[a] > sz[b])
swap(a, b);
p[a] = b;
sz[b] += sz[a];
if (type){
st.push(mp(a, sz[a]));
}
}
void rollback(){
pair<int, int> cur = st.top();
int a = cur.f, siz = cur.s;
sz[p[a]] -= siz;
p[a] = a;
}
} rt;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--){
cin >> n >> m;
for (int i = 1; i <= n; ++i){
cin >> a[i];
}
vector<pair<int, int>> vec;
for (int i = 1; i <= n; ++i){
cin >> b[i];
vec.pb(mp(b[i], i));
}
vector<pair<int, int>> reb;
for (int i = 1; i <= m; ++i){
cin >> u[i] >> v[i];
reb.pb(mp(min(a[u[i]], a[v[i]]), i));
}
rt.init();
sort(reb.begin(), reb.end());
reverse(reb.begin(), reb.end());
sort(vec.begin(), vec.end());
reverse(vec.begin(), vec.end());
int j = 0, bad = 0;
for (int i = 0; i < n; ){
vector<int> vert;
vert.pb(vec[i].s);
is[vec[i].s] = 1;
while (i + 1 < n && vec[i + 1].f == vec[i].f){
vert.pb(vec[i + 1].s);
is[vec[i + 1].s] = 1;
i += 1;
}
vector<pair<int, pair<int, int>>> add;
int val = vec[i].f;
vector<int> z;
while (j < m && reb[j].f >= val){
int x = u[reb[j].s], y = v[reb[j].s];
if (blck[x] || blck[y]){
++j;
continue;
}
z.pb(x);
z.pb(y);
if (!(is[x] || is[y])){
rt.unite(x, y, 0);
if (reb[j].f == val){
int root = rt.get(x);
rt.si[root] |= 1;
}
}
else{
add.pb(mp(reb[j].f, mp(x, y)));
}
++j;
}
for (auto it : add){
rt.unite(it.s.f, it.s.s, 1);
if (it.f == val){
int root = rt.get(it.s.f);
rt.si[root] |= 1;
}
}
for (auto it : vert){
int root = rt.get(it);
if (rt.si[root] == 0 && a[it] != b[it]){
bad = 1;
break;
}
blck[it] = 1;
}
while (rt.st.size() > 0){
rt.rollback();
rt.st.pop();
}
for (auto it : z){
int root = rt.get(it);
rt.si[root] = 0;
}
i += 1;
}
if (bad){
cout << 0 << '\n';
}
else{
cout << 1 << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
46 ms |
372 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
69 ms |
436 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
49 ms |
504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
49 ms |
504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
46 ms |
372 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
103 ms |
380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
46 ms |
372 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |