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 <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 = 1e6 + 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, z, bad;
int a[maxn], b[maxn], is[maxn];
int u[maxn], v[maxn];
vector<pair<int, int>> tree[maxn * 4];
vector<int> nur[maxn], sik[maxn];
struct dsu{
int p[maxn], sz[maxn];
stack<pair<int, int>> st;
void init(){
for (int i = 1; i <= n; ++i){
p[i] = i, sz[i] = 1;
}
}
int get(int v){
if (v == p[v])
return v;
return get(p[v]);
}
void unite(int a, int b){
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];
st.push(mp(a, sz[a]));
}
void rollback(){
pair<int, int> cur = st.top();
int x = cur.f, y = cur.s;
sz[p[x]] -= y;
p[x] = x;
st.pop();
}
} rt;
void upd(int l, int r, int k, int vr = 1, int tl = 1, int tr = n){
if (l <= tl && tr <= r){
tree[vr].pb(mp(u[k], v[k]));
return;
}
if (l > tr || r < tl)
return;
int tm = (tl + tr) / 2;
upd(l, r, k, vr + vr, tl, tm);
upd(l, r, k, vr + vr + 1, tm + 1, tr);
}
void solve(int v = 1, int tl = 1, int tr = n){
int stz = rt.st.size();
for (auto it : tree[v]){
int x = it.f, y = it.s;
rt.unite(x, y);
}
if (tl == tr){
++z;
for (auto it : nur[tl]){
int root = rt.get(it);
is[root] = z;
}
for (auto it : sik[tl]){
int root = rt.get(it);
if (is[root] != z){
bad = 1;
}
}
}
else{
int tm = (tl + tr) / 2;
solve(v + v, tl, tm);
solve(v + v + 1, tm + 1, tr);
}
while (rt.st.size() > stz){
rt.rollback();
}
tree[v].clear();
nur[tl].clear();
sik[tl].clear();
}
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];
nur[a[i]].pb(i);
}
for (int i = 1; i <= n; ++i){
cin >> b[i];
sik[b[i]].pb(i);
}
bad = 0;
for (int i = 1; i <= m; ++i){
cin >> u[i] >> v[i];
int l = max(b[u[i]], b[v[i]]);
int r = min(a[u[i]], a[v[i]]);
upd(l, r, i);
}
rt.init();
solve();
if (bad){
cout << 0 << '\n';
}
else{
cout << 1 << '\n';
}
}
}
/*
*/
Compilation message (stderr)
colors.cpp: In function 'void solve(int, int, int)':
colors.cpp:113:25: warning: comparison of integer expressions of different signedness: 'std::stack<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
113 | while (rt.st.size() > stz){
| ~~~~~~~~~~~~~^~~~~
# | 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... |