結果

問題 No.1914 Directed by Two Sequences
ユーザー chineristACchineristAC
提出日時 2022-02-12 23:00:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,679 bytes
コンパイル時間 8,543 ms
コンパイル使用メモリ 237,124 KB
実行使用メモリ 28,236 KB
最終ジャッジ日時 2023-08-22 06:34:41
合計ジャッジ時間 26,215 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,384 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 WA -
testcase_07 WA -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 WA -
testcase_34 RE -
testcase_35 TLE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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]){
        cout<<E[i][0]<<" "<<E[j][0]<<endl;
      }
      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;

    

    

    

    


}
0