結果

問題 No.363 門松サイクル
ユーザー LayCurseLayCurse
提出日時 2016-04-18 00:14:32
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 6,218 bytes
コンパイル時間 1,419 ms
コンパイル使用メモリ 164,472 KB
実行使用メモリ 44,580 KB
最終ジャッジ日時 2024-10-04 11:19:07
合計ジャッジ時間 8,632 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,628 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
7,496 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 2 ms
6,816 KB
testcase_25 WA -
testcase_26 TLE -
testcase_27 -- -
testcase_28 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 main(){
  int i, j, k;
  int x, y, z;
  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);

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

    z = lca.get(x, y);
    if(x==z) up[x] = getUp(y, dep[y]-dep[x]-1);
    if(y==z) up[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[up[x]],A[x],A[y])==0){ writerLn("NO"); continue; }
    if(isKado(A[up[y]],A[y],A[x])==0){ writerLn("NO"); continue; }

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

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

  return 0;
}
0