結果
| 問題 | 
                            No.363 門松サイクル
                             | 
                    
| コンテスト | |
| ユーザー | 
                             🐬hec
                         | 
                    
| 提出日時 | 2016-04-17 23:32:14 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 180 ms / 4,000 ms | 
| コード長 | 3,044 bytes | 
| コンパイル時間 | 1,830 ms | 
| コンパイル使用メモリ | 174,928 KB | 
| 実行使用メモリ | 22,792 KB | 
| 最終ジャッジ日時 | 2024-10-10 23:58:50 | 
| 合計ジャッジ時間 | 7,293 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 27 | 
ソースコード
#include <bits/stdc++.h>
#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)
#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
#define popcount(n) (__builtin_popcountll(n))
template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}
using namespace std;
bool check(int a,int b,int c){
	if(a==b) return false;
	if(b==c) return false;
	if(c==a) return false;
	if(min({a,b,c})==b) return true;
	if(max({a,b,c})==b) return true;
	return false;
}
const int vmax=100010;
vector<int> graph[vmax];
int value[vmax];
int ok[vmax];
int depth[vmax],par[vmax][20];
void tree_dfs(int v,int p,int d){
	par[v][0]=p,depth[v]=d;
	for(auto &v2:graph[v]) if(v2!=p) tree_dfs(v2,v,d+1);
}
void init(int n){
	tree_dfs(0,-1,0);
	rep(j,19)rep(i,n){
		if(par[i][j]==-1)
			par[i][j+1]=-1;
		else
			par[i][j+1]=par[par[i][j]][j];
	}
}
tuple<int,int>  lca(int u,int v){
	if(depth[u] < depth[v]) swap(u,v);
	rrep(i,20) if((depth[u]-depth[v])>>i&1) u=par[u][i];
	if(u==v) return make_tuple(u,-1);
	rrep(i,20) if(par[u][i]!=par[v][i])u=par[u][i],v=par[v][i];
	return make_tuple(u,v);
}
void dfs(int v,int p,int p2){
	ok[v]=0;
	if(p!=-1) ok[v]+=ok[p];	
	if(p2!=-1) ok[v]+=check(value[p2],value[p],value[v]);		
	for(auto &v2:graph[v]) if(v2!=p) dfs(v2,v,p);
}
int trace(int v,int d){
	rrep(i,20) if(d>>i&1) v=par[v][i];
	return v;
}
int main(void){
	int n;
	scanf("%d",&n);
	rep(i,n) scanf("%d",value+i);
	rep(i,n-1){
		int x,y;
		scanf("%d%d",&x,&y);
		x--,y--;
		graph[x].push_back(y);
		graph[y].push_back(x);
	}
	dfs(0,-1,-1);
	init(n);
	int q;
	cin >> q;
	rep(i,q){
		int u,v;
		scanf("%d%d",&u,&v);
		u--,v--;
		if(u>v) swap(u,v);
		bool ans=true;
		
		
		int a,b,c;
		tie(a,c)=lca(u,v);
		if(c==-1){
			if(a==u) swap(u,v);
			const int ud=depth[u]-depth[a]-1;
			const int vchi=trace(u,ud);
			const int ucnt=ok[u]-ok[vchi];
			if(ucnt!=ud) ans=false;
			
			if(par[u][0]!=-1) ans &= check(value[par[u][0]],value[u],value[v]);
			ans &= check(value[u],value[v],value[vchi]);
		}else{
			b=par[a][0];
			ans &= check(value[a],value[b],value[c]);
			if(par[u][0]!=-1) ans &= check(value[par[u][0]],value[u],value[v]);
			if(par[v][0]!=-1) ans &= check(value[u],value[v],value[par[v][0]]);
			const int ud=depth[u]-depth[b]-1;
			const int ucnt=ok[u]-ok[trace(u,ud)];
			if(ucnt!=ud) ans=false;
			const int vd=depth[v]-depth[b]-1;
			const int vcnt=ok[v]-ok[trace(v,vd)];
			if(vcnt!=vd) ans=false;
		}
		if(ans)
			puts("YES");
		else
			puts("NO");
	}
	return 0;
}
            
            
            
        
            
🐬hec