結果

問題 No.363 門松サイクル
ユーザー beetbeet
提出日時 2018-12-06 17:07:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 362 ms / 4,000 ms
コード長 4,232 bytes
コンパイル時間 4,324 ms
コンパイル使用メモリ 216,468 KB
実行使用メモリ 38,028 KB
最終ジャッジ日時 2023-10-12 02:49:20
合計ジャッジ時間 10,582 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 3 ms
4,368 KB
testcase_03 AC 3 ms
4,372 KB
testcase_04 AC 4 ms
4,368 KB
testcase_05 AC 3 ms
4,368 KB
testcase_06 AC 3 ms
4,368 KB
testcase_07 AC 3 ms
4,368 KB
testcase_08 AC 3 ms
4,368 KB
testcase_09 AC 124 ms
17,160 KB
testcase_10 AC 123 ms
18,408 KB
testcase_11 AC 274 ms
34,768 KB
testcase_12 AC 362 ms
32,440 KB
testcase_13 AC 313 ms
31,948 KB
testcase_14 AC 172 ms
30,312 KB
testcase_15 AC 187 ms
33,176 KB
testcase_16 AC 154 ms
22,504 KB
testcase_17 AC 309 ms
37,628 KB
testcase_18 AC 270 ms
34,368 KB
testcase_19 AC 178 ms
38,028 KB
testcase_20 AC 246 ms
35,112 KB
testcase_21 AC 295 ms
37,448 KB
testcase_22 AC 253 ms
33,976 KB
testcase_23 AC 250 ms
30,688 KB
testcase_24 AC 1 ms
4,372 KB
testcase_25 AC 1 ms
4,368 KB
testcase_26 AC 168 ms
37,324 KB
testcase_27 AC 166 ms
37,320 KB
testcase_28 AC 262 ms
37,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

struct LevelAncestor{
  int n,h;
  vector<vector<int> > G,par,lad;
  vector<int> dep,nxt,len,pth,ord,hs;
  LevelAncestor(){}
  LevelAncestor(int n):
    n(n),G(n),dep(n),nxt(n,-1),len(n),pth(n),ord(n),hs(n+1,0){
    h=1;
    while((1<<h)<=n) h++;
    par.assign(h,vector<int>(n,-1));
    
    for(int i=2;i<=n;i++) hs[i]=hs[i>>1]+1;
  }

  void add_edge(int u,int v){
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }
  
  void dfs(int v,int p,int d,int f){
    if(nxt[v]<0){
      par[0][nxt[v]=v]=p;
      len[v]=dep[v]=d;
      for(int u:G[v]){
        if(u==p) continue;
        dfs(u,v,d+1,0);
        if(len[v]<len[u]) nxt[v]=u,len[v]=len[u];
      }
    }
    if(!f) return;
    pth[v]=lad.size();
    lad.emplace_back();
    for(int k=v;;k=nxt[k]){
      lad.back().emplace_back(k);
      pth[k]=pth[v];
      if(k==nxt[k]) break;
    }
    for(;;p=v,v=nxt[v]){	
      for(int u:G[v])
        if(u!=p&&u!=nxt[v]) dfs(u,v,d+1,1);	
      if(v==nxt[v]) break;
    }
  }
  
  void build(int r=0){
    dfs(r,-1,0,1);
    par[0][r]=0;
    for(int k=0;k+1<h;k++){
      for(int v=0;v<n;v++){
        if(par[k][v]<0) par[k+1][v]=-1;
        else par[k+1][v]=par[k][par[k][v]];
      }
    }
    for(int i=0;i<(int)lad.size();i++){
      int v=lad[i][0],p=par[0][v];
      if(~p){
        int k=pth[p],l=min(ord[p]+1,(int)lad[i].size());
        lad[i].resize(l+lad[i].size());
        for(int j=0,m=lad[i].size();j+l<m;j++)
          lad[i][m-(j+1)]=lad[i][m-(j+l+1)];
        for(int j=0;j<l;j++)
          lad[i][j]=lad[k][ord[p]-l+j+1];
      }
      for(int j=0;j<(int)lad[i].size();j++)
        if(pth[lad[i][j]]==i) ord[lad[i][j]]=j;
    }
  }
  
  int lca(int u,int v){
    if(dep[u]>dep[v]) swap(u,v);
    for(int k=0;k<h;k++){
      if((dep[v]-dep[u])>>k&1){
        v=par[k][v];
      }
    }
    if(u==v) return u;
    for(int k=h-1;k>=0;k--){
      if(par[k][u]!=par[k][v]){
        u=par[k][u];
        v=par[k][v];
      }
    }
    return par[0][u];
  }

  int up(int v,int d){
    if(d==0) return v;
    v=par[hs[d]][v];
    d-=1LL<<hs[d];
    return lad[pth[v]][ord[v]-d];
  }
  
  int distance(int u,int v){
    return dep[u]+dep[v]-dep[lca(u,v)]*2;
  }
};


struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;

//INSERT ABOVE HERE
signed main(){
  int n;
  cin>>n;
  vector<int> a(n);
  for(int i=0;i<n;i++) cin>>a[i];
  
  LevelAncestor la(n);
  for(int i=1;i<n;i++){
    int x,y;
    cin>>x>>y;
    x--;y--;
    la.add_edge(x,y);
  }

  la.build();
  auto par=la.par;
  auto dep=la.dep;
  auto is_kado=[&](int x,int y,int z)->int{
                 if(x==y||x==z||y==z) return 0;
                 return (x<y&&y>z)||(x>y&&y<z);
               };
  auto check=[&](int v)->int{
               if(dep[v]<2) return 0;
               return is_kado(a[v],a[par[0][v]],a[par[1][v]]);
             };

  int h=la.h;
  vector<vector<int> > dp(h,vector<int>(n,0));
  for(int j=0;j<n;j++) dp[0][j]=check(j);
  for(int i=1;i<h;i++)
    for(int j=0;j<n;j++)
      dp[i][j]=dp[i-1][j]&&dp[i-1][par[i-1][j]];
  
  int q;
  cin>>q;
  for(int i=0;i<q;i++){
    int x,y;
    cin>>x>>y;
    x--;y--;
    if(la.distance(x,y)<=2){
      cout<<"NO\n";
      continue;
    }
    if(dep[x]>dep[y]) swap(x,y);
    int z=la.lca(x,y);
    int flg=1;
    
    if(x==z){      
      int cx=la.up(y,la.distance(y,z)-1);
      int py=par[0][y];
      flg&=is_kado(a[cx],a[x],a[y]);
      flg&=is_kado(a[x],a[y],a[py]);
    }else{
      int px=par[0][x];
      int py=par[0][y];
      flg&=is_kado(a[x],a[y],a[py]);
      flg&=is_kado(a[px],a[x],a[y]);
      
      int lx=la.up(x,la.distance(x,z)-1);
      int ly=la.up(y,la.distance(y,z)-1);
      flg&=is_kado(a[lx],a[z],a[ly]);
    }    
    
    for(int w:{x,y}){
      int v=w,d=dep[w]-dep[z]-1,k=0;
      while(d>0){
        if(d&1){
          flg&=dp[k][v];        
          v=par[k][v];
        }
        k++;
        d>>=1;
      }      
    }
    
    cout<<(flg?"YES\n":"NO\n");
  }
  cout<<flush;
  return 0;
}
0