結果

問題 No.363 門松サイクル
ユーザー parukiparuki
提出日時 2016-06-17 02:19:13
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 155 ms / 4,000 ms
コード長 3,607 bytes
コンパイル時間 1,681 ms
コンパイル使用メモリ 175,736 KB
実行使用メモリ 21,968 KB
最終ジャッジ日時 2023-08-01 13:33:56
合計ジャッジ時間 5,599 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 57 ms
9,980 KB
testcase_10 AC 60 ms
10,536 KB
testcase_11 AC 111 ms
19,024 KB
testcase_12 AC 128 ms
17,476 KB
testcase_13 AC 125 ms
15,964 KB
testcase_14 AC 100 ms
14,740 KB
testcase_15 AC 97 ms
17,916 KB
testcase_16 AC 79 ms
13,588 KB
testcase_17 AC 127 ms
21,968 KB
testcase_18 AC 155 ms
17,328 KB
testcase_19 AC 121 ms
17,844 KB
testcase_20 AC 147 ms
17,296 KB
testcase_21 AC 137 ms
20,440 KB
testcase_22 AC 137 ms
17,164 KB
testcase_23 AC 109 ms
17,812 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 100 ms
21,856 KB
testcase_27 AC 92 ms
21,712 KB
testcase_28 AC 137 ms
19,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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