結果

問題 No.363 門松サイクル
ユーザー koyumeishikoyumeishi
提出日時 2016-03-16 01:49:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 496 ms / 4,000 ms
コード長 3,570 bytes
コンパイル時間 941 ms
コンパイル使用メモリ 104,516 KB
実行使用メモリ 36,068 KB
最終ジャッジ日時 2024-04-09 19:54:58
合計ジャッジ時間 9,737 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 5 ms
6,940 KB
testcase_03 AC 4 ms
6,944 KB
testcase_04 AC 5 ms
6,944 KB
testcase_05 AC 5 ms
6,944 KB
testcase_06 AC 6 ms
6,940 KB
testcase_07 AC 5 ms
6,940 KB
testcase_08 AC 6 ms
6,944 KB
testcase_09 AC 169 ms
13,056 KB
testcase_10 AC 168 ms
13,312 KB
testcase_11 AC 309 ms
27,392 KB
testcase_12 AC 383 ms
25,088 KB
testcase_13 AC 351 ms
20,480 KB
testcase_14 AC 301 ms
18,560 KB
testcase_15 AC 291 ms
25,728 KB
testcase_16 AC 260 ms
20,352 KB
testcase_17 AC 451 ms
35,968 KB
testcase_18 AC 413 ms
21,888 KB
testcase_19 AC 349 ms
22,528 KB
testcase_20 AC 397 ms
22,016 KB
testcase_21 AC 496 ms
32,384 KB
testcase_22 AC 396 ms
21,760 KB
testcase_23 AC 395 ms
28,416 KB
testcase_24 AC 1 ms
6,944 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 344 ms
36,068 KB
testcase_27 AC 348 ms
35,964 KB
testcase_28 AC 417 ms
29,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <functional>
#include <set>
#include <ctime>
#include <random>
#include <chrono>
#include <cassert>
using namespace std;
//input from stdin
template<class T> istream& operator >> (istream& is, vector<T>& vec){for(T& val: vec) is >> val; return is;}
template<class T> istream& operator ,  (istream& is, T& val){ return is >> val;}
template<class T> ostream& operator << (ostream& os, const vector<T>& vec){for(int i=0; i<vec.size(); i++) os << vec[i] << (i==vec.size()-1?"":" "); return os;}
template<class T> ostream& operator ,  (ostream& os, const T& val){ return os << " " << val;}
template<class T> ostream& operator >> (ostream& os, const T& val){ return os << " " << val;}

bool is_kadomatsu(vector<int> v){
	if(v.size()!=3) return false;
	if(v[0]==v[1] || v[1]==v[2] || v[2]==v[0]) return false;
	return (v[0]<v[1] && v[1]>v[2]) || (v[0]>v[1] && v[1]<v[2]);
}

int main_(){
	int n;
	cin >> n;
	vector<int> a(n);
	cin >> a;
	vector<vector<int>> G(n);
	for(int i=0; i<n-1; i++){
		int u,v; cin >> u,v;
		//scanf("%d%d", &u,&v);
		u--,v--;
		G[u].push_back(v);
		G[v].push_back(u);
	}

	vector<int> depth(n, -1);
	vector<int> kadomatsu(n, 0);
	vector<vector<int>> par(n, vector<int>(20));

	function<void(int,int,int)> dfs = [&](int pos, int last, int last2){
		kadomatsu[pos] = is_kadomatsu({a[pos],a[last],a[last2]});
		depth[pos] = depth[last]+1;
		par[pos][0] = last;
		for(int next : G[pos]){
			if(next == last) continue;
			dfs(next, pos, last);
		}
	};
	dfs(0,0,0);

	for(int k=1; k<20; k++){
		for(int i=0; i<n; i++){
			par[i][k] = par[ par[i][k-1] ][k-1];
			kadomatsu[i] |= ( kadomatsu[i] & kadomatsu[ par[i][k-1] ] & (1<<(k-1)) ) << 1;
		}
	}

	auto kth_parent = [&](int pos, int k){
		if(k<0) return -1;
		for(int i=0; k!=0; k>>=1, i++) if(k&1) pos = par[pos][i];
		return pos;
	};

	auto lca = [&](int x, int y){
		if(depth[x] < depth[y]) swap(x,y);
		x = kth_parent(x, depth[x] - depth[y]);
		if(x==y) return x;
		for(int k=19; k>=0; k--){
			if(par[x][k] != par[y][k]){
				x = par[x][k];
				y = par[y][k];
			}
		}
		return par[x][0];
	};

	vector<int> log_k(n+1, 0);
	for(int i=2, tmp=0; i<log_k.size(); i++){
		log_k[i] = tmp;
		if((i&-i) == i) tmp++;
	}

	auto is_kadomatsu_line = [&](int x, int y){
		if(depth[x] < depth[y]) swap(x,y);
		//assert(lca(x,y) == y);
		int d = depth[x] - depth[y] - 2;
		if(d<0) return true;
		int k = log_k[d+1];
		y = kth_parent(x, d-(1<<k)+1);
		int ok = kadomatsu[x] & kadomatsu[y] & (1<<k);
		return ok>0;
	};

	int q;
	cin >> q;
	while(q--){
		int u,v;
		//scanf("%d%d", &u,&v);
		cin >> u,v;
		u--,v--;
		assert(u!=v);

		int p = lca(u,v);
		bool ok = true;

		if(p==u || p==v){	//straight line
			if(p==u) swap(u,v); // v==p
			int pu = kth_parent(u, depth[u]-depth[p]-1);
			int qu = par[u][0];
			ok = is_kadomatsu_line(u,p) && is_kadomatsu({a[qu], a[u], a[p]}) && is_kadomatsu({a[u], a[v], a[pu]});
		}else{
			int pu = kth_parent(u, depth[u]-depth[p]-1);
			int pv = kth_parent(v, depth[v]-depth[p]-1);
			int qu = par[u][0];
			int qv = par[v][0];
			ok = (
				is_kadomatsu_line(u,p) &&
				is_kadomatsu_line(v,p) && 
				is_kadomatsu({a[pu], a[p], a[pv]}) &&
				is_kadomatsu({a[qu], a[u], a[v] }) &&
				is_kadomatsu({a[u],  a[v], a[qv]})
				);
		}

		if(ok) cout << "YES" << "\n";
		else cout << "NO" << "\n";
	}
	return 0;
}

int main(){
	//auto s = clock();
	main_();
	//cerr << clock()-s << " [ms] " << endl;
	return 0;
}
0