結果

問題 No.382 シャイな人たち (2)
ユーザー LayCurseLayCurse
提出日時 2019-09-21 09:46:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,270 ms / 8,000 ms
コード長 13,176 bytes
コンパイル時間 3,520 ms
コンパイル使用メモリ 235,832 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-17 09:53:30
合計ジャッジ時間 43,846 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,270 ms
5,248 KB
testcase_01 AC 2,129 ms
5,376 KB
testcase_02 AC 1,912 ms
5,376 KB
testcase_03 AC 1,797 ms
5,376 KB
testcase_04 AC 1,898 ms
5,376 KB
testcase_05 AC 1,961 ms
5,376 KB
testcase_06 AC 1,973 ms
5,376 KB
testcase_07 AC 1,819 ms
5,376 KB
testcase_08 AC 1,778 ms
5,376 KB
testcase_09 AC 1,848 ms
5,376 KB
testcase_10 AC 1,999 ms
5,376 KB
testcase_11 AC 1,854 ms
5,376 KB
testcase_12 AC 1,486 ms
5,376 KB
testcase_13 AC 1,908 ms
5,376 KB
testcase_14 AC 1,762 ms
5,376 KB
testcase_15 AC 1,741 ms
5,376 KB
testcase_16 AC 1,892 ms
5,376 KB
testcase_17 AC 1,396 ms
5,376 KB
testcase_18 AC 1,911 ms
5,376 KB
testcase_19 AC 1,484 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In member function 'int graph::maxIndependenceSet(int*, void*, int, int)':
main.cpp:572:10: warning: 'WYIGIcGE' may be used uninitialized [-Wmaybe-uninitialized]
  572 |     used[x] = 1;
      |          ^
main.cpp:543:11: note: 'WYIGIcGE' was declared here
  543 |       int WYIGIcGE;
      |           ^~~~~~~~
