結果

問題 No.1153 ねこちゃんゲーム
ユーザー LayCurseLayCurse
提出日時 2020-08-07 22:30:49
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 7,374 bytes
コンパイル時間 3,435 ms
コンパイル使用メモリ 228,572 KB
実行使用メモリ 72,288 KB
最終ジャッジ日時 2023-10-25 02:16:04
合計ジャッジ時間 22,407 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
25,084 KB
testcase_01 AC 7 ms
20,708 KB
testcase_02 AC 7 ms
20,840 KB
testcase_03 AC 7 ms
20,788 KB
testcase_04 AC 8 ms
20,796 KB
testcase_05 AC 7 ms
20,788 KB
testcase_06 AC 7 ms
20,788 KB
testcase_07 AC 172 ms
48,464 KB
testcase_08 AC 179 ms
48,488 KB
testcase_09 AC 180 ms
48,448 KB
testcase_10 AC 183 ms
48,484 KB
testcase_11 AC 188 ms
48,464 KB
testcase_12 AC 182 ms
48,484 KB
testcase_13 AC 183 ms
48,464 KB
testcase_14 AC 179 ms
48,436 KB
testcase_15 AC 174 ms
48,468 KB
testcase_16 AC 180 ms
48,476 KB
testcase_17 AC 204 ms
49,152 KB
testcase_18 AC 199 ms
49,872 KB
testcase_19 AC 201 ms
49,884 KB
testcase_20 AC 208 ms
51,292 KB
testcase_21 AC 202 ms
51,288 KB
testcase_22 AC 196 ms
48,996 KB
testcase_23 AC 191 ms
49,540 KB
testcase_24 AC 193 ms
49,532 KB
testcase_25 AC 202 ms
50,600 KB
testcase_26 AC 196 ms
50,576 KB
testcase_27 AC 193 ms
64,480 KB
testcase_28 AC 250 ms
72,288 KB
testcase_29 AC 246 ms
70,424 KB
testcase_30 AC 250 ms
70,216 KB
testcase_31 AC 166 ms
54,936 KB
testcase_32 AC 99 ms
41,616 KB
testcase_33 AC 100 ms
41,604 KB
testcase_34 AC 97 ms
41,800 KB
testcase_35 AC 101 ms
41,604 KB
testcase_36 AC 98 ms
42,036 KB
testcase_37 TLE -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#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);
}
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');
  }
}
inline void wt_L(const char c[]){
  int i=0;
  for(i=0;c[i]!='\0';i++){
    my_putchar_unlocked(c[i]);
  }
}
struct graph{
  int N;
  int *es;
  int **edge;
  void setEdgeRootedTree(int N__, int M, int A[], int B[], int root=0, int reorder=0, int cnv[] = NULL, void **mem = &wmem){
    int i;
    int j;
    int k;
    int *dist;
    int *q;
    int qs;
    int qe;
    int *ind;
    void *tmem;
    N = N__;
    tmem = ((char*)(*mem)) + (sizeof(int) * N + 15) + (sizeof(int*) * N + 15) + (sizeof(int) * M + 15 * 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], &tmem);
    }
    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];
    }
    walloc1d(&dist, N, &tmem);
    walloc1d(&q, N, &tmem);
    walloc1d(&ind, N, &tmem);
    if(cnv==NULL){
      walloc1d(&cnv, N, &tmem);
    }
    for(i=(0);i<(N);i++){
      dist[i] = -1;
    }
    dist[root] = 0;
    qs = qe = 0;
    q[qe++] = root;
    while(qs < qe){
      i = q[qs++];
      for(j=(0);j<(es[i]);j++){
        k = edge[i][j];
        if(dist[k]==-1){
          dist[k] = dist[i] + 1;
          q[qe++] = k;
        }
      }
    }
    if(reorder == 0){
      for(i=(0);i<(N);i++){
        cnv[i] = i;
      }
      for(i=(0);i<(N);i++){
        ind[i] = i;
      }
    }
    else{
      for(i=(0);i<(N);i++){
        cnv[i] = q[i];
      }
      for(i=(0);i<(N);i++){
        ind[cnv[i]] = i;
      }
    }
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      j = A[i];
      k = B[i];
      if(dist[j] > dist[k]){
        swap(j, k);
      }
      es[ind[j]]++;
    }
    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++){
      j = A[i];
      k = B[i];
      if(dist[j] > dist[k]){
        swap(j, k);
      }
      j = ind[j];
      k = ind[k];
      edge[j][es[j]++] = k;
    }
  }
}
;
int N;
int M;
int A[200000];
int U[200000];
int V[200000];
graph g;
int up[200000];
int dw_g[200000];
int tot_g[200000];
int up_g[200000];
map<int,int> s[200000];
void dfs(int n){
  int Lj4PdHRW;
  int i;
  for(Lj4PdHRW=(0);Lj4PdHRW<(g.es[n]);Lj4PdHRW++){
    auto &i = g.edge[n][Lj4PdHRW];
    up[i] = n;
    dfs(i);
    s[n][dw_g[i]]++;
  }
  for(i=0;;i++){
    if(s[n].count(i)==0 || s[n][i]==0){
      break;
    }
  }
  dw_g[n] = i;
}
void dfs2(int n){
  int t_ynMSdg;
  int i;
  int j;
  int k;
  if(up[n] != -1){
    k = up[n];
    s[k][dw_g[n]]--;
    for(i=0;;i++){
      if(s[k].count(i)==0 || s[k][i]==0){
        break;
      }
    }
    up_g[n] = i;
    s[k][dw_g[n]]++;
    s[n][up_g[n]]++;
  }
  for(i=0;;i++){
    if(s[n].count(i)==0 || s[n][i]==0){
      break;
    }
  }
  tot_g[n] = i;
  for(t_ynMSdg=(0);t_ynMSdg<(g.es[n]);t_ynMSdg++){
    auto &i = g.edge[n][t_ynMSdg];
    dfs2(i);
  }
}
int main(){
  wmem = memarr;
  int i;
  int j;
  int k;
  int x = 0;
  set<int> chked;
  rd(N);
  rd(M);
  {
    int tU__gIr_;
    for(tU__gIr_=(0);tU__gIr_<(M);tU__gIr_++){
      rd(A[tU__gIr_]);A[tU__gIr_] += (-1);
    }
  }
  {
    int V9aVTaxx;
    for(V9aVTaxx=(0);V9aVTaxx<(N-1);V9aVTaxx++){
      rd(U[V9aVTaxx]);U[V9aVTaxx] += (-1);
      rd(V[V9aVTaxx]);V[V9aVTaxx] += (-1);
    }
  }
  g.setEdgeRootedTree(N,N-1,U,V);
  up[0] = -1;
  dfs(0);
  dfs2(0);
  for(i=(0);i<(M);i++){
    x ^= tot_g[A[i]];
  }
  if(x==0){
    wt_L(-1);
    wt_L(' ');
    wt_L(-1);
    wt_L('\n');
    return 0;
  }
  for(k=(0);k<(M);k++){
    int AlM5nNnR;
    i = A[k];
    if(chked.count(k)){
      continue;
    }
    chked.insert(k);
    if(up[i] >= 0 && (x ^ tot_g[i] ^ up_g[i])==0){
      wt_L(k+1);
      wt_L(' ');
      wt_L(up[i]+1);
      wt_L('\n');
      return 0;
    }
    for(AlM5nNnR=(0);AlM5nNnR<(g.es[i]);AlM5nNnR++){
      auto &j = g.edge[i][AlM5nNnR];
      if((x ^ tot_g[i] ^ dw_g[j])==0){
        wt_L(k+1);
        wt_L(' ');
        wt_L(j+1);
        wt_L('\n');
        return 0;
      }
    }
  }
  wt_L("not found");
  wt_L('\n');
  return 0;
}
// cLay varsion 20200509-1

