Submission #73903788
Source Code Expand
#include <iostream>
#include <vector>
#include <unordered_set>
#include <stack>
using namespace std;
struct StackFrame {
int node;
int parent;
bool visited;
bool has_dup;
bool inserted;
StackFrame(int n, int p, bool v, bool hd, bool ins)
: node(n), parent(p), visited(v), has_dup(hd), inserted(ins) {}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<int> A(N + 1);
for (int i = 1; i <= N; ++i) {
cin >> A[i];
}
vector<vector<int>> adj(N + 1);
for (int i = 0; i < N - 1; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<string> ans(N + 1);
unordered_set<int> path_nums;
stack<StackFrame> st;
st.emplace(1, -1, false, false, false);
while (!st.empty()) {
auto frame = st.top();
st.pop();
int u = frame.node;
int parent = frame.parent;
bool visited = frame.visited;
bool has_dup = frame.has_dup;
bool inserted = frame.inserted;
if (!visited) {
if (has_dup) {
ans[u] = "Yes";
st.emplace(u, parent, true, has_dup, false);
} else {
if (path_nums.count(A[u])) {
ans[u] = "Yes";
st.emplace(u, parent, true, true, false);
} else {
ans[u] = "No";
path_nums.insert(A[u]);
st.emplace(u, parent, true, false, true);
}
}
for (auto it = adj[u].rbegin(); it != adj[u].rend(); ++it) {
int v = *it;
if (v != parent) {
bool new_has_dup = (ans[u] == "Yes");
st.emplace(v, u, false, new_has_dup, false);
}
}
} else {
if (inserted) {
path_nums.erase(A[u]);
}
}
}
for (int k = 1; k <= N; ++k) {
cout << ans[k] << '\n';
}
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | D - Integer-duplicated Path |
| User | xuhanjin |
| Language | C++23 (GCC 15.2.0) |
| Score | 400 |
| Code Size | 2160 Byte |
| Status | AC |
| Exec Time | 113 ms |
| Memory | 35768 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 400 / 400 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | sample_01.txt, sample_02.txt, sample_03.txt |
| All | hack_01.txt, hack_02.txt, hack_03.txt, hack_04.txt, hack_05.txt, hack_06.txt, sample_01.txt, sample_02.txt, sample_03.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt, test_37.txt, test_38.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| hack_01.txt | AC | 89 ms | 31424 KiB |
| hack_02.txt | AC | 80 ms | 31276 KiB |
| hack_03.txt | AC | 54 ms | 27364 KiB |
| hack_04.txt | AC | 53 ms | 27308 KiB |
| hack_05.txt | AC | 68 ms | 29132 KiB |
| hack_06.txt | AC | 67 ms | 29244 KiB |
| sample_01.txt | AC | 2 ms | 6356 KiB |
| sample_02.txt | AC | 2 ms | 6484 KiB |
| sample_03.txt | AC | 2 ms | 6484 KiB |
| test_01.txt | AC | 2 ms | 6564 KiB |
| test_02.txt | AC | 2 ms | 6480 KiB |
| test_03.txt | AC | 113 ms | 35768 KiB |
| test_04.txt | AC | 105 ms | 35768 KiB |
| test_05.txt | AC | 86 ms | 31028 KiB |
| test_06.txt | AC | 71 ms | 26680 KiB |
| test_07.txt | AC | 70 ms | 26692 KiB |
| test_08.txt | AC | 69 ms | 26612 KiB |
| test_09.txt | AC | 56 ms | 27408 KiB |
| test_10.txt | AC | 55 ms | 27348 KiB |
| test_11.txt | AC | 54 ms | 27492 KiB |
| test_12.txt | AC | 54 ms | 27300 KiB |
| test_13.txt | AC | 54 ms | 27504 KiB |
| test_14.txt | AC | 52 ms | 27332 KiB |
| test_15.txt | AC | 75 ms | 24376 KiB |
| test_16.txt | AC | 75 ms | 24180 KiB |
| test_17.txt | AC | 75 ms | 24180 KiB |
| test_18.txt | AC | 75 ms | 24188 KiB |
| test_19.txt | AC | 66 ms | 24192 KiB |
| test_20.txt | AC | 67 ms | 24124 KiB |
| test_21.txt | AC | 68 ms | 25572 KiB |
| test_22.txt | AC | 67 ms | 24632 KiB |
| test_23.txt | AC | 62 ms | 25244 KiB |
| test_24.txt | AC | 65 ms | 25372 KiB |
| test_25.txt | AC | 62 ms | 24732 KiB |
| test_26.txt | AC | 62 ms | 25588 KiB |
| test_27.txt | AC | 98 ms | 32412 KiB |
| test_28.txt | AC | 85 ms | 28040 KiB |
| test_29.txt | AC | 80 ms | 29000 KiB |
| test_30.txt | AC | 70 ms | 26384 KiB |
| test_31.txt | AC | 70 ms | 26040 KiB |
| test_32.txt | AC | 69 ms | 26208 KiB |
| test_33.txt | AC | 88 ms | 28668 KiB |
| test_34.txt | AC | 91 ms | 31420 KiB |
| test_35.txt | AC | 78 ms | 28056 KiB |
| test_36.txt | AC | 69 ms | 25156 KiB |
| test_37.txt | AC | 70 ms | 26564 KiB |
| test_38.txt | AC | 69 ms | 25412 KiB |