제출 #441674

#제출 시각아이디문제언어결과실행 시간메모리
441674JovanBColors (RMI18_colors)C++17
7 / 100
183 ms14360 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const int MAXN = 200000; int A[MAXN+5]; int B[MAXN+5]; struct DSU{ int n; int par[MAXN+5]; int rnk[MAXN+5]; struct operacija{ int a, b, rnka, rnkb, c; }; stack <operacija> st; DSU(int g){ n = g; for(int i=1; i<=n; i++) par[i] = i; } int root(int x){ return (x == par[x] ? x : root(par[x])); } void povezi(int a, int b, int c){ int a1 = root(a); int b1 = root(b); if(a1 == b1) return; if(rnk[a1] < rnk[b1]) swap(a1, b1); st.push({a1, b1, rnk[a1], rnk[b1], c}); if(rnk[a1] == rnk[b1]) rnk[a1]++; par[b1] = a1; } void rollback(){ if(st.empty()) return; int a = st.top().a; int b = st.top().b; rnk[a] = st.top().rnka; rnk[b] = st.top().rnkb; par[a] = a; par[b] = b; st.pop(); } void brisi(int c){ while(!st.empty() && st.top().c == c) rollback(); } }; vector <int> koji[MAXN+5]; vector <int> treba[MAXN+5]; struct Edge{ int a, b, c; bool operator <(const Edge &x){ return c < x.c; } }; vector <Edge> edges; void klear(int n){ for(int i=1; i<=n; i++) koji[A[i]].clear(); for(int i=1; i<=n; i++) treba[B[i]].clear(); edges.clear(); } map <int, int> ima; void solve(){ int n, m; cin >> n >> m; for(int i=1; i<=n; i++) cin >> A[i]; for(int i=1; i<=n; i++) cin >> B[i]; for(int i=1; i<=n; i++) koji[A[i]].push_back(i); for(int i=1; i<=n; i++) treba[B[i]].push_back(i); for(int i=1; i<=m; i++){ int a, b; cin >> a >> b; edges.push_back({a, b, max(B[a], B[b])}); } for(int i=1; i<=n; i++){ if(A[i] < B[i]){ cout << "0\n"; klear(n); return; } } sort(edges.begin(), edges.end()); DSU dsu(n); for(auto edge : edges) dsu.povezi(edge.a, edge.b, edge.c); for(int i=n; i>=1; i--){ ima.clear(); for(auto c : koji[i]) ima[dsu.root(c)] = 1; for(auto c : treba[i]){ if(!ima[dsu.root(c)]){ cout << "0\n"; klear(n); return; } } dsu.brisi(i); } cout << "1\n"; klear(n); return; } int main(){ ios_base::sync_with_stdio(false), cin.tie(0); cout.precision(10); cout << fixed; int t; cin >> t; while(t--) solve(); return 0; }
#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...