# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
899811 |
2024-01-07T04:14:35 Z |
Cookie |
Colors (RMI18_colors) |
C++14 |
|
542 ms |
102612 KB |
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359;
using u128 = __uint128_t;
const int x[4] = {1, 0, -1, 0};
const int y[4] = {0, -1, 0, 1};
const ll mod = 1e9 + 7;
const int mxn = 3e5 + 69, mxq = 1e5 + 5, sq = 500, mxv = 2e7 + 5;
//const int base = (1 <<18);
const int inf = 1e9 + 5, neg = -69420;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
//const int x[9] = {0, 1, 1, -1, -1, 2, -2, 2, -2};
//const inty[9] = {0, 2, -2, 2, -2, 1, 1, -1, -1};
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
int n, m;
vt<int>adj[mxn + 1];
bool vis[mxn + 1];
vt<int>compa[mxn + 1], compb[mxn + 1];
bool bad = 0;
int seen[mxn + 1], a[mxn + 1], b[mxn + 1], goodnode[mxn + 1], goodcomp[mxn + 1];
struct DSU{
vt<pii>past_p, past_sz;
vt<int>checkpoint;
int p[mxn + 1], sz[mxn + 1];
void init(){
for(int i = 1; i <= n; i++){
p[i] = i; sz[i] = 1;
}
past_sz.clear(); past_p.clear(); checkpoint.clear();
}
int fp(int a){
if(p[a] == a)return(a);
return(fp(p[a]));
}
bool check(int u, int v){
return(fp(u) == fp(v));
}
void unon(int a, int b){
assert(a && b);
a = fp(a); b = fp(b);
if(sz[a] < sz[b])swap(a, b);
past_sz.pb({a, sz[a]}); past_p.pb({b, p[b]});
if(a != b){
sz[a] += sz[b]; p[b] = a;
}
}
void rollback(){
//int before = p[past_p.back().fi], after = past_p.back().se;
//if(before != past_p.back().fi && after == past_p.back().fi)cnt++;
p[past_p.back().fi] = past_p.back().se; past_p.pop_back();
sz[past_sz.back().fi] = past_sz.back().se; past_sz.pop_back();
}
void save(){
checkpoint.pb(past_sz.size());
}
void to_last(){
while(past_sz.size() != checkpoint.back())rollback();
checkpoint.pop_back();
}
};
DSU dsu;
vt<int>st[4 * mxn + 1];
void upd(int nd, int l, int r, int ql, int qr, int u){
if(ql > r || qr < l)return;
if(ql <= l && qr >= r){
st[nd].pb(u);
return;
}
int mid = (l + r) >> 1;
upd(nd << 1, l, mid, ql, qr, u); upd(nd << 1 | 1, mid + 1, r, ql, qr, u);
}
void dfs(int s, int l, int r){
if(l > r)return;
dsu.save();
for(auto u: st[s]){
goodnode[u] = 1;
for(auto j: adj[u]){
if(goodnode[j]){
dsu.unon(u, j);
}
}
}
if(l == r){
for(auto i: compa[l]){
goodcomp[dsu.fp(i)] = 1;
}
for(auto i: compb[l]){
if(!goodcomp[dsu.fp(i)])bad = 1;
}
for(auto i: compa[l]){
goodcomp[dsu.fp(i)] = 0;
}
}else{
int mid = (l + r) >> 1;
dfs(s << 1, l, mid); dfs(s << 1 | 1, mid + 1, r);
}
for(auto u: st[s])goodnode[u] = 0;
dsu.to_last();
}
void solve(){
//for(int i = 1; i <= n; i++)pa[i] = -1;
cin >> n >> m;
dsu.init();
for(int i = 1; i <= n; i++){
seen[i] = 0; compa[i].clear(); compb[i].clear(); adj[i].clear(); goodnode[i] = goodcomp[i] = 0;
}
for(int i = 1; i <= 4 * n; i++)st[i].clear();
for(int i = 1; i <= n; i++){
cin >> a[i]; seen[a[i]] = 1; compa[a[i]].pb(i);
}
for(int i = 1; i <= n; i++){
cin >> b[i]; compb[b[i]].pb(i);
}
for(int i = 0; i < m; i++){
int u, v; cin >> u >> v;
adj[u].pb(v); adj[v].pb(u);
}
for(int i = 1; i <= n; i++){
if(b[i] > a[i] || !seen[b[i]]){
cout << 0 << "\n";
return;
}
}
for(int i = 1; i <= n; i++){
upd(1, 1, n, b[i], a[i], i);
}
bad = 0;
dfs(1, 1, n);
//for(int i = 1; i <= n; i++)cout << goodnode[i] << " ";
cout << ((bad) ? 0 : 1) << "\n";
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("COLLEGE.INP", "r", stdin);
//freopen("COLLEGE.OUT", "w", stdout);
int tt; cin >> tt;
while(tt--){
solve();
}
return(0);
}
/*
4 4
3 3 2 1
1 2 2 1
1 2
2 3
3 4
4 2
*/
Compilation message
colors.cpp: In member function 'void DSU::to_last()':
colors.cpp:78:30: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} [-Wsign-compare]
78 | while(past_sz.size() != checkpoint.back())rollback();
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
58324 KB |
Output is correct |
2 |
Correct |
28 ms |
58204 KB |
Output is correct |
3 |
Correct |
14 ms |
58460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
58204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
58320 KB |
Output is correct |
2 |
Correct |
31 ms |
58200 KB |
Output is correct |
3 |
Correct |
16 ms |
58456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
58320 KB |
Output is correct |
2 |
Correct |
31 ms |
58200 KB |
Output is correct |
3 |
Correct |
16 ms |
58456 KB |
Output is correct |
4 |
Correct |
122 ms |
58204 KB |
Output is correct |
5 |
Correct |
365 ms |
74048 KB |
Output is correct |
6 |
Correct |
542 ms |
94880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
58324 KB |
Output is correct |
2 |
Correct |
28 ms |
58204 KB |
Output is correct |
3 |
Correct |
14 ms |
58460 KB |
Output is correct |
4 |
Correct |
70 ms |
58320 KB |
Output is correct |
5 |
Correct |
31 ms |
58200 KB |
Output is correct |
6 |
Correct |
16 ms |
58456 KB |
Output is correct |
7 |
Correct |
68 ms |
58200 KB |
Output is correct |
8 |
Correct |
31 ms |
58200 KB |
Output is correct |
9 |
Correct |
15 ms |
58460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
58360 KB |
Output is correct |
2 |
Correct |
337 ms |
75780 KB |
Output is correct |
3 |
Correct |
340 ms |
93424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
58204 KB |
Output is correct |
2 |
Correct |
18 ms |
58460 KB |
Output is correct |
3 |
Correct |
16 ms |
58460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
58324 KB |
Output is correct |
2 |
Correct |
28 ms |
58204 KB |
Output is correct |
3 |
Correct |
14 ms |
58460 KB |
Output is correct |
4 |
Correct |
50 ms |
58204 KB |
Output is correct |
5 |
Correct |
70 ms |
58320 KB |
Output is correct |
6 |
Correct |
31 ms |
58200 KB |
Output is correct |
7 |
Correct |
16 ms |
58456 KB |
Output is correct |
8 |
Correct |
122 ms |
58204 KB |
Output is correct |
9 |
Correct |
365 ms |
74048 KB |
Output is correct |
10 |
Correct |
542 ms |
94880 KB |
Output is correct |
11 |
Correct |
68 ms |
58200 KB |
Output is correct |
12 |
Correct |
31 ms |
58200 KB |
Output is correct |
13 |
Correct |
15 ms |
58460 KB |
Output is correct |
14 |
Correct |
116 ms |
58360 KB |
Output is correct |
15 |
Correct |
337 ms |
75780 KB |
Output is correct |
16 |
Correct |
340 ms |
93424 KB |
Output is correct |
17 |
Correct |
30 ms |
58204 KB |
Output is correct |
18 |
Correct |
18 ms |
58460 KB |
Output is correct |
19 |
Correct |
16 ms |
58460 KB |
Output is correct |
20 |
Correct |
67 ms |
60700 KB |
Output is correct |
21 |
Correct |
356 ms |
78780 KB |
Output is correct |
22 |
Correct |
481 ms |
102612 KB |
Output is correct |