Submission #557853

#TimeUsernameProblemLanguageResultExecution timeMemory
557853FatihSolakColors (RMI18_colors)C++17
22 / 100
192 ms19332 KiB
#include <bits/stdc++.h> #define N 200005 using namespace std; int a[N]; int b[N]; vector<int> adj[N]; set<int> s[N]; vector<int> v[N]; int par[N]; int find(int a){ if(a == par[a])return a; return par[a] = find(par[a]); } void merge(int a,int b){ a = find(a); b = find(b); if(a == b)return; if(s[a].size() > s[b].size()) swap(s[a],s[b]); for(auto u:s[a]) s[b].insert(u); s[a].clear(); par[a] = b; } void solve(){ int n,m; cin >> n >> m; for(int i = 1;i<=n;i++){ cin >> a[i]; adj[i].clear(); par[i] = i; s[i] = {a[i]}; v[i].clear(); } for(int i = 1;i<=n;i++){ cin >> b[i]; v[b[i]].push_back(i); } for(int i = 1;i<=m;i++){ int u,v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); if(a[v] >= b[u] && a[u] >= a[v]) s[u].insert(a[v]); if(a[u] >= b[v] && a[v] >= a[u]) s[v].insert(a[u]); } for(int i = 1;i<=n;i++){ if(a[i] < b[i]){ cout << 0 << endl; return; } } for(int i = 1;i<=n;i++){ if(v[i].empty())continue; for(auto u:v[i]){ for(auto c:adj[u]){ if(b[c] <= b[u] && a[c]>=i){ merge(u,c); } } } for(auto u:v[i]){ //cout << u << " " << find(u) << endl; if(s[find(u)].count(i) == 0){ cout << 0 << endl; return; } } } cout << 1 << endl; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef Local freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif int t = 1; cin >> t; while(t--){ solve(); } #ifdef Local cout << endl << fixed << setprecision(2) << 1000.0*clock()/CLOCKS_PER_SEC << " milliseconds."; #endif }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...