結果

問題 No.363 門松サイクル
ユーザー 🐬hec🐬hec
提出日時 2016-04-17 23:32:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 190 ms / 4,000 ms
コード長 3,044 bytes
コンパイル時間 1,698 ms
コンパイル使用メモリ 173,672 KB
実行使用メモリ 22,628 KB
最終ジャッジ日時 2023-08-01 07:46:23
合計ジャッジ時間 7,408 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,544 KB
testcase_01 AC 3 ms
8,612 KB
testcase_02 AC 4 ms
8,912 KB
testcase_03 AC 4 ms
8,724 KB
testcase_04 AC 4 ms
8,764 KB
testcase_05 AC 5 ms
8,656 KB
testcase_06 AC 5 ms
8,728 KB
testcase_07 AC 5 ms
8,768 KB
testcase_08 AC 4 ms
8,664 KB
testcase_09 AC 61 ms
14,532 KB
testcase_10 AC 64 ms
14,768 KB
testcase_11 AC 132 ms
19,992 KB
testcase_12 AC 149 ms
19,300 KB
testcase_13 AC 136 ms
17,740 KB
testcase_14 AC 109 ms
17,488 KB
testcase_15 AC 119 ms
19,420 KB
testcase_16 AC 94 ms
18,800 KB
testcase_17 AC 168 ms
22,572 KB
testcase_18 AC 151 ms
17,856 KB
testcase_19 AC 115 ms
18,460 KB
testcase_20 AC 150 ms
18,024 KB
testcase_21 AC 190 ms
21,280 KB
testcase_22 AC 138 ms
17,912 KB
testcase_23 AC 133 ms
20,572 KB
testcase_24 AC 3 ms
8,628 KB
testcase_25 AC 4 ms
8,628 KB
testcase_26 AC 112 ms
22,628 KB
testcase_27 AC 113 ms
22,500 KB
testcase_28 AC 139 ms
20,160 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