結果
問題 | No.363 門松サイクル |
ユーザー |
|
提出日時 | 2016-06-17 02:19:13 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 159 ms / 4,000 ms |
コード長 | 3,607 bytes |
コンパイル時間 | 1,883 ms |
コンパイル使用メモリ | 176,504 KB |
実行使用メモリ | 22,144 KB |
最終ジャッジ日時 | 2024-10-11 05:16:04 |
合計ジャッジ時間 | 5,636 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 27 |
ソースコード
#include "bits/stdc++.h"using namespace std;#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))#define rep(i,j) FOR(i,0,j)#define each(x,y) for(auto &(x):(y))#define mp make_pair#define all(x) (x).begin(),(x).end()#define debug(x) cout<<#x<<": "<<(x)<<endl#define smax(x,y) (x)=max((x),(y))#define smin(x,y) (x)=min((x),(y))#define MEM(x,y) memset((x),(y),sizeof (x))#define sz(x) (int)(x).size()typedef long long ll;typedef pair<int, int> pii;typedef vector<int> vi;typedef vector<ll> vll;class LCA{public:LCA(){ }LCA(const vector<vector<int>> &G, int root = 0):V((int)G.size()), depth(V), parent(V){rep(i, LG)ancestor[i] = vector<int>(V);dfs(root, 0, 0, G);rep(i, V)ancestor[0][i] = parent[i];rep(i,LG-1)rep(v,V)ancestor[i + 1][v] = ancestor[i][ancestor[i][v]];}int get(int u, int v){if(depth[u] < depth[v]) swap(u, v);int dist = depth[u] - depth[v];for(int i = 0; i < LG; ++i)if((dist >> i) & 1)u = ancestor[i][u];if(u == v) return u;for(int i = LG - 1; i >= 0; --i){if(ancestor[i][u] != ancestor[i][v]){u = ancestor[i][u];v = ancestor[i][v];}}return ancestor[0][u];}int getDepth(int u){return depth[u];}int goUp(int u, int len){for(int i = 0; i < LG; ++i)if(len >> i & 1)u = ancestor[i][u];return u;}int distance(int u, int v){return depth[u] + depth[v] - 2 * depth[get(u, v)];}int getParent(int u){return parent[u];}private:static const int LG = 18;int V;vector<int> depth, parent, ancestor[LG];void dfs(int v, int p, int d, const vector<vector<int>> &G){depth[v] = d;parent[v] = p;for(int to : G[v]){if(to != p){dfs(to, v, d + 1, G);}}}};const int N_MAX = (int)1e5 + 9;int N, A[N_MAX], kad[N_MAX];vector<vector<int>> G;LCA lca;bool isK(int a, int b, int c){return (a!=b&&b!=c&&c!=a)&&((a<b&&b>c)||(a>b&&b<c));}void dfs(int u, int v, int w){if(w != -1 && isK(A[u], A[v], A[w]))kad[u] = kad[v] + 1;each(to, G[u])if(to != v)dfs(to, u, v);}void init(){cin >> N;G.resize(N);rep(i, N)scanf("%d", &A[i]);rep(i, N - 1){int u, v;scanf("%d%d", &u, &v);G[--u].push_back(--v);G[v].push_back(u);}lca = LCA(G);dfs(0, -1, -1);}int solve(int u, int v){int duv = lca.distance(u, v);if(duv < 2)return 0;int w = lca.get(u, v);if(w != u&&w != v){int dwu = lca.getDepth(u) - lca.getDepth(w);int dwv = lca.getDepth(v) - lca.getDepth(w);if(kad[u] < dwu - 1 || kad[v] < dwv - 1)return 0;int uu = lca.goUp(u, dwu - 1);int vv = lca.goUp(v, dwv - 1);if(!isK(A[uu], A[w], A[vv]))return 0;int pu = lca.getParent(u);int pv = lca.getParent(v);if(!isK(A[pu], A[u], A[v]))return 0;if(!isK(A[u], A[v], A[pv]))return 0;} else{if(u == w)swap(u, v);if(kad[u] < duv - 1)return 0;int pu = lca.getParent(u);if(!isK(A[pu], A[u], A[v]))return 0;int vc = lca.goUp(u, duv - 1);if(!isK(A[u], A[v], A[vc]))return 0;}return 1;}int main(){init();int Q;cin >> Q;while(Q--){int u, v;scanf("%d%d", &u, &v);puts(solve(--u, --v) ? "YES" : "NO");}}