// --- original code ---
// int N, M, A[2d5], U[2d5], V[2d5];
// 
// graph g;
// int up[2d5], dw_g[2d5], tot_g[2d5], up_g[2d5];
// map<int,int> s[2d5];
// 
// void dfs(int n){
//   int i;
//   rep[g.edge[n]](i,g.es[n]){
//     up[i] = n;
//     dfs(i);
//     s[n][dw_g[i]]++;
//   }
//   for(i=0;;i++) if(s[n].count(i)==0 || s[n][i]==0) break;
//   dw_g[n] = i;
// }
// 
// void dfs2(int n){
//   int i, j, k;
// 
//   if(up[n] != -1){
//     k = up[n];
//     s[k][dw_g[n]]--;
//     for(i=0;;i++) if(s[k].count(i)==0 || s[k][i]==0) break;
//     up_g[n] = i;
//     s[k][dw_g[n]]++;
//     s[n][up_g[n]]++;
//   }
//   for(i=0;;i++) if(s[n].count(i)==0 || s[n][i]==0) break;
//   tot_g[n] = i;
// 
//   rep[g.edge[n]](i,g.es[n]){
//     dfs2(i);
//   }
// }
// 
// {
//   int i, j, k, x = 0;
//   set<int> chked;
// 
//   rd(N,M,(A--)(M),(U--,V--)(N-1));
//   g.setEdgeRootedTree(N,N-1,U,V);
// 
//   up[0] = -1;
//   dfs(0);
//   dfs2(0);
// 
// //  rep(i,N) wt(i,dw_g[i],up_g[i],tot_g[i]);
// 
//   rep(i,M) x ^= tot_g[A[i]];
//   if(x==0) wt(-1, -1), return 0;
// 
//   rep(k,M){
//     i = A[k];
//     if(chked.count(k)) continue;
//     chked.insert(k);
//     if(up[i] >= 0 && (x ^ tot_g[i] ^ up_g[i])==0) wt(k+1, up[i]+1), return 0;
//     rep[g.edge[i]](j,g.es[i]){
//       if((x ^ tot_g[i] ^ dw_g[j])==0) wt(k+1, j+1), return 0;
//     }
//   }
// 
//   wt("not found");
// }
0