main.cpp:546:35: warning: 'FmcKpFmN' may be used uninitialized [-Wmaybe-uninitialized]
  546 |         if(RZTsC2BF==0 || FmcKpFmN<xr20shxY){
      |                           ~~~~~~~~^~~~~~~~~
main.cpp:541:17: note: 'FmcKpFmN' was declared here
  541 |       long long FmcKpFmN;
      |                 ^~~~~~~~
main.cpp:407:40: warning: 'y' may be used uninitialized [-Wmaybe-uninitialized]
  407 |               used[i] = used[x] = used[y] = 1;
      |                                        ^
main.cpp:318:9: note: 'y' was declared here
  318 |     int y;
      |         ^
main.cpp:400:29: warning: 'x' may be used uninitialized [-Wmaybe-uninitialized]
  400 |             for(j=(0);j<(es[x]);j++){
      |                             ^
main.cpp:317:9: note: 'x' was declared here
  317 |     int x;
      |         ^
main.cpp:373:17: warning: 'k' may be used uninitialized [-Wmaybe-uninitialized]
  373 |             deg[k]--;
      |                 ^
main.cpp:315:9: note: 'k' was declared here
  315 |     int k;
      |         ^

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
char memarr[96000000];
template<class S, class T> inline S max_L(S a,T b){
  return a>=b?a:b;
}
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 T1, class T2> void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){
  int i;
  pair<T1, T2> *arr;
  walloc1d(&arr, N, &mem);
  for(i=(0);i<(N);i++){
    arr[i].first = a[i];
    arr[i].second = b[i];
  }
  sort(arr, arr+N);
  for(i=(0);i<(N);i++){
    a[i] = arr[i].first;
    b[i] = arr[i].second;
  }
}
inline void rd(long long &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void wt_L(char a){
  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){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
template<class S, class T> inline S chmin(S &a, T b){
  if(a>b){
    a=b;
  }
  return a;
}
struct unionFind{
  int *d;
  int N;
  int M;
  inline void malloc(const int n){
    d = (int*)std::malloc(n*sizeof(int));
    M = n;
  }
  inline void free(void){
    std::free(d);
  }
  inline void walloc(const int n, void **mem=&wmem){
    walloc1d(&d, n, mem);
    M = n;
  }
  inline void init(const int n){
    int i;
    N = n;
    for(i=(0);i<(n);i++){
      d[i] = -1;
    }
  }
  inline void init(void){
    init(M);
  }
  inline int get(int a){
    int t = a;
    int k;
    while(d[t]>=0){
      t=d[t];
    }
    while(d[a]>=0){
      k=d[a];
      d[a]=t;
      a=k;
    }
    return a;
  }
  inline int connect(int a, int b){
    if(d[a]>=0){
      a=get(a);
    }
    if(d[b]>=0){
      b=get(b);
    }
    if(a==b){
      return 0;
    }
    if(d[a] < d[b]){
      d[a] += d[b];
      d[b] = a;
    }
    else{
      d[b] += d[a];
      d[a] = b;
    }
    return 1;
  }
  inline int operator()(int a){
    return get(a);
  }
  inline int operator()(int a, int b){
    return connect(a,b);
  }
  inline int& operator[](const int a){
    return d[a];
  }
  inline int size(int a){
    a = get(a);
    return -d[a];
  }
  inline int sizeList(int res[]){
    int i;
    int sz=0;
    for(i=(0);i<(N);i++){
      if(d[i]<0){
        res[sz++] = -d[i];
      }
    }
    return sz;
  }
}
;
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];
    }
  }
  graph reduce(int tn, int ind[], int self_e = 0, int dep_e = 0, void **mem = &wmem){
    int i;
    int j;
    int k;
    int M = 0;
    int x;
    int y;
    graph g;
    void *tmem;
    pair<int,int> *A;
    for(i=(0);i<(N);i++){
      M += es[i];
    }
    tmem = ((char*)(*mem)) + sizeof(int**) * N + sizeof(int*) * N + sizeof(int) * M + 16 * (N+2);
    walloc1d(&A, M, &tmem);
    M = 0;
    for(i=(0);i<(N);i++){
      x = ind[i];
      if(x < 0){
        continue;
      }
      for(j=(0);j<(es[i]);j++){
        y = ind[edge[i][j]];
        if(y < 0){
          continue;
        }
        if(self_e==0 && x==y){
          continue;
        }
        A[M++] = make_pair(x, y);
      }
    }
    if(dep_e==0){
      sort(A, A+M);
      k = 0;
      for(i=(0);i<(M);i++){
        if(k && A[k-1]==A[i]){
          continue;
        }
        A[k++] = A[i];
      }
      M = k;
    }
    g.N = tn;
    walloc1d(&g.es, tn, mem);
    walloc1d(&g.edge, tn, mem);
    for(i=(0);i<(tn);i++){
      g.es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      g.es[A[i].first]++;
    }
    for(i=(0);i<(tn);i++){
      walloc1d(&g.edge[i], g.es[i], mem);
    }
    for(i=(0);i<(tn);i++){
      g.es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      j = A[i].first;
      k = A[i].second;
      g.edge[j][g.es[j]++] = k;
    }
    return g;
  }
  void articulationDFS(int n, int b, int &k, int od[], int lw[], int vs[], int &ress, int res[]){
    int i;
    int j;
    int a = 0;
    int c = 0;
    vs[n] = 1;
    lw[n] = od[n] = k++;
    for(i=(0);i<(es[n]);i++){
      j = edge[n][i];
      if(j==b){
        continue;
      }
      if(!vs[j]){
        c++;
        articulationDFS(j, n, k, od, lw, vs, ress, res);
        chmin(lw[n], lw[j]);
        if(b != -1 && od[n] <= lw[j]){
          a = 1;
        }
      }
      else{
        chmin(lw[n], od[j]);
      }
    }
    if(b == -1 && c >= 2){
      a = 1;
    }
    if(a){
      res[ress++] = n;
    }
  }
  int articulation(int res[], void *mem=wmem){
    int i;
    int k = 0;
    int ress = 0;
    int *od;
    int *lw;
    int *vs;
    walloc1d(&od, N, &mem);
    walloc1d(&lw, N, &mem);
    walloc1d(&vs, N, &mem);
    for(i=(0);i<(N);i++){
      vs[i] = 0;
    }
    for(i=(0);i<(N);i++){
      if(!vs[i]){
        articulationDFS(i, -1, k, od, lw, vs, ress, res);
      }
    }
    return ress;
  }
  int maxIndependenceSet(int res[] = NULL, void *mem = wmem, int lim = -1, int skip = 0){
    int i;
    int j;
    int k;
    int m;
    int x;
    int y;
    int fg;
    int ress;
    int *deg;
    int *used;
    int *ind;
    int *rev;
    int *val;
    long long *pr;
    graph g;
    unionFind uf;
    void *tmem;
    if(N == 0){
      return 0;
    }
    if(N == 1){
      res[0] = 0;
      return 1;
    }
    if(N <= lim){
      return 0;
    }
    if(res==NULL){
      walloc1d(&res, N, &mem);
    }
    walloc1d(&deg, N, &mem);
    walloc1d(&used, N, &mem);
    walloc1d(&ind, N, &mem);
    walloc1d(&rev, N, &mem);
    walloc1d(&val, N, &mem);
    walloc1d(&pr, N, &mem);
    for(i=(0);i<(N);i++){
      deg[i] = es[i];
    }
    for(i=(0);i<(N);i++){
      used[i] = 0;
    }
    ress = 0;
    if(!(skip&1)){
      do{
        fg = 0;
        for(i=(0);i<(N);i++){
          if(!used[i] && deg[i] <= 1){
            fg = 1;
            res[ress++] = i;
            used[i] = 1;
            if(deg[i] == 0){
              continue;
            }
            for(j=(0);j<(es[i]);j++){
              k = edge[i][j];
              if(!used[k]){
                break;
              }
            }
            deg[k]--;
            used[k] = 1;
            for(j=(0);j<(es[k]);j++){
              m = edge[k][j];
              if(!used[m]){
                deg[m]--;
              }
            }
          }
        }
        for(i=(0);i<(N);i++){
          if(!used[i] && deg[i]==2){
            m = 0;
            for(j=(0);j<(es[i]);j++){
              k = edge[i][j];
              if(used[k]){
                continue;
              }
              if(m==0){
                x = k;
                m++;
              }
              else{
                y = k;
                m++;
              }
            }
            for(j=(0);j<(es[x]);j++){
              if(edge[x][j] == y){
                break;
              }
            }
            if(j < es[x]){
              fg = 1;
              used[i] = used[x] = used[y] = 1;
              res[ress++] = i;
              for(j=(0);j<(es[x]);j++){
                m = edge[x][j];
                if(!used[m]){
                  deg[m]--;
                }
              }
              for(j=(0);j<(es[y]);j++){
                m = edge[y][j];
                if(!used[m]){
                  deg[m]--;
                }
              }
            }
          }
        }
      }
      while(fg);
    }
    if(ress){
      k = 0;
      for(i=(0);i<(N);i++){
        if(used[i]){
          ind[i] = -1;
          continue;
        }
        ind[i] = k;
        rev[k] = i;
        k++;
      }
      g = reduce(k, ind, 1, 1, &mem);
      m = g.maxIndependenceSet(res+ress, mem, lim - ress, 1);
      for(i=(0);i<(m);i++){
        res[ress+i] = rev[res[ress+i]];
      }
      ress += m;
      sort(res, res+ress);
      return ress;
    }
    if(N-2 <= lim){
      return 0;
    }
    if(lim >= 1){
      for(i=(0);i<(N);i++){
        deg[i] = es[i];
      }
      sort(deg, deg+N);
      int ss = 0;
      for(k=(0);k<(N);k++){
        ss += es[k];
      }
      j = 0;
      for(i=(0);i<(N);i++){
        j += deg[i];
        if(2*j > ss){
          break;
        }
      }
      if(i <= lim){
        return 0;
      }
      for(i=(0);i<(N);i++){
        if(deg[i] > N-i-1){
          break;
        }
      }
      if(i <= lim){
        return 0;
      }
    }
    if(!(skip&2)){
      k = 0;
      uf.walloc(N, &mem);
      uf.init(N);
      for(i=(0);i<(N);i++){
        for(j=(0);j<(es[i]);j++){
          k += uf(i,edge[i][j]);
        }
      }
      if(k < N-1){
        for(i=(0);i<(N);i++){
          val[i] = i;
        }
        for(i=(0);i<(N);i++){
          deg[i] = 0;
        }
        for(i=(0);i<(N);i++){
          deg[uf(i)]++;
        }
        sortA_L(N, deg, val);
        y = N;
        for(x=(N)-1;x>=(0);x--){
          if(deg[x]){
            k = 0;
            for(i=(0);i<(N);i++){
              if(uf(i)!=x){
                ind[i] = -1;
                continue;
              }
              ind[i] = k;
              rev[k] = i;
              k++;
            }
            y -= k;
            tmem = mem;
            g = reduce(k, ind, 1, 1, &mem);
            m = g.maxIndependenceSet(res+ress, mem,max_L(-1, lim-(y-2*x)-ress), 3);
            mem = tmem;
            for(i=(0);i<(m);i++){
              res[ress+i] = rev[res[ress+i]];
            }
            ress += m;
          }
        }
        sort(res,res+ress);
        return ress;
      }
    }
    k = articulation(ind);
    for(i=(0);i<(N);i++){
      pr[i] = 0;
    }
    for(i=(0);i<(k);i++){
      pr[ind[i]] += (1LL<<40);
    }
    for(i=(0);i<(N);i++){
      for(j=(0);j<(es[i]);j++){
        pr[i] += (1LL<<20) - es[edge[i][j]];
      }
    }
    {
      int cTE1_r3A;
      int RZTsC2BF = 0;
      long long FmcKpFmN;
      long long xr20shxY;
      int WYIGIcGE;
      for(cTE1_r3A=(0);cTE1_r3A<(((N)-1)+1);cTE1_r3A++){
        xr20shxY = pr[cTE1_r3A];
        if(RZTsC2BF==0 || FmcKpFmN<xr20shxY){
          FmcKpFmN = xr20shxY;
          RZTsC2BF = 1;
          WYIGIcGE = cTE1_r3A;
        }
      }
      x =WYIGIcGE;
    }
    k = 0;
    for(i=(0);i<(N);i++){
      if(i == x){
        ind[i] = -1;
        continue;
      }
      ind[i] = k;
      rev[k] = i;
      k++;
    }
    tmem = mem;
    g = reduce(k, ind, 1, 1, &mem);
    ress = g.maxIndependenceSet(res, mem, lim);
    mem = tmem;
    for(i=(0);i<(ress);i++){
      res[i] = rev[res[i]];
    }
    k = 0;
    used[x] = 1;
    for(j=(0);j<(es[x]);j++){
      used[edge[x][j]] = 1;
    }
    for(i=(0);i<(N);i++){
      if(used[i]){
        ind[i] = -1;
        continue;
      }
      ind[i] = k;
      rev[k] = i;
      k++;
    }
    g = reduce(k, ind, 1, 1, &mem);
    m = g.maxIndependenceSet(deg, mem,max_L(ress, lim)-1);
    if(m+1 > ress){
      for(i=(0);i<(m);i++){
        res[i] = rev[deg[i]];
      }
      res[m++] = x;
      ress = m;
    }
    sort(res,res+ress);
    return ress;
  }
}
;
long long S;
int get_next(void){
  S = S * 12345 % 1000003;
  return S;
}
int N;
int M;
int A[10000];
int B[10000];
int P;
int ress;
int res[122];
int main(){
  int i;
  wmem = memarr;
  graph g;
  rd(S);
  N = get_next() % 120 + 2;
  P = get_next();
  for(i=(0);i<(N);i++){
    int j;
    int Lj4PdHRW = N;
    for(j=(i+1);j<(Lj4PdHRW);j++){
      if(get_next() >= P){
        A[M] = i;
        B[M++] = j;
      }
    }
  }
  g.setEdge(N,M,A,B);
  ress = g.maxIndependenceSet(res);
  if(ress==N){
    wt_L(-1);
    wt_L('\n');
  }
  else{
    wt_L(ress+1);
    wt_L('\n');
    {
      int KL2GvlyY;
      if(ress==0){
        putchar_unlocked('\n');
      }
      else{
        for(KL2GvlyY=(0);KL2GvlyY<(ress-1);KL2GvlyY++){
          wt_L(res[KL2GvlyY]+1);
          wt_L(' ');
        }
        wt_L(res[KL2GvlyY]+1);
        wt_L('\n');
      }
    }
  }
  return 0;
}
// cLay varsion 20190921-1 [beta]

// --- original code ---
// ll S;
// int get_next(void){
//   S = S * 12345 % 1000003;
//   return S;
// }
// 
// int N, M, A[10000], B[10000], P;
// int ress, res[122];
// {
//   graph g;
// 
//   rd(S);
//   N = get_next() % 120 + 2;
//   P = get_next();
//   rep(i,N) REP(j,i+1,N) if(get_next() >= P) A[M] = i, B[M++] = j;
//   g.setEdge(N,M,A,B);
// 
//   ress = g.maxIndependenceSet(res);
//   if(ress==N){
//     wt(-1);
//   } else {
//     wt(ress+1);
//     wt(res(ress)+1);
//   }
// }
0