제출 #1134606

#제출 시각아이디문제언어결과실행 시간메모리
1134606emptypringlescanColors (RMI18_colors)C++17
100 / 100
514 ms33156 KiB
#include <bits/stdc++.h> using namespace std; #define int long long vector<int> adj[150005]; vector<int> arr[150005],brr[150005]; int del[150005],par[150005],sz[150005]; vector<int> rb; int find(int x){ if(par[x]==x) return x; return find(par[x]); } void merge(int x, int y){ x=find(x); y=find(y); assert(x!=y); if(sz[x]>sz[y]) swap(x,y); rb.push_back(x); par[x]=y; sz[y]+=sz[x]; } void rollback(){ int x=rb.back(); rb.pop_back(); int y=par[x]; assert(x!=y); par[x]=x; sz[y]-=sz[x]; } bool can=true; int is[150005]; void dnc(int l, int r){ if(l==r){ int num=0; for(int i:arr[l]){ del[i]--; if(del[i]) continue; for(int j:adj[i]){ if(del[j]==0&&find(i)!=find(j)){ num++; merge(i,j); } } } for(int i:brr[l]){ del[i]--; if(del[i]) continue; for(int j:adj[i]){ if(del[j]==0&&find(i)!=find(j)){ num++; merge(i,j); } } } for(int i:arr[l]) is[find(i)]=1; for(int i:brr[l]) if(!is[find(i)]) can=false; for(int i:arr[l]) is[find(i)]=0; for(int i:arr[l]) del[i]++; for(int i:brr[l]) del[i]++; for(int i=0; i<num; i++) rollback(); return; } int m=(l+r)/2; int num=0; for(int i=m+1; i<=r; i++){ for(int j:arr[i]){ del[j]--; if(del[j]==0){ for(int k:adj[j]){ if(del[k]==0&&find(k)!=find(j)){ num++; merge(j,k); } } } } } dnc(l,m); for(int i=m+1; i<=r; i++){ for(int j:arr[i]){ del[j]++; } } for(int i=0; i<num; i++) rollback(); num=0; for(int i=l; i<=m; i++){ for(int j:brr[i]){ del[j]--; if(del[j]==0){ for(int k:adj[j]){ if(del[k]==0&&find(k)!=find(j)){ num++; merge(j,k); } } } } } dnc(m+1,r); for(int i=l; i<=m; i++){ for(int j:brr[i]){ del[j]++; } } for(int i=0; i<num; i++) rollback(); num=0; return; } int32_t main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while(t--){ int n,m; cin >> n >> m; can=true; for(int i=0; i<=n; i++) par[i]=i,sz[i]=1,del[i]=2; for(int i=1; i<=n; i++){ int x; cin >> x; arr[x].push_back(i); } for(int i=1; i<=n; i++){ int x; cin >> x; brr[x].push_back(i); } for(int i=0; i<m; i++){ int a,b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } dnc(1,n); if(can) cout << 1 << '\n'; else cout << 0 << '\n'; for(int i=0; i<=n; i++){ arr[i].clear(); brr[i].clear(); adj[i].clear(); par[i]=i; sz[i]=1; del[i]=2; } rb.clear(); } }
#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...