結果

問題 No.363 門松サイクル
ユーザー 🐬hec🐬hec
提出日時 2016-04-17 23:32:14
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 192 ms / 4,000 ms
コード長 3,044 bytes
コンパイル時間 1,989 ms
コンパイル使用メモリ 171,356 KB
実行使用メモリ 22,912 KB
最終ジャッジ日時 2024-04-19 07:19:35
合計ジャッジ時間 7,558 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,272 KB
testcase_01 AC 4 ms
6,144 KB
testcase_02 AC 4 ms
6,272 KB
testcase_03 AC 4 ms
6,272 KB
testcase_04 AC 5 ms
6,272 KB
testcase_05 AC 5 ms
6,144 KB
testcase_06 AC 5 ms
6,272 KB
testcase_07 AC 4 ms
6,272 KB
testcase_08 AC 5 ms
6,016 KB
testcase_09 AC 62 ms
12,032 KB
testcase_10 AC 69 ms
12,416 KB
testcase_11 AC 128 ms
19,968 KB
testcase_12 AC 151 ms
18,688 KB
testcase_13 AC 138 ms
17,024 KB
testcase_14 AC 110 ms
16,128 KB
testcase_15 AC 121 ms
19,072 KB
testcase_16 AC 95 ms
15,488 KB
testcase_17 AC 184 ms
22,712 KB
testcase_18 AC 163 ms
18,264 KB
testcase_19 AC 125 ms
18,816 KB
testcase_20 AC 155 ms
18,304 KB
testcase_21 AC 192 ms
21,632 KB
testcase_22 AC 158 ms
18,048 KB
testcase_23 AC 150 ms
19,072 KB
testcase_24 AC 4 ms
6,144 KB
testcase_25 AC 4 ms
6,144 KB
testcase_26 AC 119 ms
22,912 KB
testcase_27 AC 119 ms
22,912 KB
testcase_28 AC 154 ms
20,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0