# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
701510 |
2023-02-21T11:38:08 Z |
Nursik |
Colors (RMI18_colors) |
C++14 |
|
128 ms |
3456 KB |
#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;
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<pair<int, 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];
si[b] |= si[a];
st.push(mp(mp(a, b), sz[a]));
}
void rollback(){
pair<pair<int, int>, int> cur = st.top();
int a = cur.f.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];
blck[i] = 0;
is[i] = 0;
}
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; ){
int val = vec[i].f;
/* cout << i << '\n';
for (int k = 1; k <= n; ++k){
cout << rt.get(k) << " ";
}
cout << '\n';
for (int k = 1; k <= n; ++k){
cout << rt.si[rt.get(k)] << " ";
}
cout << '\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;
vector<int> z;
while (j < m && reb[j].f >= val){
int x = u[reb[j].s], y = v[reb[j].s];
z.pb(x);
z.pb(y);
if (a[x] == val){
int root = rt.get(x);
rt.si[root] = 1;
}
if (a[y] == val){
int root = rt.get(x);
rt.si[root] = 1;
}
if (blck[x] || blck[y]){
++j;
continue;
}
if (!(is[x] || is[y])){
rt.unite(x, y, 0);
// cout << x << " " << y << " " << 0 << '\n';
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);
// cout << it.s.f << " " << it.s.s << " " << 1 << '\n';
if (it.f == val){
int root = rt.get(it.s.f);
rt.si[root] |= 1;
}
}
for (auto it : vert){
int root = rt.get(it);
blck[it] = 1;
if (a[it] == b[it]){
continue;
}
if (rt.si[root] == 0){
bad = 1;
break;
}
}
while (rt.st.size() > 0){
pair<pair<int, int>, int> cur = rt.st.top();
if (is[cur.f.f] || is[cur.f.s]){
rt.rollback();
rt.st.pop();
}
else{
break;
}
}
for (auto it : z){
int root = rt.get(it);
rt.si[root] = 0;
is[it] = 0;
}
/* cout << i << '\n';
for (int k = 1; k <= n; ++k){
cout << rt.get(k) << " ";
}
cout << '\n';
for (int k = 1; k <= n; ++k){
cout << rt.si[rt.get(k)] << " ";
}
cout << '\n';
if (i == 2){
exit(0);
}*/
i += 1;
}
if (bad){
cout << 0 << '\n';
}
else{
cout << 1 << '\n';
}
}
}
/*
4 3
2 1 4 1
1 1 4 1
2 3
3 4
4 1
10 9
2 9 10 7 2 2 10 8 7 10
2 8 3 7 1 1 6 2 2 7
6 4
4 1
1 8
8 9
9 3
3 10
10 2
2 5
5 7
10 9
6 5 7 9 2 10 5 6 1 6
1 3 4 1 1 3 3 2 1 5
5 2
2 8
8 3
3 9
9 7
7 6
6 10
10 1
1 4
9 8
7 5 3 9 8 1 8 5 7
4 1 2 9 7 1 4 5 6
6 1
1 2
2 4
4 8
8 9
9 7
7 3
3 5
9 8
7 5 5 4 6 7 7 9 8
3 4 1 2 5 3 2 5 5
8 3
3 2
2 6
6 4
4 5
5 9
9 1
1 7
1 0
1
1
5 4
2 5 4 4 2
1 5 3 2 1
2 3
3 4
4 5
5 1
6 5
2 2 3 2 5 1
1 2 3 1 2 1
3 2
2 6
6 4
4 5
5 1
7 6
2 7 5 1 3 1 7
2 7 1 1 3 1 4
2 4
4 7
7 1
1 6
6 5
5 3
8 7
2 7 6 4 3 8 1 6
2 7 1 1 3 2 1 3
1 4
4 6
6 7
7 3
3 2
2 8
8 5
10 9
1 3 7 3 7 1 7 1 6 8
1 3 6 1 3 1 3 1 3 7
1 2
2 6
6 8
8 10
10 9
9 7
7 3
3 4
4 5
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
54 ms |
1744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
2116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
66 ms |
1788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
66 ms |
1788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
54 ms |
1744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
128 ms |
3456 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
1200 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
54 ms |
1744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |