結果

問題 No.1914 Directed by Two Sequences
ユーザー vjudge1vjudge1
提出日時 2024-03-31 08:01:33
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 3,000 ms
コード長 6,177 bytes
コンパイル時間 7,522 ms
コンパイル使用メモリ 282,080 KB
実行使用メモリ 18,564 KB
最終ジャッジ日時 2024-09-30 17:50:43
合計ジャッジ時間 31,016 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 47 ms
5,248 KB
testcase_03 AC 51 ms
6,816 KB
testcase_04 AC 49 ms
6,820 KB
testcase_05 AC 49 ms
6,816 KB
testcase_06 AC 47 ms
6,820 KB
testcase_07 AC 45 ms
6,820 KB
testcase_08 AC 48 ms
6,820 KB
testcase_09 AC 47 ms
6,816 KB
testcase_10 AC 195 ms
16,036 KB
testcase_11 AC 209 ms
16,148 KB
testcase_12 AC 211 ms
16,148 KB
testcase_13 AC 194 ms
16,508 KB
testcase_14 AC 195 ms
16,452 KB
testcase_15 AC 195 ms
16,344 KB
testcase_16 AC 211 ms
18,248 KB
testcase_17 AC 207 ms
18,216 KB
testcase_18 AC 212 ms
18,344 KB
testcase_19 AC 124 ms
9,808 KB
testcase_20 AC 130 ms
10,956 KB
testcase_21 AC 133 ms
11,144 KB
testcase_22 AC 212 ms
18,144 KB
testcase_23 AC 210 ms
18,212 KB
testcase_24 AC 215 ms
18,152 KB
testcase_25 AC 231 ms
16,124 KB
testcase_26 AC 225 ms
16,260 KB
testcase_27 AC 242 ms
16,484 KB
testcase_28 AC 235 ms
16,408 KB
testcase_29 AC 236 ms
16,124 KB
testcase_30 AC 236 ms
16,268 KB
testcase_31 AC 232 ms
18,048 KB
testcase_32 AC 221 ms
16,084 KB
testcase_33 AC 79 ms
6,120 KB
testcase_34 AC 271 ms
18,296 KB
testcase_35 AC 114 ms
6,860 KB
testcase_36 AC 186 ms
15,060 KB
testcase_37 AC 190 ms
15,092 KB
testcase_38 AC 258 ms
18,548 KB
testcase_39 AC 249 ms
18,564 KB
権限があれば一括ダウンロードができます

ソースコード

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 <unordered_map>
#include <atcoder/all>
using namespace std;
using namespace atcoder;

//using mint = modint998244353;
#define rep(i,n) for (int i=0;i<n;i+=1)
#define rrep(i,n) for (int i=n-1;i>-1;i--)
#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<<endl;
}

ll lpow(int n,int k){
  ll res = 1;
  ll e = n;
  while (k){
    if (k&1){
      res *= e;
    }
    e = e * e;
    k >>= 1;
  }
  return res;
}

int op1(int i,int j){
  return min(i,j);
}

int e1(){
  return 1e5;
}

int op2(int i,int j){
  return max(i,j);
}

int e2(){
  return -1;
}

int A[100000];
int B[100000];

bool direct(int i,int j){
  if (i < j){
    return (A[i] < B[j]);
  }
  else{
    return (B[i] < A[j]);
  }
}

vec<int> hamilton_path(vec<int> V){
  int n = V.size();
  if (n <= 1){
    return V;
  }

  vec<int> VA = hamilton_path({V.begin(),V.begin()+(n/2)});
  vec<int> VB = hamilton_path({V.begin()+(n/2),V.begin()+(n)});

  vec<int> res = {};
  int i = 0;
  for (auto a:VA){
    while (i < VB.size() && direct(VB[i],a)){
      res.append(VB[i]);
      i++;
    }
    res.append(a);
  }

  for (int j=i;j<VB.size();j++){
    res.append(VB[j]);
  }

  return res;
}

