結果

問題 No.363 門松サイクル
ユーザー chocorusk
提出日時 2020-01-01 13:29:06
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 460 ms / 4,000 ms
コード長 3,298 bytes
コンパイル時間 1,624 ms
コンパイル使用メモリ 127,436 KB
実行使用メモリ 25,628 KB
最終ジャッジ日時 2024-11-22 01:37:30
合計ジャッジ時間 9,446 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
struct LCA{
vector<vector<int>> g;
vector<int> d;
vector<vector<int>> p;
int log;
int n;
LCA(const vector<vector<int>> &g):n(g.size()), g(g), d(g.size()){
log=0;
while(1<<log<=n) log++;
p.resize(log, vector<int>(n));
}
void dfs(int x, int prev){
for(auto y:g[x]){
if(y==prev) continue;
d[y]=d[x]+1;
p[0][y]=x;
dfs(y, x);
}
}
void build(){
dfs(0, -1);
for(int i=1; i<log; i++){
for(int j=0; j<n; j++){
p[i][j]=p[i-1][p[i-1][j]];
}
}
}
int lca(int a, int b){
if(d[a]>d[b]) swap(a, b);
int dd=d[b]-d[a], i=0;
int a1=a, b1=b;
while(dd){
if(dd&1) b1=p[i][b1];
dd>>=1;
i++;
}
if(a1==b1) return a1;
for(int j=log-1; j>=0; j--){
if(p[j][a1]!=p[j][b1]){
a1=p[j][a1], b1=p[j][b1];
}
}
return p[0][a1];
}
int la(int a, int k){
int ret=a;
int i=0;
while(k){
if(k&1) ret=p[i][ret];
i++;
k>>=1;
}
return ret;
}
int myon(int x, int r){
return la(x, d[x]-d[r]-1);
}
int dist(int a, int b){
return d[a]+d[b]-2*d[lca(a, b)];
}
};
int n;
int a[100010];
vector<vector<int>> g;
bool ok[100010];
int s[100010];
int main()
{
cin>>n;
for(int i=0; i<n; i++) cin>>a[i];
g.resize(n);
for(int i=0; i<n-1; i++){
int x, y;
cin>>x>>y;
x--; y--;
g[x].push_back(y);
g[y].push_back(x);
}
LCA lca(g);
lca.build();
auto check=[&](int x, int y, int z){
if(a[x]!=a[z] && ((a[x]<a[y] && a[y]>a[z]) || (a[x]>a[y] && a[y]<a[z]))) return true;
else return false;
};
for(int i=0; i<n; i++){
if(lca.d[i]<=1) continue;
int x=lca.p[0][i], y=lca.p[1][i];
if(check(i, x, y)) ok[i]=1;
}
auto dfs=[&](auto dfs, int x, int p)->void{
for(auto y:g[x]){
if(y==p) continue;
s[y]=s[x]+ok[y];
dfs(dfs, y, x);
}
};
dfs(dfs, 0, -1);
int q; cin>>q;
for(int i=0; i<q; i++){
int x, y;
cin>>x>>y;
x--; y--;
int r=lca.lca(x, y);
int d=lca.d[x]+lca.d[y]-2*lca.d[r];
if(d%2==0 || d==1){
cout<<"NO"<<endl;
continue;
}
if(r==y) swap(x, y);
if(r==x){
int y1=lca.myon(y, r), y2=lca.p[0][y];
if(s[y]-s[y1]==lca.d[y]-lca.d[y1] && check(y2, y, r) && check(y, r, y1)){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
continue;
}
int x1=lca.myon(x, r), y1=lca.myon(y, r);
if(!check(x1, r, y1) || s[x]-s[x1]!=lca.d[x]-lca.d[x1] || s[y]-s[y1]!=lca.d[y]-lca.d[y1]){
cout<<"NO"<<endl;
continue;
}
int x2=lca.p[0][x], y2=lca.p[0][y];
if(check(x, y, y2) && check(x2, x, y)) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0