結果

問題 No.363 門松サイクル
ユーザー toma
提出日時 2019-10-11 03:22:23
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,477 bytes
コンパイル時間 1,867 ms
コンパイル使用メモリ 178,584 KB
実行使用メモリ 10,924 KB
最終ジャッジ日時 2024-11-22 12:16:55
合計ジャッジ時間 10,189 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 8 WA * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;
using pii = pair<int, int>;

bool is_kadomatsu(int a, int b, int c) {
    return a != b && b != c && c != a && (a < b != b < c);
}

int main()
{
    int N, Q;
    cin >> N;
    vector<int> A(N);
    vector<vector<int>> edges(N);
    rep(i, N)cin >> A[i];
    rep(i, N - 1) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        edges[x].push_back(y);
        edges[y].push_back(x);
    }

    vector<int> depth(N), par(N), lim(N);
    {
        stack<pii> st;
        st.push({ -1,0 });
        while (!st.empty()) {
            int p, n;
            tie(p, n) = st.top(); st.pop();
            depth[n] = p == -1 ? 0 : depth[p] + 1;
            par[n] = p;
            lim[n] = p == -1 ? n
                : par[p] == -1 ? p
                : is_kadomatsu(A[n], A[p], A[par[p]]) ? lim[p] : p;
            for (int next : edges[n])if (next != p)st.push({ n,next });
        }
    }

    cin >> Q;
    while (Q--) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        if (depth[u] < depth[v])swap(u, v);

        bool res = true;
        res &= (depth[u] + depth[v]) % 2 == 1;
        res &= lim[u] == lim[v];
        res &= is_kadomatsu(A[par[u]], A[u], A[v]);
        res &= (par[v] == -1 || is_kadomatsu(A[u], A[v], A[par[v]]));
        cout << (res ? "YES" : "NO") << endl;
    }
    return 0;
}
0