結果

問題 No.363 門松サイクル
ユーザー LayCurseLayCurse
提出日時 2016-04-18 00:23:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 94 ms / 4,000 ms
コード長 7,191 bytes
コンパイル時間 1,421 ms
コンパイル使用メモリ 166,188 KB
実行使用メモリ 39,436 KB
最終ジャッジ日時 2024-10-04 12:07:43
合計ジャッジ時間 4,017 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 3 ms
9,552 KB
testcase_03 AC 3 ms
9,668 KB
testcase_04 AC 2 ms
9,672 KB
testcase_05 AC 2 ms
7,496 KB
testcase_06 AC 2 ms
6,820 KB
testcase_07 AC 2 ms
6,816 KB
testcase_08 AC 2 ms
6,820 KB
testcase_09 AC 21 ms
20,276 KB
testcase_10 AC 22 ms
20,788 KB
testcase_11 AC 50 ms
34,992 KB
testcase_12 AC 55 ms
34,348 KB
testcase_13 AC 48 ms
32,364 KB
testcase_14 AC 39 ms
30,424 KB
testcase_15 AC 52 ms
34,776 KB
testcase_16 AC 43 ms
26,020 KB
testcase_17 AC 94 ms
38,920 KB
testcase_18 AC 66 ms
34,392 KB
testcase_19 AC 50 ms
33,424 KB
testcase_20 AC 66 ms
35,032 KB
testcase_21 AC 90 ms
38,088 KB
testcase_22 AC 54 ms
33,488 KB
testcase_23 AC 69 ms
33,652 KB
testcase_24 AC 3 ms
9,676 KB
testcase_25 AC 2 ms
6,820 KB
testcase_26 AC 74 ms
39,436 KB
testcase_27 AC 66 ms
39,176 KB
testcase_28 AC 54 ms
36,848 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void reader(double*)’:
main.cpp:15:29: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   15 | void reader(double *x){scanf("%lf",x);}
      |                        ~~~~~^~~~~~~~~

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

#define mygc(c) (c)=getchar_unlocked()
#define mypc(c) putchar_unlocked(c)

#define ll long long
#define ull unsigned ll

void reader(int *x){int k,m=0;*x=0;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){*x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);}
void reader(ll *x){int k,m=0;*x=0;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){*x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);}
void reader(double *x){scanf("%lf",x);}
int reader(char c[]){int i,s=0;for(;;){mygc(i);if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF) break;}c[s++]=i;for(;;){mygc(i);if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF) break;c[s++]=i;}c[s]='\0';return s;}
template <class T, class S> void reader(T *x, S *y){reader(x);reader(y);}
template <class T, class S, class U> void reader(T *x, S *y, U *z){reader(x);reader(y);reader(z);}
template <class T, class S, class U, class V> void reader(T *x, S *y, U *z, V *w){reader(x);reader(y);reader(z);reader(w);}

void writer(int x, char c){int s=0,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)mypc('-');while(s--)mypc(f[s]+'0');mypc(c);}
void writer(ll x, char c){int s=0,m=0;char f[20];if(x<0)m=1,x=-x;while(x)f[s++]=x%10,x/=10;if(!s)f[s++]=0;if(m)mypc('-');while(s--)mypc(f[s]+'0');mypc(c);}
void writer(double x, char c){printf("%.15f",x);mypc(c);}
void writer(const char c[]){int i;for(i=0;c[i]!='\0';i++)mypc(c[i]);}
void writer(const char x[], char c){int i;for(i=0;x[i]!='\0';i++)mypc(x[i]);mypc(c);}
template<class T> void writerLn(T x){writer(x,'\n');}
template<class T, class S> void writerLn(T x, S y){writer(x,' ');writer(y,'\n');}
template<class T, class S, class U> void writerLn(T x, S y, U z){writer(x,' ');writer(y,' ');writer(z,'\n');}
template<class T> void writerArr(T x[], int n){int i;if(!n){mypc('\n');return;}rep(i,n-1)writer(x[i],' ');writer(x[n-1],'\n');}

