結果
問題 | No.1914 Directed by Two Sequences |
ユーザー |
|
提出日時 | 2022-02-12 23:07:11 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 3,601 bytes |
コンパイル時間 | 24,845 ms |
コンパイル使用メモリ | 279,856 KB |
最終ジャッジ日時 | 2025-01-27 22:53:02 |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 10 RE * 28 |
ソースコード
#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <iostream> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <algorithm> #include <cmath> #include <iomanip> #include <random> #include <stdio.h> #include <fstream> #include <functional> #include <cassert> #include <bitset> #include <atcoder/all> using namespace std; using namespace atcoder; #define rep(i,n) for (int i=0;i<n;i+=1) #define rep2(i,n) for (int i=n;-1<i;i-=1) #define append push_back #define all(x) (x).begin(), (x).end() template<class T> using vec = vector<T>; template<class T> using vvec = vec<vec<T>>; template<class T> using vvvec = vec<vvec<T>>; using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; template<class T> bool chmin(T &a, T b){ if (a>b){ a = b; return true; } return false; } template<class T> bool chmax(T &a, T b){ if (a<b){ a = b; return true; } return false; } template<class T> T sum(vec<T> x){ T res=0; for (auto e:x){ res += e; } return res; } template<class T> void printv(vec<T> x){ for (auto e:x){ cout<<e<<" "; } cout<<"\n"; } vec<vec<bool>> boolean_matrix_mul(int n, vec<bitset<5000>> A, vec<bitset<5000>> B){ vec<vec<bool>> res(n,vec<bool>(n,false)); rep(i,n){ rep(j,n){ auto tmp = A[i]&B[j]; res[i][j] = tmp.any(); } } return res; } vec<bitset<5000>> transtive_closure(int n,vec<bitset<5000>> edge){ //input graph should be acyclic //1,2,...,n should be topological order //res[i][j] is true iff j->i assert (n==edge.size()); vec<bitset<5000>> res(n); rep(i,n){ res[i].set(i,true); rep(j,n){ if (edge[i][j]){ res[j] |= res[i]; } } res[i].set(i,false); } return res; } vec<pii> transtive_reduction(int N,vec<bitset<5000>> edge){ // scc -> m3 = edge * transtive_closure -> (edge - m3) -> cyclic expansion scc_graph G(N); rep(i,N){ rep(j,N){ if (edge[i][j]){ G.add_edge(i,j); } } } auto E = G.scc(); int n = E.size(); vec<int> v_to_e(N,-1); rep(i,n){ for (auto v:E[i]){ v_to_e[v] = i; } } vec<bitset<5000>> scc_edge(n); rep(i,N){ rep(j,N){ if (edge[i][j] && v_to_e[i]!=v_to_e[j]){ scc_edge[v_to_e[i]].set(v_to_e[j],true); } } } auto tra = transtive_closure(n,scc_edge); auto m3 = boolean_matrix_mul(n,scc_edge,tra); vec<pii> res; rep(i,n){ rep(j,n){ if (scc_edge[i][j] && !m3[i][j]){ res.append({E[i][0],E[j][0]}); } } } rep(i,n){ if (E[i].size()!=1){ rep(j,E[i].size()-1){ res.append({E[i][j],E[i][j+1]}); } int p = E[i].size(); res.append({E[i][p-1],E[i][0]}); } } return res; } int main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); int N,M; cin>>N>>M; assert (N <= 5000); vec<int> A(N),B(N); rep(i,N){ cin>>A[i]; } rep(i,N){ cin>>B[i]; } vec<bitset<5000>> edge(N); rep(i,N){ rep(j,N){ if (i<j){ if (A[i] < B[j]){ edge[i].set(j,true); } else{ edge[j].set(i,true); } } } } rep(i,M){ int u,v; cin>>u>>v; edge[u-1].set(v-1,false); edge[v-1].set(u-1,false); } auto res = transtive_reduction(N,edge); cout<<res.size()<<endl; for (auto [u,v]:res){ cout<<u+1<<" "<<v+1<<endl; } //cout<<"finish"<<endl; }