結果

問題 No.363 門松サイクル
ユーザー fumofumofunifumofumofuni
提出日時 2022-09-10 13:34:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 511 ms / 4,000 ms
コード長 5,626 bytes
コンパイル時間 3,389 ms
コンパイル使用メモリ 220,260 KB
実行使用メモリ 20,952 KB
最終ジャッジ日時 2023-08-17 17:39:29
合計ジャッジ時間 11,315 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 6 ms
4,376 KB
testcase_05 AC 6 ms
4,380 KB
testcase_06 AC 6 ms
4,380 KB
testcase_07 AC 6 ms
4,380 KB
testcase_08 AC 6 ms
4,376 KB
testcase_09 AC 179 ms
9,868 KB
testcase_10 AC 184 ms
10,404 KB
testcase_11 AC 331 ms
18,656 KB
testcase_12 AC 440 ms
17,404 KB
testcase_13 AC 401 ms
16,424 KB
testcase_14 AC 332 ms
15,080 KB
testcase_15 AC 312 ms
17,808 KB
testcase_16 AC 272 ms
12,920 KB
testcase_17 AC 508 ms
20,952 KB
testcase_18 AC 469 ms
17,684 KB
testcase_19 AC 405 ms
18,428 KB
testcase_20 AC 474 ms
17,864 KB
testcase_21 AC 511 ms
20,004 KB
testcase_22 AC 446 ms
17,264 KB
testcase_23 AC 444 ms
17,124 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 346 ms
20,860 KB
testcase_27 AC 357 ms
20,952 KB
testcase_28 AC 465 ms
20,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={0,1,0,-1,1,1,-1,-1,0};
const ll dx[9]={1,0,-1,0,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
bool isKad(ll a,ll b,ll c){
    if(a==b||a==c||b==c)return false;
    if(a<b&&b<c)return false;
    if(a>b&&b>c)return false;
    return true;
}

struct Edge {
    long long to;
};
using Graph = vector<vector<Edge>>;
struct LCA {
    vector<vector<int>> parent;  // parent[k][u]:= u の 2^k 先の親
    vector<int> dist;            // root からの距離
    LCA(const Graph &G, int root = 0) { init(G, root); }
    void init(const Graph &G, int root = 0) {
        int V = G.size();
        int K = 1;
        while ((1 << K) < V) K++;
        parent.assign(K, vector<int>(V, -1));
        dist.assign(V, -1);
        dfs(G, root, -1, 0);
        for (int k = 0; k + 1 < K; k++) {
            for (int v = 0; v < V; v++) {
                if (parent[k][v] >= 0) {
                    parent[k + 1][v] = parent[k][parent[k][v]];
                }
            }
        }
    }
    // 根からの距離と1つ先の頂点を求める
    void dfs(const Graph &G, int v, int p, int d) {
        parent[0][v] = p;
        dist[v] = d;
        for (auto e : G[v]) {
            if (e.to != p) {
                dfs(G, e.to, v, d + 1);
            }
        }
    }
    int query(int u, int v) {
        if (dist[u] < dist[v]) swap(u, v);  // u の方が深いとする
        int K = parent.size();
        // LCA までの距離を同じにする
        for (int k = 0; k < K; k++) {
            if ((dist[u] - dist[v])&(1<<k) ){
                u = parent[k][u];
            }
        }
        // 二分探索で LCA を求める
        if (u == v) return u;
        for (int k = K - 1; k >= 0; k--) {
            if (parent[k][u] != parent[k][v]) {
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];
    }
    int length(int u, int v) { return dist[u] + dist[v] - 2 * dist[query(u, v)]; }
    bool is_in(int u, int v, int a) { return length(u, a) + length(a, v) == length(u, v); }
    int ancestor(int v,int k){//vからk個根側の頂点番号,存在しないなら-1
        int K = parent.size();
        for(int i=K-1;i>=0;i--){
            if(k>>i&1)v=parent[i][v];
            if(v==-1)return -1;
        }
        return v;
    }
};

int main(){
    ll n;cin >> n;
    vl a(n);rep(i,n)cin >> a[i];
    Graph g(n);
    rep(i,n-1){
        ll a,b;cin >> a >> b;a--;b--;
        g[a].push_back({b});
        g[b].push_back({a});
    }
    LCA lc(g);
    vl dp(n);
    auto dfs=[&](auto &self,ll v,ll par,ll ppar)->void{
        if(par!=-1){
            dp[v]=(isKad(a[v],a[par],a[ppar])? 1:0);
            dp[v]+=dp[par];
        }
        //ll opt=0;
        for(auto f:g[v]){
            if(f.to==par)continue;
            self(self,f.to,v,par);
            //opt++;
        }
        //if(opt==0)dp[v]=dp[par];
    };
    dfs(dfs,0,-1,-1);
    //rep(i,n)cout << dp[i] << endl;
    ll q;cin >> q;
    while(q--){
        ll u,v;cin >> u >> v;u--;v--;
        ll l=lc.query(u,v);
        if(u==l||v==l){
            //cout << "ERROR" << endl;continue;
            if(lc.length(u,v)==1){
                cout <<"NO" << endl;continue;
            }
            if(u!=l)swap(u,v);
            ll s=lc.ancestor(v,lc.length(u,v)-1);
            ll t=lc.ancestor(v,1);
            if(dp[v]-dp[s]!=lc.length(u,v)-1){
                cout << "NO" << endl;continue;
            }
            if(!isKad(a[s],a[u],a[v])){
                cout << "NO" << endl;continue;
            }
            if(!isKad(a[u],a[v],a[t])){
                cout << "NO" << endl;continue;
            }
            cout << "YES" << endl;
        }
        else{
            ll s=lc.ancestor(u,lc.length(u,l)-1);
            //cout << lc.length(u,l) << endl;
            ll t=lc.ancestor(v,lc.length(v,l)-1);
            //cout << lc.level_ancestor(u,0) << endl;
            //cout << s << " " << u << endl;
            ll f=dp[u]-dp[s];
            if(f!=lc.length(l,u)-1){
                //cout << f << endl;
                cout << "NO" << endl;continue;
            }
            f=dp[v]-dp[t];
            if(f!=lc.length(l,v)-1){
                cout << "NO" << endl;continue;
            }
            if(!isKad(a[s],a[l],a[t])){
                cout << "NO" << endl;continue;
            }
            s=lc.ancestor(u,1);
            t=lc.ancestor(v,1);
            if(!isKad(a[s],a[u],a[v])){
                cout << "NO" << endl;continue;
            }
            if(!isKad(a[u],a[v],a[t])){
                cout << "NO" << endl;continue;
            }
            cout << "YES" << endl;
        }
    }
}
0