char memarr[87000000]; void *mem = memarr;
#define MD 1000000007

int isKado(int a, int b, int c){
  if(a != c && a < b && b > c) return 1;
  if(a != c && a > b && b < c) return 1;
  return 0;
}

void* readUndirectedGraph(int N, int M, int **es, int ***edge, void *mem, int origin=0){int i,*A;A=((int*)(((int**)mem)+N)+N+2*M);rep(i,2*M)reader(A+i),A[i]-=origin;*es=(int*)mem;*edge=(int**)((*es)+N);memset(*es,0,sizeof(int)*N);rep(i,2*M)(*es)[A[i]]++;(*edge)[0]=(int*)((*edge)+N);REP(i,1,N)(*edge)[i]=(*edge)[i-1]+(*es)[i-1];memset(*es,0,sizeof(int)*N);for(i=0;i<2*M;i+=2)(*edge)[A[i]][(*es)[A[i]]++]=A[i+1],(*edge)[A[i+1]][(*es)[A[i+1]]++]=A[i];return A;}
void getDist(int N, int root, int *es, int **edge, int dist[], void *mem){int i,j,k,*q,s,z;q=(int*)mem;rep(i,N)dist[i]=-1;dist[root]=0;s=0;z=1;q[0]=root;while(z){i=q[s++];z--;rep(j,es[i]){k=edge[i][j];if(dist[k]>=0)continue;dist[k]=dist[i]+1;q[s+z++]=k;}}}

template<class T> void doublingRMQBuild(T arr[], int n, int res[]){int i,k,h;rep(i,n)res[i]=i;for(k=1;;k++){h=(1<<(k-1));if(h>=n)break;rep(i,n){res[n*k+i]=res[n*(k-1)+i];if(i+h<n&&arr[res[n*k+i]]>arr[res[n*(k-1)+i+h]])res[n*k+i]=res[n*(k-1)+i+h];}}}
template<class T> void* doublingRMQBuild(T arr[], int n, int **res, void *workMemory){int i,k,h;*res=(int*)workMemory;rep(i,n)(*res)[i]=i;for(k=1;;k++){h=(1<<(k-1));if(h>=n)break;rep(i,n){(*res)[n*k+i]=(*res)[n*(k-1)+i];if(i+h<n&&arr[(*res)[n*k+i]]>arr[(*res)[n*(k-1)+i+h]])(*res)[n*k+i]=(*res)[n*(k-1)+i+h];}}return (void*)((*res)+n*k);}
template<class T> int doublingRMQQuery(T arr[], int n, int rmq[], int a, int b){int d,w=b-a+1,A,B;for(d=0;(1<<(d+1))<=w;d++);A=rmq[n*d+a];B=rmq[n*d+b-(1<<d)+1];return arr[A]>arr[B]?B:A;}
struct LCA{
  int *depth, *vs, *id, *rmq, N, *es, **edge;
  void dfs(int node, int bef, int dep, int *num){int i,k;id[node]=*num;vs[*num]=node;depth[(*num)++]=dep;rep(i,es[node]){k=edge[node][i];if(k==bef)continue;dfs(k,node,dep+1,num);vs[*num]=node;depth[(*num)++]=dep;}}
  void* init(int N_, int root, int *es_, int **edge_, void *workMemory){int m=0;N=N_;es=es_;edge=edge_;depth=(int*)workMemory;vs=depth+2*N-1;id=vs+2*N-1;dfs(root,-1,0,&m);return doublingRMQBuild(depth,2*N-1,&rmq,(void*)(id+N));}
  int get(int a, int b){return vs[doublingRMQQuery(depth,2*N-1,rmq,min(id[a],id[b]),max(id[a],id[b]))];}
  int getDepth(int a, int b){return depth[doublingRMQQuery(depth,2*N-1,rmq,min(id[a],id[b]),max(id[a],id[b]))];}
  int dist(int a, int b){return depth[id[a]]+depth[id[b]]-getDepth(a,b)*2;}
};


int N, Q;
int A[110000];
int *es, **edge;

