結果
問題 | No.363 門松サイクル |
ユーザー | toma |
提出日時 | 2019-10-11 03:33:02 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,894 bytes |
コンパイル時間 | 2,190 ms |
コンパイル使用メモリ | 187,924 KB |
実行使用メモリ | 20,224 KB |
最終ジャッジ日時 | 2024-05-02 02:47:03 |
合計ジャッジ時間 | 10,309 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | AC | 295 ms
16,640 KB |
testcase_15 | AC | 262 ms
18,560 KB |
testcase_16 | AC | 221 ms
13,056 KB |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | AC | 344 ms
20,224 KB |
testcase_20 | AC | 383 ms
19,428 KB |
testcase_21 | WA | - |
testcase_22 | AC | 370 ms
18,900 KB |
testcase_23 | WA | - |
testcase_24 | AC | 2 ms
6,944 KB |
testcase_25 | AC | 2 ms
6,940 KB |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
ソースコード
#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>; using Graph = vector<vector<int>>; struct HLDecomposition { using pii = pair<int, int>; int n; Graph G; vector<int> vid, inv, par, depth, subsize, head, prev, next, type; HLDecomposition(const Graph& G_) : n(G_.size()), G(G_), vid(n, -1), inv(n), par(n), depth(n), subsize(n, 1), head(n), prev(n, -1), next(n, -1), type(n) {} void build(vector<int> roots = { 0 }) { int curtype = 0, pos = 0; for (int root : roots) { decide_heavy_edge(root); reconstruct(root, curtype++, pos); } } void decide_heavy_edge(int root) { stack<pii> st; par[root] = -1, depth[root] = 0; st.emplace(root, 0); while (!st.empty()) { int now = st.top().first; int& way = st.top().second; if (way < G[now].size()) { int child = G[now][way++]; if (child == par[now])continue; par[child] = now; depth[child] = depth[now] + 1; st.emplace(child, 0); } else { st.pop(); int maxsize = 0; for (auto child : G[now]) { if (child == par[now])continue; subsize[now] += subsize[child]; if (maxsize < subsize[child]) { maxsize = subsize[child]; prev[child] = now; next[now] = child; } } } } } void reconstruct(int root, int curtype, int& pos) { stack<int> st({ root }); while (!st.empty()) { int start = st.top(); st.pop(); for (int v = start; v != -1; v = next[v]) { type[v] = curtype; vid[v] = pos++; inv[vid[v]] = v; head[v] = start; for (auto child : G[v]) { if (child != par[v] && child != next[v]) { st.push(child); } } } } } int lca(int u, int v) { while (true) { if (vid[u] > vid[v])swap(u, v); if (head[u] == head[v])return u; v = par[head[v]]; } } }; 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); } HLDecomposition hld(edges); hld.build(); 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 &= is_kadomatsu(A[par[u]], A[u], A[v]); res &= par[v] == -1 || is_kadomatsu(A[u], A[v], A[par[v]]); res &= max(depth[lim[u]], depth[lim[v]]) <= depth[hld.lca(u, v)]; cout << (res ? "YES" : "NO") << endl; } return 0; }