結果

問題 No.1647 Travel in Mitaru city 2
ユーザー LayCurseLayCurse
提出日時 2021-08-13 22:27:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 72 ms / 2,500 ms
コード長 11,270 bytes
コンパイル時間 3,243 ms
コンパイル使用メモリ 231,036 KB
実行使用メモリ 23,660 KB
最終ジャッジ日時 2023-08-03 05:39:57
合計ジャッジ時間 9,103 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
11,584 KB
testcase_01 AC 6 ms
11,764 KB
testcase_02 AC 5 ms
11,592 KB
testcase_03 AC 5 ms
11,584 KB
testcase_04 AC 6 ms
11,668 KB
testcase_05 AC 5 ms
11,816 KB
testcase_06 AC 5 ms
11,584 KB
testcase_07 AC 5 ms
11,788 KB
testcase_08 AC 57 ms
22,616 KB
testcase_09 AC 27 ms
18,880 KB
testcase_10 AC 60 ms
22,716 KB
testcase_11 AC 49 ms
21,556 KB
testcase_12 AC 31 ms
19,484 KB
testcase_13 AC 54 ms
22,576 KB
testcase_14 AC 60 ms
22,980 KB
testcase_15 AC 60 ms
22,716 KB
testcase_16 AC 54 ms
22,344 KB
testcase_17 AC 50 ms
21,816 KB
testcase_18 AC 27 ms
18,756 KB
testcase_19 AC 25 ms
18,760 KB
testcase_20 AC 50 ms
21,604 KB
testcase_21 AC 33 ms
19,940 KB
testcase_22 AC 63 ms
22,984 KB
testcase_23 AC 63 ms
22,984 KB
testcase_24 AC 41 ms
20,812 KB
testcase_25 AC 29 ms
19,496 KB
testcase_26 AC 63 ms
22,984 KB
testcase_27 AC 63 ms
23,312 KB
testcase_28 AC 72 ms
23,456 KB
testcase_29 AC 71 ms
23,516 KB
testcase_30 AC 27 ms
19,276 KB
testcase_31 AC 67 ms
23,660 KB
testcase_32 AC 35 ms
20,068 KB
testcase_33 AC 37 ms
20,500 KB
testcase_34 AC 70 ms
23,504 KB
testcase_35 AC 29 ms
19,416 KB
testcase_36 AC 70 ms
23,548 KB
testcase_37 AC 67 ms
23,504 KB
testcase_38 AC 47 ms
21,192 KB
testcase_39 AC 35 ms
19,812 KB
testcase_40 AC 46 ms
21,032 KB
testcase_41 AC 26 ms
18,704 KB
testcase_42 AC 46 ms
21,280 KB
testcase_43 AC 6 ms
11,620 KB
testcase_44 AC 6 ms
11,524 KB
testcase_45 AC 20 ms
17,876 KB
testcase_46 AC 27 ms
17,972 KB
testcase_47 AC 21 ms
17,936 KB
testcase_48 AC 22 ms
18,156 KB
testcase_49 AC 20 ms
17,880 KB
testcase_50 AC 19 ms
18,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("inline")
#include<bits/stdc++.h>
using namespace std;
void*wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){
  walloc1d(arr, x2-x1, mem);
  (*arr) -= x1;
}
struct Rand{
  unsigned x;
  unsigned y;
  unsigned z;
  unsigned w;
  Rand(void){
    x=123456789;
    y=362436069;
    z=521288629;
    w=(unsigned)time(NULL);
  }
  Rand(unsigned seed){
    x=123456789;
    y=362436069;
    z=521288629;
    w=seed;
  }
  inline unsigned get(void){
    unsigned t;
    t = (x^(x<<11));
    x=y;
    y=z;
    z=w;
    w = (w^(w>>19))^(t^(t>>8));
    return w;
  }
  inline double getUni(void){
    return get()/4294967296.0;
  }
  inline int get(int a){
    return (int)(a*getUni());
  }
  inline int get(int a, int b){
    return a+(int)((b-a+1)*getUni());
  }
  inline long long get(long long a){
    return(long long)(a*getUni());
  }
  inline long long get(long long a, long long b){
    return a+(long long)((b-a+1)*getUni());
  }
  inline double get(double a, double b){
    return a+(b-a)*getUni();
  }
  inline int getExp(int a){
    return(int)(exp(getUni()*log(a+1.0))-1.0);
  }
  inline int getExp(int a, int b){
    return a+(int)(exp(getUni()*log((b-a+1)+1.0))-1.0);
  }
}
;
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template<class S, class T> inline S moddw_L(S a, const T b){
  a %= b;
  if(a < 0){
    a += b;
  }
  return a;
}
template<class T> void arrRot(int k, int N, T A[], T B[] = NULL, void *mem = wmem){
  int i;
  int fg = 0;
  (k = moddw_L(k,N));
  if(B==NULL){
    walloc1d(&B, N, &mem);
    fg = 1;
  }
  for(i=(k);i<(N);i++){
    B[i-k] = A[i];
  }
  for(i=(0);i<(k);i++){
    B[N-k+i] = A[i];
  }
  if(fg){
    for(i=(0);i<(N);i++){
      A[i] = B[i];
    }
  }
}
struct graph{
  int N;
  int*es;
  int**edge;
  void setEdge(int N__, int M, int A[], int B[], void **mem = &wmem){
    int i;
    N = N__;
    walloc1d(&es, N, mem);
    walloc1d(&edge, N, mem);
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      es[A[i]]++;
      es[B[i]]++;
    }
    for(i=(0);i<(N);i++){
      walloc1d(&edge[i], es[i], mem);
    }
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      edge[A[i]][es[A[i]]++] = B[i];
      edge[B[i]][es[B[i]]++] = A[i];
    }
  }
  int anUndirectedCycle(int res[] = NULL, void *mem = wmem){
    int i;
    int j;
    int k, m;
    int*arr;
    int*q;
    int qs;
    int qe;
    int*bk;
    int*vis;
    if(res==NULL){
      walloc1d(&res, N+1, &mem);
    }
    for(i=(0);i<(N);i++){
      for(j=(0);j<(es[i]);j++){
        if(edge[i][j]==i){
          res[0] = res[1] = i;
          return 1;
        }
      }
    }
    walloc1d(&arr, N, &mem);
    walloc1d(&q, N, &mem);
    walloc1d(&bk, N, &mem);
    walloc1d(&vis, N, &mem);
    for(i=(0);i<(N);i++){
      arr[i] = -1;
    }
    for(i=(0);i<(N);i++){
      for(j=(0);j<(es[i]);j++){
        k = edge[i][j];
        if(arr[k] == i){
          res[0] = i;
          res[1] = k;
          res[2] = i;
          return 2;
        }
        arr[k] = i;
      }
    }
    for(i=(0);i<(N);i++){
      arr[i] = bk[i] = -1;
    }
    for(i=0;i<N;i++) vis[i] = 0;
    for(m=0;m<N;m++) if(!vis[m]){
    qs = qe = 0;
    q[qe++] = m;
    arr[m] = 0;
    while(qs < qe){
      i = q[qs++];
      vis[i] = 1;
      for(j=(0);j<(es[i]);j++){
        k = edge[i][j];
        if(arr[k]==-1){
          arr[k] = arr[i] + 1;
          bk[k] = i;
          q[qe++] = k;
          continue;
        }
        if(arr[k] == arr[i] - 1){
          continue;
        }
        qs = qe = 1;
        res[0] = i;
        q[0] = k;
        while(i!=k){
          if(arr[i] > arr[k]){
            res[qs++] = (i = bk[i]);
          }
          else{
            q[qe++] = (k = bk[k]);
          }
        }
        reverse(res, res+qs);
        for(i=(0);i<(qe);i++){
          res[qs++] = q[i];
        }
        return qs - 1;
      }
    }
    }
    return -1;
  }
}
;
unsigned long long HashMap_ullP_L[4];
template<class KEY, class VAL> struct HashMap{
  char*used;
  KEY*key;
  VAL*val;
  int mem;
  int n;
  int mask;
  int init_flag;
  VAL init_val;
  HashMap(){
    mem = 0;
    init_flag = 0;
  }
  ~HashMap(){
    free();
  }
  void expand(int nn){
    if(mem >= nn){
      return;
    }
    if(mem){
      free();
    }
    mem = nn;
    used = new char[nn];
    key = new KEY[nn];
    val = new VAL[nn];
  }
  void free(){
    if(mem){
      mem = 0;
      delete[] used;
      delete[] key;
      delete[] val;
    }
  }
  void init(int nn){
    int i;
    n = 1;
    nn = nn + (nn + 1) / 2;
    while(n < nn){
      n *= 2;
    }
    mask = n - 1;
    expand(n);
    for(i=(0);i<(n);i++){
      used[i] = 0;
    }
    init_flag = 0;
  }
  void init(int nn, VAL ini){
    int i;
    n = 1;
    nn = nn + (nn + 1) / 2;
    while(n < nn){
      n *= 2;
    }
    mask = n - 1;
    expand(n);
    for(i=(0);i<(n);i++){
      used[i] = 0;
    }
    init_flag = 1;
    init_val = ini;
  }
  inline int getHash(const int a){
    unsigned long long d = a;
    d = (((d * HashMap_ullP_L[0]) >> 32) * HashMap_ullP_L[1]) & mask;
    return d;
  }
  inline int getHash(const unsigned a){
    unsigned long long d = a;
    d = (((d * HashMap_ullP_L[0]) >> 32) * HashMap_ullP_L[1]) & mask;
    return d;
  }
  inline int getHash(const long long a){
    unsigned long long d = a;
    d = (((((d * HashMap_ullP_L[0]) >> 32) * HashMap_ullP_L[1]) >> 32) * HashMap_ullP_L[2]) & mask;
    return d;
  }
  inline int getHash(const unsigned long long a){
    unsigned long long d = a;
    d = (((((d * HashMap_ullP_L[0]) >> 32) * HashMap_ullP_L[1]) >> 32) * HashMap_ullP_L[2]) & mask;
    return d;
  }
  inline int getHash(const pair<int,int> a){
    unsigned long long d = (((unsigned long long)a.first) << 32) + ((unsigned long long)a.second);
    d = (((((d * HashMap_ullP_L[0]) >> 32) * HashMap_ullP_L[1]) >> 32) * HashMap_ullP_L[2]) & mask;
    return d;
  }
  inline VAL& operator[](const KEY a){
    int k = getHash(a);
    for(;;){
      if(used[k]==1 && key[k]==a){
        break;
      }
      if(used[k]==0){
        used[k] = 1;
        key[k] = a;
        if(init_flag){
          val[k] = init_val;
        }
        break;
      }
      k = (k+1) & mask;
    }
    return val[k];
  }
  inline bool exist(const KEY a){
    int k = getHash(a);
    for(;;){
      if(used[k]==1 && key[k]==a){
        return true;
      }
      if(used[k]==0){
        break;
      }
      k = (k+1) & mask;
    }
    return false;
  }
  template<class S> inline bool exist(const KEY a, S &res){
    int k = getHash(a);
    for(;;){
      if(used[k]==1 && key[k]==a){
        res = val[k];
        return true;
      }
      if(used[k]==0){
        break;
      }
      k = (k+1) & mask;
    }
    return false;
  }
}
;
int H;
int W;
int N;
int A[100000];
int B[100000];
HashMap<pair<int,int>,int> hs;
graph g;
int ress;
int res[200000+1];
int arr[200000+1];
int main(){
  int i;
  wmem = memarr;
  {
    int i;
    int j;
    int k;
    Rand rnd;
    for(i=(0);i<(20);i++){
      rnd.get(2);
    }
    for(i=(0);i<(4);i++){
      for(j=(0);j<(32);j++){
        k = rnd.get(1,62);
        HashMap_ullP_L[i] |= (1ULL << k);
      }
      HashMap_ullP_L[i] |= (1ULL << 0);
      HashMap_ullP_L[i] |= (1ULL << 63);
    }
  }
  int x;
  int y;
  rd(H);
  rd(W);
  rd(N);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);A[Lj4PdHRW] += (-1);
      rd(B[Lj4PdHRW]);B[Lj4PdHRW] += (-1);
    }
  }
  hs.init(N);
  for(i=(0);i<(N);i++){
    B[i] += 100000;
  }
  for(i=(0);i<(N);i++){
    hs[{A[i],B[i]}] = i;
  }
  g.setEdge(200000, N, A, B);
  ress = g.anUndirectedCycle(res);
  if(ress==-1){
    wt_L(ress);
    wt_L('\n');
    return 0;
  }
  if(res[0] >= 100000){
    arrRot(1,ress,res);
    res[ress] = res[0];
  }
  for(i=(0);i<(ress);i++){
    auto t_ynMSdg = ((res[i]));
    auto KrdatlYV = (( res[i+1]));
    x=t_ynMSdg;
    y=KrdatlYV;
    if(x > y){
      swap(x, y);
    }
    ;
    arr[i] = hs[{x,y}];
  }
  wt_L(ress);
  wt_L('\n');
  {
    int ao_dF3pO;
    if(ress==0){
      wt_L('\n');
    }
    else{
      for(ao_dF3pO=(0);ao_dF3pO<(ress-1);ao_dF3pO++){
        wt_L(arr[ao_dF3pO]+1);
        wt_L(' ');
      }
      wt_L(arr[ao_dF3pO]+1);
      wt_L('\n');
    }
  }
  assert(ress >= 4);
  for(i=(0);i<(ress+1);i+=(2)){
    assert(B[arr[i%ress]] == B[arr[(i+1)%ress]]);
  }
  for(i=(1);i<(ress+1);i+=(2)){
    assert(A[arr[i%ress]] == A[arr[(i+1)%ress]]);
  }
  {
    set<int> s;
    for(i=(0);i<(ress);i++){
      s.insert(arr[i]);
    }
    assert(s.size()==ress);
  }
  return 0;
}
// cLay version 20210717-1 [beta]

