結果

問題 No.1914 Directed by Two Sequences
ユーザー chineristACchineristAC
提出日時 2021-11-03 04:08:18
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 304 ms / 3,000 ms
コード長 6,266 bytes
コンパイル時間 7,320 ms
コンパイル使用メモリ 262,420 KB
実行使用メモリ 19,724 KB
最終ジャッジ日時 2023-08-22 06:23:53
合計ジャッジ時間 30,027 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 46 ms
4,924 KB
testcase_03 AC 48 ms
5,232 KB
testcase_04 AC 48 ms
5,288 KB
testcase_05 AC 48 ms
5,032 KB
testcase_06 AC 46 ms
4,972 KB
testcase_07 AC 42 ms
4,380 KB
testcase_08 AC 45 ms
4,816 KB
testcase_09 AC 44 ms
4,828 KB
testcase_10 AC 207 ms
16,044 KB
testcase_11 AC 202 ms
16,052 KB
testcase_12 AC 203 ms
16,024 KB
testcase_13 AC 192 ms
16,296 KB
testcase_14 AC 194 ms
16,700 KB
testcase_15 AC 191 ms
16,372 KB
testcase_16 AC 216 ms
18,480 KB
testcase_17 AC 216 ms
19,368 KB
testcase_18 AC 216 ms
18,764 KB
testcase_19 AC 123 ms
9,732 KB
testcase_20 AC 127 ms
10,916 KB
testcase_21 AC 132 ms
10,976 KB
testcase_22 AC 213 ms
19,476 KB
testcase_23 AC 213 ms
18,872 KB
testcase_24 AC 211 ms
19,000 KB
testcase_25 AC 239 ms
16,204 KB
testcase_26 AC 230 ms
16,208 KB
testcase_27 AC 238 ms
16,456 KB
testcase_28 AC 238 ms
16,276 KB
testcase_29 AC 237 ms
16,400 KB
testcase_30 AC 235 ms
16,076 KB
testcase_31 AC 264 ms
18,280 KB
testcase_32 AC 238 ms
16,192 KB
testcase_33 AC 79 ms
5,664 KB
testcase_34 AC 304 ms
19,724 KB
testcase_35 AC 115 ms
6,872 KB
testcase_36 AC 191 ms
14,888 KB
testcase_37 AC 198 ms
14,884 KB
testcase_38 AC 265 ms
19,192 KB
testcase_39 AC 276 ms
18,860 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]=i;};
  sort(all(ci),[&clique](int i,int j){
    return clique[i].size() < clique[j].size();
  });
  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