int dep[110000];
int up[110000], goup[110000];

int upp[110000][20];

void getUpArr(int n, int b){
  int i, j, k;
  upp[n][0] = b;

  REP(i,1,20){
    if(upp[n][i-1]==-1) upp[n][i] = -1;
    else upp[n][i] = upp[upp[n][i-1]][i-1];
  }

  rep(i,es[n]){
    k = edge[n][i];
    if(k==b) continue;
    getUpArr(k, n);
  }
}

int getUp(int n, int d){
  int i;
  rep(i,20) if(d&1<<i) n = upp[n][i];
  return n;
}

int canup[200000];

void calCanup(int n, int b){
  int i, j, k;

  canup[n] = 0;
  if(up[n]!=-1 && up[up[n]]!=-1){
    if(isKado(A[n],A[up[n]],A[up[up[n]]])){
      canup[n] = canup[up[n]] + 1;
    }
  }

  rep(i,es[n]){
    k = edge[n][i];
    if(k==b) continue;
    calCanup(k, n);
  }
}

int tmpup[200000];

int main(){
  int i, j, k;
  int x, y, z, xx, yy;
  int step;
  LCA lca;

  reader(&N);
  rep(i,N) reader(A+i);
  mem = readUndirectedGraph(N, N-1, &es, &edge, mem, 1);
  getDist(N, 0, es, edge, dep, mem);
  mem = lca.init(N, 0, es, edge, mem);

  up[0] = -1;
  REP(i,1,N) rep(j,es[i]) if(dep[edge[i][j]] < dep[i]) up[i] = edge[i][j];

  getUpArr(0, -1);
  calCanup(0, -1);

  rep(i,N) tmpup[i] = up[i];

  reader(&Q);
  while(Q--){
    reader(&x,&y);
    x--; y--;
    xx = x; yy = y;
    if(up[x]==y || up[y]==x){ writerLn("NO"); continue; }

    z = lca.get(x, y);
    if(x==z) tmpup[x] = getUp(y, dep[y]-dep[x]-1);
    if(y==z) tmpup[y] = getUp(x, dep[x]-dep[y]-1);

//    printf("%d %d %d\n",up[x],x,y);
//    printf("%d %d %d\n",up[y],y,x);
    if(isKado(A[tmpup[x]],A[x],A[y])==0){ tmpup[xx]=up[xx]; tmpup[yy]=up[yy]; writerLn("NO"); continue; }
    if(isKado(A[tmpup[y]],A[y],A[x])==0){ tmpup[xx]=up[xx]; tmpup[yy]=up[yy]; writerLn("NO"); continue; }

    step = dep[x] - dep[z] - 5;
    if(step < 0) step = 0;
    if(canup[x] < step){ tmpup[xx]=up[xx]; tmpup[yy]=up[yy]; writerLn("NO"); continue; }
    x = getUp(x, step);

    step = dep[y] - dep[z] - 5;
    if(step < 0) step = 0;
    if(canup[y] < step){ tmpup[xx]=up[xx]; tmpup[yy]=up[yy]; writerLn("NO"); continue; }
    y = getUp(y, step);

    for(;;){
      if(tmpup[x]==tmpup[y]) break;
      if(dep[x] > dep[y]){
        if(!isKado(A[x],A[tmpup[x]],A[tmpup[tmpup[x]]])) break;
        x = up[x];
      } else {
        if(!isKado(A[y],A[tmpup[y]],A[tmpup[tmpup[y]]])) break;
        y = up[y];
      }
    }
    if(tmpup[x]!=tmpup[y]){ tmpup[xx]=up[xx]; tmpup[yy]=up[yy]; writerLn("NO"); continue; }

//    printf("%d %d %d\n",x,up[x],y);
    if(isKado(A[x],A[tmpup[x]],A[y])==0){ tmpup[xx]=up[xx]; tmpup[yy]=up[yy]; writerLn("NO"); continue; }
    tmpup[x]=up[x]; tmpup[y]=up[y]; 
    writerLn("YES");
  }

  return 0;
}
0