vec<pii> solve(){
  int N,M;
  cin>>N>>M;
  for (int i=0;i<N;i++){
    cin>>A[i];
    A[i]--;
  }
  for (int i=0;i<N;i++){
    cin>>B[i];
    B[i]--;
  }
  vec<vec<int>> edge(N,vec<int>(0));
  int u,v;
  for (int i=0;i<M;i++){
    cin>>u>>v;
    edge[u-1].append(v-1);
    edge[v-1].append(u-1);
  }

  vec<pii> new_E = {};

  vec<int> group(N,0);
  for (int i=0;i<N;i++){
    int n = edge[i].size();
    vec<bool> mex(n+1,false);
    for (auto nv:edge[i]){
      if (nv < i && group[nv]<=n){
        mex[group[nv]] = true;
      }
    }
    for (int j=0;j<n+1;j++){
      if (!mex[j]){
        group[i] = j;
        break;
      }
    }
  }

  int n = *max_element(all(group)) + 1;
  vec<vec<int>> clique(n,vec<int>(0));
  rep(v,N){
    clique[group[v]].append(v);
  }

  vec<int> idx(N,-1);
  for (int i=0;i<n;i++){
    clique[i] = hamilton_path(clique[i]);
    for (int j=0;j<clique[i].size();j++){
      idx[clique[i][j]] = j;
      if (j!=clique[i].size()-1){
        new_E.append({clique[i][j],clique[i][j+1]});
      }
    }
  }

  vec<int> memo_to(N,1e5);
  vec<int> memo_from(N,-1);
  vec<int> ci(n);
  rep(i,n){ci[i]=n-1-i;};
  vec<int> idx_on_ci(n);
  rep(i,n){idx_on_ci[ci[i]]=i;};
  vec<vec<int>> ban(N,vec<int>(0));
  vec<int> Vs = {};

  segtree<int,op1,e1> seg_to_large(2*N);
  segtree<int,op1,e1> seg_to_small(2*N);
  segtree<int,op2,e2> seg_from_large(2*N);
  segtree<int,op2,e2> seg_from_small(2*N);

  

  rep(i,n){
    int target = ci[i];
    for (auto v:clique[target]){
      Vs.append(v);
    }
    sort(all(Vs));
    for (auto nv:clique[target]){
      for (auto v:edge[nv]){
        if (idx_on_ci[group[v]] <= i){
          ban[v].append(nv);
        }
      }
    }

    rrep(j,Vs.size()){
      v = Vs[j];
      for (auto nv:ban[v]){
        if (v < nv){
          seg_to_large.set(B[nv],1e5);
        }
      }
      chmin(memo_to[v],seg_to_large.prod(A[v],2*N));
      for (auto nv:ban[v]){
        if (v < nv){
          seg_to_large.set(B[nv],idx[nv]);
        }
      }
      if (group[v]==target){
        seg_to_large.set(B[v],idx[v]);
      }
    }

    rep(j,Vs.size()){
      v = Vs[j];
      for (auto nv:ban[v]){
        if (nv < v){
          seg_to_small.set(A[nv],1e5);
        }
      }
      chmin(memo_to[v],seg_to_small.prod(B[v],2*N));
      for (auto nv:ban[v]){
        if (nv < v){
          seg_to_small.set(A[nv],idx[nv]);
        }
      }
      if (group[v]==target){
        seg_to_small.set(A[v],idx[v]);
      }
    }

    rrep(j,Vs.size()){
      v = Vs[j];
      for (auto nv:ban[v]){
        if (v < nv){
          seg_from_large.set(B[nv],-1);
        }
      }
      chmax(memo_from[v],seg_from_large.prod(0,A[v]));
      for (auto nv:ban[v]){
        if (v < nv){
          seg_from_large.set(B[nv],idx[nv]);
        }
      }
      if (group[v]==target){
        seg_from_large.set(B[v],idx[v]);
      }
    }

    rep(j,Vs.size()){
      v = Vs[j];
      for (auto nv:ban[v]){
        if (nv < v){
          seg_from_small.set(A[nv],-1);
        }
      }
      chmax(memo_from[v],seg_from_small.prod(0,B[v]));
      for (auto nv:ban[v]){
        if (nv < v){
          seg_from_small.set(A[nv],idx[nv]);
        }
      }
      if (group[v]==target){
        seg_from_small.set(A[v],idx[v]);
      }
    }

    for (auto v:Vs){
      if (memo_to[v]!=1e5){
        int nv = clique[target][memo_to[v]];
        new_E.append({v,nv});
        memo_to[v] = 1e5;
      }

      if (memo_from[v]!=-1){
        int pv = clique[target][memo_from[v]];
        new_E.append({pv,v});
        memo_from[v] = -1;
      }

      ban[v] = {};
      if (group[v]==target){
        seg_to_large.set(B[v],1e5);
        seg_to_small.set(A[v],1e5);
        seg_from_large.set(B[v],-1);
        seg_from_small.set(A[v],-1) ;
      }
    }
  }

  sort(all(new_E));
  new_E.erase(unique(all(new_E)),new_E.end());
  
  return new_E;
}

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  auto res = solve();
  cout<<res.size()<<endl;
  for (auto [u,v]:res){
    cout<<u+1<<" "<<v+1<<"\n";
  }
}
0