// --- original code ---
// int H, W, N, A[1d5], B[1d5];
// HashMap<pair<int,int>,int> hs;
// graph g;
// int ress, res[2d5+1], arr[];
// {
//   int x, y;
//   rd(H,W,N,(A--,B--)(N));
//   hs.init(N);
//   rep(i,N) B[i] += 1d5;
//   rep(i,N) hs[{A[i],B[i]}] = i;
//   g.setEdge(2d5, N, A, B);
//   ress = g.anUndirectedCycle(res);
//   assert(ress >= 4);
//   if(ress==-1) wt(ress), return 0;
//   if(res[0] >= 1d5) arrRot(1,ress,res), res[ress] = res[0];
// 
//   rep(i,ress){
//     (x, y) = (res[i], res[i+1]);
//     sortE(x, y);
//     arr[i] = hs[{x,y}];
//   }
//   wt(ress);
//   wt(arr(ress)+1);
// 
//   assert(ress >= 4);
//   rep(i,0,ress+1,2) assert(B[arr[i%ress]] == B[arr[(i+1)%ress]]);
//   rep(i,1,ress+1,2) assert(A[arr[i%ress]] == A[arr[(i+1)%ress]]);
//   {
//     set<int> s;
//     rep(i,ress) s.insert(arr[i]);
//     assert(s.size()==ress);
//   }
// 
//   // rep(i,ress+1) wt("--", A[arr[i%ress]],B[arr[i%ress]]-1d5);
// }
0