結果

問題 No.363 門松サイクル
ユーザー koyumeishikoyumeishi
提出日時 2016-03-16 01:55:18
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 3,288 bytes
コンパイル時間 1,584 ms
コンパイル使用メモリ 103,168 KB
実行使用メモリ 19,620 KB
最終ジャッジ日時 2024-10-01 20:09:47
合計ジャッジ時間 7,023 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 7 ms
6,816 KB
testcase_03 AC 4 ms
6,816 KB
testcase_04 AC 5 ms
6,820 KB
testcase_05 AC 4 ms
6,816 KB
testcase_06 AC 6 ms
6,820 KB
testcase_07 AC 5 ms
6,820 KB
testcase_08 AC 5 ms
6,816 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘std::istream& operator>>(std::istream&, std::vector<int>&)’:
main.cpp:19:78: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   19 | istream& operator >> (istream& is, vector<int>& vec){for(int& val: vec) scanf("%d", &val); return is;}
      |                                                                         ~~~~~^~~~~~~~~~~~
main.cpp: In function ‘std::istream& operator>>(std::istream&, std::vector<long long int>&)’:
main.cpp:20:90: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   20 | istream& operator >> (istream& is, vector<long long>& vec){for(long long& val: vec) scanf("%lld",/*"%I64d",*/ &val); return is;}
      |                                                                                     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp: In function ‘std::istream& operator>>(std::istream&, std::vector<double>&)’:
main.cpp:21:84: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   21 | istream& operator >> (istream& is, vector<double>& vec){for(double& val: vec) scanf("%lf", &val); return is;}
      |                                                                               ~~~~~^~~~~~~~~~~~~
main.cpp: In function ‘std::istream& operator>>(std::istream&, std::vector<std::__cxx11::basic_string<char> >&)’:
main.cpp:23:85: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   23 | istream& operator >> (istream& is, vector<string>& vec){for(string& val: vec) {scanf("%s", buff); val=buff;}; return is;}
      |                                                                                ~~~~~^~~~~~~~~~~~
main.cpp: In function ‘int main_()’:
main.cpp:44:46: warning: ignorin

ソースコード

diff #

//TLE
#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
istream& operator >> (istream& is, vector<int>& vec){for(int& val: vec) scanf("%d", &val); return is;}
istream& operator >> (istream& is, vector<long long>& vec){for(long long& val: vec) scanf("%lld",/*"%I64d",*/ &val); return is;}
istream& operator >> (istream& is, vector<double>& vec){for(double& val: vec) scanf("%lf", &val); return is;}
char buff[200000];
istream& operator >> (istream& is, vector<string>& vec){for(string& val: vec) {scanf("%s", buff); val=buff;}; return is;}

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(const 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<vector<int>> par(n, vector<int>(20));

	function<void(int,int,int)> dfs = [&](int pos, int last, int 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];
		}
	}

	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];
	};

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

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

		vector<int> x,y;
		x.push_back(u);
		while(u != p){
			u = par[u][0];
			x.push_back( u );
		}
		while(v != p){
			y.push_back( v );
			v = par[v][0];
		}
		while(y.size()){
			x.push_back( y.back() );
			y.pop_back();
		}

		for(int i=0; i<x.size(); i++){
			if(is_kadomatsu( {a[x[i]], a[x[(i+1)%x.size()]], a[x[(i+2)%x.size()]]} ) == false){
				ok = false;
				break;
			}
		}

		if(ok){ puts("YES"); cnt++;}
		else puts("NO");
	}

	//cerr << cnt << endl;

	return 0;
}

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