text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; int n; vector<vector<int>> adj; vector<vector<int>> dp; vector<int> parells; vector<bool> erased; int calc_par(int a, int ib) { if (dp[a][ib] != -1) return dp[a][ib]; int sum = 1; int ia; int b = adj[a][ib]; for (int i = 0; i < adj[b].size(); ++i) { if (adj[b][i] == a) { ia = i; continue; } sum += calc_par(b, i); } int ret = sum % 2; dp[b][ia] = 1 - ret; dp[a][ib] = ret; return ret; } int main() { cin >> n; adj = vector<vector<int>>(n, vector<int>()); dp = vector<vector<int>>(n, vector<int>()); erased = vector<bool>(n, false); parells = vector<int>(n, 0); for (int i = 0; i < n; ++i) { int a; cin >> a; if (a == 0) continue; a--; adj[i].push_back(a); adj[a].push_back(i); dp[i].push_back(-1); dp[a].push_back(-1); } if (n % 2 == 0) cout << "NO" << endl; else { cout << "YES" << endl; set<pair<int, int>> S; for (int i = 0; i < n; ++i) { int p = 0; for (int j = 0; j < adj[i].size(); ++j) { p += 1 - calc_par(i, j); } S.insert(pair<int, int>(p, i)); parells[i] = p; } for (int ii = 0; ii < n; ++ii) { auto v = S.begin()->second; S.erase(S.begin()); printf("%d\n", v + 1); for (int i = 0; i < adj[v].size(); ++i) { int v2 = adj[v][i]; if (erased[v2]) continue; S.erase(pair<int, int>(parells[v2], v2)); parells[v2]--; S.insert(pair<int, int>(parells[v2], v2)); } erased[v] = true; } } }
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 7; inline int R() { int rt = 0; char ch = getchar(); bool isn = false; for (; ch < '0' || ch > '9'; ch = getchar()) isn = ch == '-' ? true : isn; for (; ch >= '0' && ch <= '9'; ch = getchar()) rt = rt * 10 + ch - '0'; return isn ? -rt : rt; } vector<int> e[MAXN]; int sz[MAXN]; vector<int> ans; bool u[MAXN]; void Pre(int nf, int nfa) { sz[nf] = 1; for (vector<int>::iterator ne = e[nf].begin(); ne != e[nf].end(); ++ne) { if (*ne == nfa) continue; Pre(*ne, nf); sz[nf] += sz[*ne]; } } void Dfs(int nf, int nfa) { for (vector<int>::iterator ne = e[nf].begin(); ne != e[nf].end(); ++ne) { if (*ne == nfa) continue; if (!(sz[*ne] & 1)) Dfs(*ne, nf); } u[nf] = true; ans.push_back(nf); for (vector<int>::iterator ne = e[nf].begin(); ne != e[nf].end(); ++ne) { if (*ne == nfa) continue; if (!u[*ne]) Dfs(*ne, nf); } } int main() { int n = R(); if (!(n & 1)) { puts("NO"); return 0; } for (int i = 1; i <= n; ++i) { int nv = R(); if (nv) e[nv].push_back(i), e[i].push_back(nv); } Pre(1, 0); Dfs(1, 0); puts("YES"); for (vector<int>::iterator nans = ans.begin(); nans != ans.end(); ++nans) printf("%d\n", *nans); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 20; int n; struct edge { int y, nxt; } e[N << 1]; int lin[N], len = 1; int cnt[N], ans[N], pos; bool mark[N]; inline int read() { int s = 0, f = 1; char ch; for (; ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') f = -1; for (; ch >= '0' && ch <= '9'; ch = getchar()) s = (s << 1) + (s << 3) + ch - '0'; return s * f; } inline void add(int xx, int yy) { e[++len] = (edge){yy, lin[xx]}; lin[xx] = len; } inline void dfs(int x, int fa, bool flag) { if (!flag) { for (int i = lin[x]; i; i = e[i].nxt) { int y = e[i].y; if (y == fa) continue; if (!mark[y]) dfs(y, x, 0); } } if (!(cnt[x] % 2)) { cnt[x] = 0; ans[++pos] = x; mark[x] = 1; for (int i = lin[x]; i; i = e[i].nxt) { int y = e[i].y; --cnt[y]; if (y == fa) continue; if (!mark[y]) dfs(y, x, 1); } } } int main() { n = read(); for (int i = 1; i <= n; ++i) { int x = read(); if (x) { add(i, x); add(x, i); cnt[i]++; cnt[x]++; } } dfs(1, 0, 0); if (pos == n) { puts("YES"); for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]); } else puts("NO"); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 2; vector<int> t[N]; int fa[N], sz[N]; void dfs1(int u) { sz[u] = 1; for (auto v : t[u]) { dfs1(v); sz[u] += sz[v]; } } void dfs(int u) { for (auto v : t[u]) if (sz[v] % 2 == 0) dfs(v); printf("%d\n", u); for (auto v : t[u]) if (sz[v] & 1) dfs(v); } int main() { int n, root = -1; scanf("%d", &n); if (n % 2 == 0) return puts("NO"), 0; puts("YES"); for (int i = 1; i <= n; ++i) { scanf("%d", &fa[i]); if (fa[i]) t[fa[i]].push_back(i); else root = i; } dfs1(root); dfs(root); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using db = double; using ld = long double; template <typename T> using V = vector<T>; template <typename T> using VV = vector<vector<T>>; constexpr ll mod = 1000000007; constexpr ll INF = 1000000099; constexpr ll LINF = (ll)(1e18 + 99); const vector<ll> dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1}; template <typename T, typename U> inline bool chmin(T& t, const U& u) { if (t > u) { t = u; return 1; } return 0; } template <typename T, typename U> inline bool chmax(T& t, const U& u) { if (t < u) { t = u; return 1; } return 0; } template <typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <typename T, typename Y> inline T mpow(T a, Y n) { T res = 1; for (; n; n >>= 1) { if (n & 1) res = res * a; a = a * a; } return res; } template <typename T, typename Y> ostream& operator<<(ostream& os, const pair<T, Y>& p) { return os << "{" << p.first << "," << p.second << "}"; } template <typename T> ostream& operator<<(ostream& os, const V<T>& v) { os << "{"; for (auto e : v) os << e << ","; return os << "}"; } template <typename... Args> void debug(Args&... args) { for (auto const& x : {args...}) { cerr << x << ' '; } cerr << '\n'; } VV<int> e; V<int> ans1(0), ans2(0), deg; bool dfs(int now, int pre) { bool res = false; for (auto&& to : e[now]) { if (to == pre) continue; res |= dfs(to, now); } if (deg[now] % 2) { ans2.push_back(now); return true; } else { ans1.push_back(now); for (auto&& to : e[now]) { deg[to]--; } return false; } } signed main() { cin.tie(0); cerr.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); int n; cin >> n; e.assign(n, V<int>(0)); deg.assign(n, 0); for (ll i = 0; i < (ll)(n); ++i) { int x; cin >> x; --x; if (x < 0) continue; e[i].push_back(x); e[x].push_back(i); deg[x]++; deg[i]++; } if (!dfs(0, -1)) { cout << "YES" << '\n'; reverse((ans2).begin(), (ans2).end()); for (ll i = 0; i < (ll)((ll)(ans1).size()); ++i) { cout << ans1[i] + 1 << '\n'; } for (ll i = 0; i < (ll)((ll)(ans2).size()); ++i) { cout << ans2[i] + 1 << '\n'; } } else { cout << "NO" << '\n'; } }
#include <bits/stdc++.h> using namespace std; vector<int> graf[200100]; pair<int, pair<int, int>> tree[1 << 19]; pair<int, pair<int, int>> st[200100]; int depth = 1 << 18, n; void dfs() { int str = 1; st[0] = make_pair(1, make_pair(0, 1)); while (str > 0) { if (st[str - 1].second.first < graf[st[str - 1].first].size()) { ++st[str - 1].second.first; if (str < 2 || st[str - 2].first != graf[st[str - 1].first][st[str - 1].second.first - 1]) { st[str] = make_pair(graf[st[str - 1].first][st[str - 1].second.first - 1], make_pair(0, 1)); ++str; } } else { if (str == 1) return; st[str - 2].second.second += st[str - 1].second.second; if (st[str - 1].second.second % 2 == 0) { if (str >= 2) ++tree[st[str - 2].first + depth].first; } else { ++tree[st[str - 1].first + depth].first; } --str; } } } void rem(int del) { int i; for (int g = 0; g < graf[del].size(); ++g) { i = graf[del][g] + depth; --tree[i].first; while (i > 1) { i >>= 1; if (tree[i * 2].second.first == 0) tree[i] = tree[2 * i + 1]; else if (tree[i * 2 + 1].second.first == 0) tree[i] = tree[2 * i]; else { if (tree[2 * i].first == 0) tree[i] = tree[2 * i]; else tree[i] = tree[2 * i + 1]; } } } i = del + depth; tree[del + depth] = make_pair(0, make_pair(0, 0)); while (i > 1) { i >>= 1; if (tree[i * 2].second.first == 0) tree[i] = tree[2 * i + 1]; else if (tree[i * 2 + 1].second.first == 0) tree[i] = tree[2 * i]; else { if (tree[2 * i].first == 0) tree[i] = tree[2 * i]; else tree[i] = tree[2 * i + 1]; } } } int main() { cin >> n; if (n % 2 == 0) { cout << "NO"; return 0; } for (int i = 1; i <= n; ++i) { int i1; cin >> i1; if (i1 != 0) { graf[i].push_back(i1); graf[i1].push_back(i); } } cout << "YES\n"; dfs(); for (int i = 0; i <= n; ++i) { tree[depth + i].second.first = i; } for (int i = depth - 1; i >= 1; --i) { if (tree[i * 2].second.first == 0) tree[i] = tree[2 * i + 1]; else if (tree[i * 2 + 1].second.first == 0) tree[i] = tree[2 * i]; else { if (tree[2 * i].first == 0) tree[i] = tree[2 * i]; else tree[i] = tree[2 * i + 1]; } } for (int i = 0; i < n; ++i) { cout << tree[1].second.first << "\n"; rem(tree[1].second.first); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX = 2e5; int n; list<int> children[MAX]; vector<int> solution; int degree[MAX]; int root; void Init() { for (int i = 0; i < n; i++) { degree[i] = 0; } } void dfsd(int node) { solution.push_back(node); for (auto it = children[node].begin(); it != children[node].end();) { int child = *it; dfsd(child); it = children[node].erase(it); } } bool dfs(int node) { bool res = false; for (auto it = children[node].begin(); it != children[node].end();) { int child = *it; bool tmpres = dfs(child); if (tmpres) { it++; continue; } degree[node]--; it = children[node].erase(it); } if (degree[node] % 2 == 0) { dfsd(node); } else { res = true; } return res; } void Solve() { if (dfs(root)) { printf("NO"); } else { printf("YES\n"); for (int node : solution) { printf("%d\n", node + 1); } } } int main() { scanf("%d", &n); Init(); for (int i = 0; i < n; i++) { int parent; scanf("%d", &parent); parent--; if (parent < 0) { root = i; } else { degree[i]++; degree[parent]++; children[parent].push_back(i); } } Solve(); return 0; }
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1, ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } inline void write(int x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) write(x / 10); putchar(x % 10 + '0'); } inline void writeln(int x) { write(x); puts(""); } const int N = 2e5 + 5; struct edge { int link, next; } e[N << 1]; int n, head[N], tot, du[N]; inline void add_edge(int u, int v) { e[++tot] = (edge){v, head[u]}; head[u] = tot; } inline void insert(int u, int v) { add_edge(u, v); add_edge(v, u); } inline void init() { scanf("%d", &n); for (int i = 1; i <= n; i++) { int u = read(); if (u) insert(u, i), du[i]++, du[u]++; } } bool mark[N]; int ans[N], cnt; void dfs(int u, int fa, bool flag) { if (!flag) { for (int i = head[u]; i; i = e[i].next) { int v = e[i].link; if (v != fa && !mark[v]) { dfs(v, u, 0); } } } if (du[u] % 2 == 0) { du[u] = 0; ans[++cnt] = u; mark[u] = 1; for (int i = head[u]; i; i = e[i].next) { int v = e[i].link; --du[v]; if (v != fa && !mark[v]) { dfs(v, u, 1); } } } } inline void solve() { dfs(1, 0, 0); if (cnt == n) { puts("YES"); for (int i = 1; i <= n; i++) writeln(ans[i]); } else { puts("NO"); } } int main() { init(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int read() { int a = 0, b = 1, c; while (((c = fgetc(stdin)) > '9' || c < '0') && c != '-') ; if (c == '-') b = -1, c = fgetc(stdin); while (c >= '0' && c <= '9') a = a * 10 + c - '0', c = fgetc(stdin); return a * b; } set<pair<int, int> > even; vector<int> g[200005], ans; int n, par[200005], d[200005], dep[200005], del[200005]; void dfs(int u) { dep[u] = dep[par[u]] + 1; for (int i = 0; i < ((int)(g[u]).size()); i++) { int v = g[u][i]; if (v == par[u]) continue; dfs(v); } } int main() { n = read(); for (int i = 1; i <= n; i++) { par[i] = read(); if (!par[i]) continue; d[par[i]]++; d[i]++; g[par[i]].push_back(i); g[i].push_back(par[i]); } for (int i = 1; i <= n; i++) if (!par[i]) dfs(i); for (int i = 1; i <= n; i++) if (!(d[i] & 1)) even.insert(make_pair(dep[i], i)); while (!even.empty()) { int u = even.rbegin()->second; even.erase(make_pair(dep[u], u)); del[u] = 1; ans.push_back(u); for (int i = 0; i < ((int)(g[u]).size()); i++) { int v = g[u][i]; if (del[v]) continue; if (!even.count(make_pair(dep[v], v))) even.insert(make_pair(dep[v], v)); else even.erase(make_pair(dep[v], v)); } } if (((int)(ans).size()) != n) { printf("NO\n"); } else { printf("YES\n"); for (int i = 0; i < n; i++) { printf("%d\n", ans[i]); } } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } const int N = 200005; int tree[N]; vector<int> con[N]; int calc_parity(int s = 1, int p = 0) { tree[s] = 1; for (int v : con[s]) if (v != p) tree[s] ^= calc_parity(v, s); return tree[s]; } void dfs(int s = 1, int p = 0) { for (int v : con[s]) if (v != p && tree[v] == 0) dfs(v, s); printf("%d\n", s); for (int v : con[s]) if (v != p && tree[v] == 1) dfs(v, s); } int main() { int n; scanf("%d", &(n)); for (int i = 1; i <= n; i++) { int p; scanf("%d", &(p)); if (p) { con[i].push_back(p); con[p].push_back(i); } } if (n % 2 == 0) { printf("NO\n"); return 0; } printf("YES\n"); calc_parity(); dfs(); }
#include <bits/stdc++.h> using namespace std; const int maxn = 200050; struct Edge { int v, nxt; } e[maxn * 2]; int h[maxn], tot; void addEdge(int x, int y) { e[++tot] = (Edge){y, h[x]}; h[x] = tot; } int n, deg[maxn]; bool vis[maxn]; int ans[maxn], c; void del(int x, int x_fa) { if (vis[x]) return; vis[x] = true; ans[c++] = x; for (int i = h[x]; i; i = e[i].nxt) { deg[e[i].v]--; } for (int i = h[x]; i; i = e[i].nxt) { if (e[i].v != x_fa && !vis[e[i].v]) { del(e[i].v, x); } } } void dfs(int x, int x_fa) { for (int i = h[x]; i; i = e[i].nxt) { if (e[i].v == x_fa) continue; dfs(e[i].v, x); } if (deg[x] % 2 == 0) del(x, x_fa); } int main(int argc, char const *argv[]) { scanf("%d", &n); int root = 0; for (int i = 1; i <= n; ++i) { int x; scanf("%d", &x); if (x) { addEdge(x, i); addEdge(i, x); deg[x]++; deg[i]++; } else root = i; } dfs(root, 0); if (c < n) { printf("NO\n"); return 0; } printf("YES\n"); for (int i = 0; i < n; ++i) { printf("%d\n", ans[i]); } return 0; }
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); double fRand(double fMin, double fMax) { double f = (double)rand() / RAND_MAX; return fMin + f * (fMax - fMin); } template <class T> T min(T a, T b, T c) { return min(a, min(b, c)); } template <class T> T max(T a, T b, T c) { return max(a, max(b, c)); } int n, f[200005], d[200005], deg[200005], par[200005]; bool ok[200005], avail[200005]; vector<int> g[200005]; set<pair<int, int> > s; void prep(int u) { if (deg[u] % 2 == 0) s.insert(make_pair(d[u], u)); for (int v : g[u]) { if (v == par[u]) continue; par[v] = u; d[v] = d[u] + 1; prep(v); } } void DFS(int u) { printf("%d\n", u); avail[u] = false; s.erase(make_pair(d[u], u)); for (int v : g[u]) { --deg[v]; if (avail[v]) { if (deg[v] % 2 == 0) s.insert(make_pair(d[v], v)); else s.erase(make_pair(d[v], v)); } } for (int v : g[u]) { if (!avail[v] || v == par[u]) continue; DFS(v); } } int main() { scanf("%d", &n); if (n % 2 == 0) { puts("NO"); return 0; } for (int u = (1); u <= (n); ++u) { int p; scanf("%d", &p); if (p != 0) { g[p].push_back(u); g[u].push_back(p); } } for (int u = (1); u <= (n); ++u) deg[u] = (int)g[u].size(); prep(1); puts("YES"); memset(avail, true, sizeof avail); while (!s.empty()) { int u = s.rbegin()->second; DFS(u); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; vector<int> adj[N]; int ans[N], a[N], idx[N]; int dfs(int u, int p) { int cnt = 0; for (int v : adj[u]) if (v != p) { cnt += dfs(v, u); } return ans[u] = (int(adj[u].size()) - cnt) % 2 == 0; } void DFS(int u, int p) { if (ans[u]) a[u] = a[p] - 1; else a[u] = a[p] + 1; for (int v : adj[u]) if (v != p) { DFS(v, u); } } int main() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; ++i) { int j; cin >> j; if (j) { adj[i].push_back(j); adj[j].push_back(i); } } int root = -1; for (int i = 1; i <= n; i++) { if (adj[i].size() != 1) root = i; } if (root == -1) { cout << "NO" << endl; return 0; } if (dfs(root, 0)) { cout << "YES" << endl; DFS(root, 0); vector<pair<int, int> > xx; for (int i = 1; i <= n; i++) xx.push_back({a[i], i}); sort(xx.begin(), xx.end()); for (int i = 1; i <= n; i++) cout << xx[i - 1].second << endl; } else cout << "NO" << endl; }
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 5; vector<long long> g[N]; bool vis[N]; long long child[N]; vector<long long> ans; void dfs(long long node) { vis[node] = true; child[node] = 1; for (long long x : g[node]) { if (!vis[x]) { dfs(x); child[node] += child[x]; } } } void dfs2(long long node) { vis[node] = true; for (long long x : g[node]) { if (!vis[x] && child[x] % 2 == 0) { dfs2(x); } } ans.push_back(node); for (long long x : g[node]) { if (!vis[x] && child[x] % 2 == 1) { dfs2(x); } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); set<long long> even, odd; long long n; cin >> n; for (long long i = 1; i <= n; i++) { long long p; cin >> p; if (p != 0) { g[p].push_back(i); g[i].push_back(p); } } if (n % 2 == 0) { cout << "NO\n"; return 0; } dfs(1); memset(vis, false, sizeof(vis)); dfs2(1); cout << "YES\n"; for (long long x : ans) cout << x << "\n"; }
#include <bits/stdc++.h> using namespace std; using ll = long long; template <typename... t> using V = vector<t...>; template <typename... t> using outit = ostream_iterator<t...>; void print(ostream& os) { os << '\n'; } template <typename t, typename... v> void print(ostream& os, const t& a, v&&... b) { os << a << ' '; print(os, b...); } int n; V<int> odp; V<int> ilo, poz; V<V<int> > gr; V<V<bool> > dp; bool dfs1(int v, int oj) { if (static_cast<int>((gr[v]).size()) == 1 and v != oj) return dp[1][v] = true; for (int s : gr[v]) if (s != oj) if (not dfs1(s, v)) return false; for (int s : gr[v]) if (s != oj) { if (dp[0][s] and dp[1][s]) ilo[v] = s; else if (dp[1][s]) ++poz[v]; } dp[0][v] = ((poz[v] & 1) == 1) or ilo[v] != -1; dp[1][v] = ((poz[v] & 1) == 0) or ilo[v] != -1; if ((not dp[0][v] and not dp[1][v]) or (v == oj and not dp[1][v])) return false; return true; } void dfs2(int v, int oj, bool fl = false) { bool zm = false; if (fl == false and (poz[v] & 1) == 1) zm = true; if (fl == true and (poz[v] & 1) == 0) zm = true; for (int s : gr[v]) if (s != oj) { if (zm and s == ilo[v]) continue; if (dp[0][s]) dfs2(s, v, false); } odp.push_back(v); for (int s : gr[v]) if (s != oj) { if (zm and s == ilo[v]) { dfs2(s, v, true); continue; } if (dp[0][s] == false and dp[1][s] == true) dfs2(s, v, true); } } int main(int argc, char** argv) { (void)(argc); (void)(argv); ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; gr.resize(n + 1); ilo.resize(n + 1, -1); poz.resize(n + 1); dp.resize(2, V<bool>(n + 1)); for (int i = 1; i <= n; ++i) { int p; cin >> p; if (p != 0) { gr[i].push_back(p); gr[p].push_back(i); } } if (not dfs1(1, 1)) return print(cout, "NO"), 0; ; dfs2(1, 1); print(cout, "YES"); copy(begin(odp), end(odp), outit<int>(cout, "\n")); }
#include <bits/stdc++.h> using namespace std; vector<int> adj[1000000], ans; int n, u, v; int vis[1000000], par[1000000]; void take(int cur) { ans.push_back(cur); vis[cur] = 1; for (int i = 0; i < adj[cur].size(); ++i) if (!vis[adj[cur][i]]) take(adj[cur][i]); } void dfs(int cur) { int deg = 0; for (int i = 0; i < adj[cur].size(); ++i) { dfs(adj[cur][i]); if (!vis[adj[cur][i]]) deg++; } if (par[cur]) deg++; if (!(deg & 1) && cur) take(cur); } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &par[i]); adj[par[i]].push_back(i); } dfs(0); if (ans.size() < n) printf("NO\n"); else { printf("YES\n"); for (int i = 0; i < ans.size(); ++i) printf("%d\n", ans[i]); } return 0; }
#include <bits/stdc++.h> using namespace std; int d[1000006], n, root; vector<int> e[1000006]; void dfs1(int u) { d[u] = 1; for (int i = 0; i < e[u].size(); i++) { int v = e[u][i]; dfs1(v); d[u] += d[v]; } return; } void dfs2(int u) { for (int i = 0; i < e[u].size(); i++) if (d[e[u][i]] % 2 == 0) dfs2(e[u][i]); cout << u << endl; for (int i = 0; i < e[u].size(); i++) if (d[e[u][i]] % 2) dfs2(e[u][i]); return; } int main() { ios_base::sync_with_stdio(false); cin >> n; if (n % 2 == 0) return cout << "NO", 0; cout << "YES" << endl; for (int i = 1; i <= n; i++) { int x; cin >> x; if (x == 0) root = i; else e[x].push_back(i); } dfs1(root); dfs2(root); return 0; }
#include <bits/stdc++.h> using namespace std; int to[3000010], last[3000010], nxt[3000010], tot = 0; int ru[3000010]; bool vis[3000010]; void add(int x, int y) { to[++tot] = y; nxt[tot] = last[x]; last[x] = tot; } int to2[3000010], last2[3000010], nxt2[3000010], tot2 = 0; void add2(int x, int y) { to2[++tot2] = y; nxt2[tot2] = last2[x]; last2[x] = tot2; ru[y]++; } int siz[3000010]; void dfs(int x, int pre) { siz[x] = 1; for (int j = last[x]; j; j = nxt[j]) { if (to[j] != pre && to[j] != 0) { dfs(to[j], x); siz[x] += siz[to[j]]; } } if (pre == 0) return; if (siz[x] % 2 == 0) add2(x, pre); else add2(pre, x); } void work(int x) { printf("%d\n", x); vis[x] = true; for (int j = last[x]; j; j = nxt[j]) { ru[to[j]]--; if (ru[to[j]] == 0 && vis[to[j]] == false) work(to[j]); } } int main(int argc, char const *argv[]) { memset(ru, 0, sizeof(ru)); memset(last, 0, sizeof(last)); memset(vis, false, sizeof(vis)); int n; scanf("%d", &n); if (n % 2 == 0) { printf("NO\n"); return 0; } else printf("YES\n"); int faa; for (int i = 1; i <= n; ++i) { int x; scanf("%d", &x); add(x, i); add(i, x); if (x == 0) faa = i; } dfs(faa, 0); for (int i = 1; i <= n; ++i) { if (ru[i] == 0 && vis[i] == false) work(i); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, ta, tb; vector<pair<int, int> > v; vector<int> vv[200005], vvv; stack<int> s; bool dfs(int a, int b) { bool deg = 0; for (int i = 0; i < vv[a].size(); i++) if (vv[a][i] == b) deg ^= 1; else deg ^= dfs(vv[a][i], a); if (deg) { s.push(a); return 1; } vvv.push_back(a); return 0; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { ta = i; scanf("%d", &tb); if (tb) { if (ta > tb) swap(ta, tb); v.push_back(make_pair(ta, tb)); } } sort(v.begin(), v.end()); for (int i = 0; i < v.size(); i++) if (!i || v[i] != v[i - 1]) { vv[v[i].first].push_back(v[i].second); vv[v[i].second].push_back(v[i].first); } if (dfs(1, 0)) printf("NO\n"); else { printf("YES\n"); for (int i = 0; i < vvv.size(); i++) printf("%d\n", vvv[i]); while (!s.empty()) { printf("%d\n", s.top()); s.pop(); } } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> ans; int previ[200005]; vector<int> tree[200005]; int cnt[200005]; void countN(int curr) { cnt[curr] = 1; for (int a : tree[curr]) { if (a != previ[curr]) { previ[a] = curr; countN(a); cnt[curr] += cnt[a]; } } } void recur(int curr) { for (int a : tree[curr]) { if (a != previ[curr]) { if (cnt[a] % 2 == 0) { recur(a); } } } ans.push_back(curr); for (int a : tree[curr]) { if (a != previ[curr]) { if (cnt[a] % 2 == 1) { recur(a); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 0; i < n; i++) { int temp; cin >> temp; temp--; if (temp != -1) { tree[i].push_back(temp); tree[temp].push_back(i); } } if (n % 2 == 1) { cout << "YES\n"; previ[0] = -1; countN(0); recur(0); for (int a : ans) { cout << a + 1 << "\n"; } } else { cout << "NO\n"; } }
#include <bits/stdc++.h> using namespace std; const int N = 200001; const long long MOD = 1e9 + 7; int n, root, sz[N]; vector<vector<int> > g; vector<int> ans; void dfs1(int u, int p) { sz[u] = 1; for (int i = 0; i < g[u].size(); i++) if (g[u][i] != p) { dfs1(g[u][i], u); sz[u] += sz[g[u][i]]; } } void dfs2(int u, int p) { for (int i = 0; i < g[u].size(); i++) if (g[u][i] != p && !(sz[g[u][i]] & 1)) dfs2(g[u][i], u); ans.push_back(u); for (int i = 0; i < g[u].size(); i++) if (g[u][i] != p && (sz[g[u][i]] & 1)) dfs2(g[u][i], u); } int main() { scanf("%d", &n); g.resize(n); for (int a = 0, b; a < n; a++) { scanf("%d", &b); b--; if (b != -1) { g[a].push_back(b); g[b].push_back(a); } else root = a; } if (n % 2 == 0) { puts("NO"); return 0; } dfs1(root, -1); dfs2(root, -1); puts("YES"); for (int i = 0; i < ans.size(); i++) printf("%d\n", ans[i] + 1); return 0; }
#include <bits/stdc++.h> const int maxn = 2e5 + 10; const int maxm = 2e5 + 10; const int mod = 1e9 + 7; const int inf = 0x3f3f3f3f; const double eps = 1e-7; using namespace std; vector<int> G[maxn]; int rt = 1; int n, p, sz[maxn]; void dfs(int u, int fa) { sz[u]++; for (auto v : G[u]) { if (v == fa) continue; dfs(v, u); sz[u] += sz[v]; } } void dfs1(int u, int fa) { for (auto v : G[u]) { if (v == fa) continue; if (sz[v] % 2 == 0) dfs1(v, u); } cout << u << endl; for (auto v : G[u]) { if (v == fa) continue; if (sz[v] & 1) dfs1(v, u); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> p; if (p == 0) rt = i; else { G[i].push_back(p); G[p].push_back(i); } } if (n & 1) { cout << "YES" << endl; dfs(rt, 0); dfs1(rt, 0); } else cout << "NO"; return ~~(0 - 0); }
#include <bits/stdc++.h> using namespace std; const int N = 200002; int dp[N]; vector<int> ans; vector<int> g[N]; void dfs(int s, int p) { for (auto u : g[s]) { if (u != p) { dfs(u, s); dp[s] += dp[u]; } } dp[s] = !(dp[s] % 2); } void dfs2(int s, int p) { for (auto u : g[s]) { if (u != p) { if (!dp[u]) dfs2(u, s); } } ans.push_back(s); for (auto u : g[s]) { if (u != p) { if (dp[u]) dfs2(u, s); } } } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 1; i <= n; i++) { int a; cin >> a; if (!a) continue; g[a].push_back(i); g[i].push_back(a); } dfs(1, 1); dfs2(1, 1); if (!dp[1]) cout << "NO"; else { cout << "YES" << endl; for (int i = 0; i < n; i++) cout << ans[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } const int N = 2e5 + 5; int n; vector<int> adj[N]; int root; int degree[N], removed[N], level[N]; void dfs(int u, int p) { for (auto v : adj[u]) { if (v == p) continue; level[v] = level[u] + 1; dfs(v, u); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; cin >> n; for (int i = 1; i <= n; i++) { int x; cin >> x; if (x == 0) { root = i; continue; } adj[x].push_back(i); adj[i].push_back(x); degree[i]++, degree[x]++; } if (n % 2 == 0) { cout << "NO\n"; return 0; } dfs(root, 0); set<pair<int, int> > s; for (int i = 1; i <= n; i++) { if (degree[i] % 2 == 0) s.insert({level[i], i}); } vector<int> ans; while (!s.empty()) { auto top = *s.rbegin(); s.erase(prev(s.end())); int node = top.second; for (auto v : adj[node]) { if (removed[v]) continue; degree[v]--; if (degree[v] % 2 == 1) { s.erase(s.find({level[v], v})); } else { s.insert({level[v], v}); } } ans.push_back(node); removed[node] = 1; } if (ans.size() == n) { cout << "YES\n"; for (auto i : ans) cout << i << "\n"; } else cout << "NO\n"; return 0; }
#include <bits/stdc++.h> using namespace std; struct Y { int h, d; inline bool operator<(const Y& a) const { return d < a.d; } } dep[200005]; int n, d[200005], rt, p[200005], que[200005], f[200005], cnt; vector<int> a[200005]; void dfs(int x, int fa) { dep[x].h = x, dep[x].d = dep[fa].d + 1, f[x] = fa; for (int i = 0; i < a[x].size(); ++i) if (a[x][i] != fa) dfs(a[x][i], x); } void dfs1(int x) { for (int i = 0; i < a[x].size(); ++i) if (p[a[x][i]] == 1 && a[x][i] != f[x]) dfs1(a[x][i]); que[++cnt] = x; for (int i = 0; i < a[x].size(); ++i) if (p[a[x][i]] == 0 && a[x][i] != f[x]) dfs1(a[x][i]); } int main(void) { register int i, x; scanf("%d", &n); for (i = 1; i <= n; ++i) { scanf("%d", &x); if (x != 0) ++d[i], ++d[x], a[i].push_back(x), a[x].push_back(i); else rt = i; } dfs(rt, 0); sort(dep + 1, dep + n + 1); for (i = n; i; --i) { if (d[dep[i].h] & 1) p[dep[i].h] = 0; else p[dep[i].h] = 1, --d[f[dep[i].h]]; ; if (dep[i].h == rt && p[dep[i].h] == 0) { puts("NO"); return 0; } } puts("YES"); dfs1(rt); for (i = 1; i <= n; ++i) printf("%d\n", que[i]); return 0; }
#include <bits/stdc++.h> using namespace std; template <class T, class S> ostream& operator<<(ostream& os, const pair<T, S>& v) { return os << "(" << v.first << ", " << v.second << ")"; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { os << "["; for (int i = int(0); i <= int((sz(v)) - 1); ++i) { if (i) os << ", "; os << v[i]; } return os << "]"; } int read_int() { int x; scanf("%d", &x); return x; } long long read_ll() { long long x; scanf("%lld", &x); return x; } string read_string() { string s; cin >> s; return s; } vector<int> read_vi(int n = -1) { if (n < 0) scanf("%d", &n); vector<int> a(n); for (int i = int(0); i <= int((n)-1); ++i) scanf("%d", &a[i]); return a; } vector<long long> read_vl(int n = -1) { if (n < 0) scanf("%d", &n); vector<long long> a(n); for (int i = int(0); i <= int((n)-1); ++i) scanf("%lld", &a[i]); return a; } vector<double> read_vd(int n = -1) { if (n < 0) scanf("%d", &n); vector<double> a(n); for (int i = int(0); i <= int((n)-1); ++i) scanf("%lf", &a[i]); return a; } vector<pair<int, int> > read_vpi(int n = -1) { if (n < 0) scanf("%d", &n); vector<pair<int, int> > a(n); for (int i = int(0); i <= int((n)-1); ++i) scanf("%d%d", &a[i].first, &a[i].second); return a; } vector<pair<long long, long long> > read_vpl(int n = -1) { if (n < 0) scanf("%d", &n); vector<pair<long long, long long> > a(n); for (int i = int(0); i <= int((n)-1); ++i) scanf("%lld%lld", &a[i].first, &a[i].second); return a; } vector<pair<double, double> > read_vpd(int n = -1) { if (n < 0) scanf("%d", &n); vector<pair<double, double> > a(n); for (int i = int(0); i <= int((n)-1); ++i) scanf("%lf%lf", &a[i].first, &a[i].second); return a; } template <class T> T sorted(T x) { sort(x.begin(), x.end()); return x; } template <class T> T reversed(T x) { reverse(x.begin(), x.end()); return x; } template <class T> bool setmax(T& _a, T _b) { if (_a < _b) { _a = _b; return true; } return false; } template <class T> bool setmin(T& _a, T _b) { if (_b < _a) { _a = _b; return true; } return false; } template <class T> T gcd(T _a, T _b) { return _b == 0 ? _a : gcd(_b, _a % _b); } inline long long powmod(long long a, long long b, long long m) { long long r = 1; for (; b > 0; b >>= 1, a = a * a % m) { if (b & 1) r = r * a % m; } return r; } int main() { const int n = read_int(); vector<vector<int> > es(n); for (int x = int(0); x <= int((n)-1); ++x) { int y = read_int() - 1; if (y >= 0) { es[x].push_back(y), es[y].push_back(x); } } vector<vector<bool> > f(n, vector<bool>(2, false)); vector<vector<vector<int> > > p0(n, vector<vector<int> >(2)); vector<vector<vector<int> > > p1(n, vector<vector<int> >(2)); const function<void(int, int)> dfs = [&](int x, int fa) { vector<int> ys; for (int y : es[x]) { if (y != fa) ys.push_back(y); } vector<vector<int> > g(static_cast<int>((ys).size()) + 1, vector<int>(2, -1)); g[0][0] = 0; for (int i = int(0); i <= int((static_cast<int>((ys).size())) - 1); ++i) { const int y = ys[i]; dfs(y, x); for (int j = int(0); j <= int((2) - 1); ++j) if (g[i][j] >= 0) { for (int k = int(0); k <= int((2) - 1); ++k) if (f[y][k]) g[i + 1][j ^ k] = k; } } for (int c = int(0); c <= int((2) - 1); ++c) if (g[static_cast<int>((ys).size())][c ^ 1] >= 0) { f[x][c] = true; for (int i = static_cast<int>((ys).size()) - 1, j = c ^ 1, k; i >= 0; j ^= k, --i) { k = g[i + 1][j]; assert(k >= 0); (k == 0 ? p0 : p1)[x][c].push_back(ys[i]); } } }; dfs(0, -1); if (!f[0][1]) { printf("NO\n"); } else { printf("YES\n"); const function<void(int, int)> answer = [&](int x, int c) { for (int y : p0[x][c]) answer(y, 0); printf("%d\n", x + 1); for (int y : p1[x][c]) answer(y, 1); }; answer(0, 1); } return 0; }
#include <bits/stdc++.h> using namespace ::std; const long long maxn = 2e5 + 500; const long long mod = 1e9 + 7; const long long inf = 1e9 + 500; vector<long long> ger[maxn]; bool hazf[maxn]; set<pair<long long, long long> > z, f; long long hell = 9999999; long long h[maxn]; void dfs(long long a, long long p = -1) { hell--; for (auto v : ger[a]) { hell--; if (v != p) { hell--; h[v] = h[a] + 1; hell--; dfs(v, a); hell--; } } } int main() { hell--; ios_base::sync_with_stdio(0); hell--; cin.tie(0); cout.tie(0); hell--; long long n; hell--; cin >> n; for (long long i = 0; i < n; i++) { hell--; long long v; cin >> v; hell--; if (v != 0) { v--; hell--; ger[v].push_back(i); ger[i].push_back(v); hell--; } } dfs(0); hell--; for (long long i = 0; i < n; i++) { hell--; if (ger[i].size() % 2 == 0) { hell--; z.insert(make_pair(-h[i], i)); hell--; } else { hell--; f.insert(make_pair(-h[i], i)); hell--; } } vector<long long> ans; hell--; long long hh = 0; hell--; while (z.size()) { hell--; pair<long long, long long> t = (*(z.begin())); hell--; long long v = t.second; hell--; hazf[v] = 1; hell--; z.erase(t); hell--; hh++; hell--; ans.push_back(v); hell--; vector<long long> tof, toz; for (auto d : ger[v]) { hell--; if (hazf[d] == 0) { hell--; if (z.find(make_pair(-h[d], d)) != z.end()) { hell--; z.erase(make_pair(-h[d], d)); hell--; tof.push_back(d); hell--; } else { hell--; f.erase(make_pair(-h[d], d)); hell--; toz.push_back(d); hell--; } } } for (auto u : toz) { hell--; z.insert(make_pair(-h[u], u)); hell--; } for (auto u : tof) { hell--; f.insert(make_pair(-h[u], u)); hell--; } } if (hh != n) { hell--; cout << "NO\n"; hell--; } else { hell--; cout << "YES\n"; hell--; for (auto v : ans) { hell--; cout << v + 1 << endl; hell--; } } }
#include <bits/stdc++.h> using namespace std; const int maxn = int(2e5) + 5; int dp[maxn][2], memoi[maxn][2]; vector<int> graph[maxn]; void dfs(int node) { for (auto it : graph[node]) dfs(it); for (int p = 0; p < 2; p++) { int cnt0 = 0, cnt1 = 0, cnt01 = 0; for (auto it : graph[node]) { if (dp[it][0] && dp[it][1]) cnt01++; else if (dp[it][0]) cnt0++; else if (dp[it][1]) cnt1++; else { dp[node][p] = 0; continue; } } for (int i = 0; i <= cnt01; i++) { int v = int(graph[node].size()) + 1 - (!p) - cnt1 - i; if (v % 2 == 0) dp[node][p] = 1, memoi[node][p] = i; } } } void dfs2(int node, int p) { vector<int> cnt0, cnt1, cnt01; for (auto it : graph[node]) { if (dp[it][0] && dp[it][1]) cnt01.push_back(it); else if (dp[it][0]) cnt0.push_back(it); else if (dp[it][1]) cnt1.push_back(it); } for (auto it : cnt1) dfs2(it, 1); for (int i = 0; i < memoi[node][p]; i++) dfs2(cnt01[i], 1); printf("%d\n", node + 1); for (auto it : cnt0) dfs2(it, 0); } int main(void) { int n, p, root = -1; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &p); p--; if (p == -1) root = i; else graph[p].push_back(i); } dfs(root); if (dp[root][0]) { printf("YES\n"); dfs2(root, 0); } else printf("NO\n"); }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 7; const int M = 100 + 7; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 9; vector<int> edge[N], e[N], ans; int n, cnt[N], deg[N]; int vis[N]; void dfs(int u, int pre) { cnt[u] = 1; for (int v : edge[u]) { if (v == pre) continue; dfs(v, u); cnt[u] += cnt[v]; } } bool dfs2(int u) { vis[u] = -1; for (int v : e[u]) { if (vis[v] == -1) return false; if (!vis[v] && !dfs2(v)) return false; } vis[u] = 1; ans.push_back(u); return true; } void dfs3(int u, int pre) { for (int v : edge[u]) { if (v == pre) continue; if (cnt[v] % 2) { e[u].push_back(v); deg[v]++; } else { e[v].push_back(u); deg[u]++; } dfs3(v, u); } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { int fa; scanf("%d", &fa); if (fa) { edge[fa].push_back(i); edge[i].push_back(fa); } } if (n % 2 == 0) { puts("NO"); return 0; } dfs(1, 0); dfs3(1, 0); for (int i = 1; i <= n; i++) { if (vis[i]) continue; if (!dfs2(i)) { puts("NO"); return 0; } } puts("YES"); for (int i = ans.size() - 1; i >= 0; i--) printf("%d\n", ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; int inf = pow(10, 9); long long int modulo = pow(10, 9) + 9; long double eps = 1e-15; ifstream in; ofstream out; vector<int> g[200100]; vector<int> taken; vector<int>* dsu[200100]; int depth[200100]; bool J(int& a, int& b) { return depth[a] < depth[b]; } void add(vector<int>*& a, vector<int>*& b) { if ((*b).size() > (*a).size()) { vector<int>* temp = a; a = b; b = temp; } for (int j = 0; j < (*b).size(); j++) (*a).push_back((*b)[j]); } bool dfs(int hd, int pr, int dep) { dsu[hd] = new vector<int>(); depth[hd] = dep; int nece = 0; for (long long int i = 0; i < g[hd].size(); i++) { int hr = g[hd][i]; if (hr == pr) continue; bool ans = dfs(hr, hd, dep + 1); nece += !ans; add(dsu[hd], dsu[hr]); } if (hd) { if (nece & 1) { sort((*dsu[hd]).begin(), (*dsu[hd]).end(), J); taken.push_back(hd); for (int j = 0; j < (*dsu[hd]).size(); j++) taken.push_back((*dsu[hd])[j]); (*dsu[hd]).clear(); return 1; } (*dsu[hd]).push_back(hd); return 0; } else { if (nece & 1) return 0; taken.push_back(hd); sort((*dsu[hd]).begin(), (*dsu[hd]).end(), J); for (int j = 0; j < (*dsu[hd]).size(); j++) taken.push_back((*dsu[hd])[j]); (*dsu[hd]).clear(); return 1; } } void deal() { int n; cin >> n; for (long long int i = 0; i < n; i++) { int pr; cin >> pr; --pr; if (pr > -1) g[pr].push_back(i), g[i].push_back(pr); } if (dfs(0, -1, 0)) { cout << "YES\n"; for (int j = 0; j < taken.size(); j++) cout << taken[j] + 1 << '\n'; } else cout << "NO\n"; } int main() { deal(); }
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse4") using namespace std; const double PI = acos(-1.0); const double EPS = 1e-6; const long long INF = 1e9; const long long LINF = 1e18; const long long mod = 1e9 + 9; const long long MAX = 2e5 + 47; int root; vector<int> g[MAX]; bool A[MAX], IS[MAX]; void dfs(int v, int par, bool d) { if (d) { cout << v << '\n'; IS[v] = false; for (int to : g[v]) if (to != par && IS[to]) dfs(to, v, 1); return; } for (int to : g[v]) { if (to != par && IS[to]) { dfs(to, v, d); A[v] ^= (A[to] ^ 1) && IS[to]; } } if (A[v] ^ (v == root)) { cout << v << '\n'; IS[v] = false; for (int to : g[v]) if (to != par && IS[to]) dfs(to, v, 1); } } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; if (!(n & 1)) { cout << "NO"; return 0; } for (int i = (1); i < (n + 1); ++i) { IS[i] = true; int p; cin >> p; if (p) { g[p].push_back(i); g[i].push_back(p); } else root = i; } cout << "YES\n"; dfs(root, -1, 0); return 0; }
#include <bits/stdc++.h> using namespace std; int n; basic_string<int> e[200005]; bool sa[200005], bez[200005]; void dfs1(int x, int p) { bool lose = false; int bravo = -1; int csa = 0, cbez = 0; for (int y : e[x]) { if (y != p) { dfs1(y, x); if (!sa[y] && !bez[y]) { lose = true; } else if (sa[y] && bez[y]) { bravo = y; } else if (sa[y]) { csa++; } else { cbez++; } } } if (lose) { sa[x] = bez[x] = false; return; } if (bravo == -1) { if (cbez % 2) { sa[x] = true; bez[x] = false; } else { sa[x] = false; bez[x] = true; } } else { sa[x] = bez[x] = true; } } void dfs2(int x, int p, bool gore) { basic_string<int> lsa, lbez, lbravo; for (int y : e[x]) { if (y != p) { if (!sa[y] && !bez[y]) { } else if (sa[y] && bez[y]) { lbravo += y; } else if (sa[y]) { lsa += y; } else { lbez += y; } } } for (int y : lsa) { dfs2(y, x, true); } while (gore ^ ((lbez.size() + lbravo.size()) % 2 == 1)) { dfs2(lbravo.back(), x, true); lbravo.pop_back(); } cout << x << '\n'; for (int y : lbravo) dfs2(y, x, false); for (int y : lbez) dfs2(y, x, false); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); cin >> n; for (int i = 1; i <= n; i++) { int x; cin >> x; if (x) { e[i] += x; e[x] += i; } } dfs1(1, 1); if (!bez[1]) { cout << "NO\n"; return 0; } cout << "YES\n"; dfs2(1, 1, false); }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; vector<int> G[N]; int vis[N], rt = 0; void print(int x) { vis[x] = 1; printf("%d\n", x); for (int i = 0; i < G[x].size(); i++) { int v = G[x][i]; if (!vis[v]) print(v); } } void DFS(int u, int fa) { int d = 0; for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if (!vis[v]) DFS(v, u); } for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if (!vis[v]) d++; } if (fa) d++; if (d % 2 == 0) { printf("%d\n", u); vis[u] = 1; for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if (!vis[v]) print(v); } } } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { int fa; scanf("%d", &fa); if (!fa) rt = i; else G[fa].push_back(i); } if ((n & 1) == 0) printf("NO\n"); else { printf("YES\n"); DFS(rt, 0); } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> g[(int)3e5 + 500]; int del[(int)3e5 + 500]; void dfs1(int u) { int cnt = 0; for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; dfs1(v); if (!del[v]) cnt++; } if (cnt % 2 == 1) { cout << u << endl; del[u] = 1; } } void dfs2(int u) { if (!del[u]) cout << u << endl; for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; dfs2(v); } } int main() { int n; while (~scanf("%d", &n)) { int rt, u; for (int i = 1; i <= n; i++) { g[i].clear(); del[i] = 0; } for (int i = 1; i <= n; i++) { scanf("%d", &u); if (u == 0) rt = i; else g[u].push_back(i); } if (n % 2 == 0) cout << "NO" << endl; else { cout << "YES" << endl; dfs1(rt); dfs2(rt); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int a[N], d[N]; vector<int> G[N]; vector<int> ans; int vis[N], sz[N]; void init(int fa, int u) { sz[u] = 1; for (int v : G[u]) { if (v != fa) { init(u, v); sz[u] += sz[v]; } } } void dfs(int fa, int u) { int flg = 0; for (int v : G[u]) { if (v != fa) { if (sz[v] % 2 == 0) { dfs(u, v); } } } ans.push_back(u); vis[u] = 1; for (int v : G[u]) { if (v != fa) { if (sz[v] % 2) { dfs(u, v); } } } } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); if (x) { G[i].push_back(x); G[x].push_back(i); d[x]++; d[i]++; } } if (n & 1) { init(-1, 1); dfs(0, 1); puts("YES"); for (int x : ans) { printf("%d\n", x); } for (int i = 1; i <= n; i++) { if (!vis[i]) { printf("%d\n", i); } } } else puts("NO"); return 0; }
#include <bits/stdc++.h> using namespace std; int n; vector<int> adj[200002]; int parent[200002]; int deg[200002]; vector<int> level[200002]; void dfs(int node, int p, int l) { parent[node] = p; level[l].push_back(node); for (long long i = 0; i < adj[node].size(); i++) if (adj[node][i] != p) dfs(adj[node][i], node, l + 1); } void solve() { cin >> n; for (long long i = 1; i < n + 1; i++) { int x; cin >> x; if (x != 0) { adj[i].push_back(x); adj[x].push_back(i); deg[i]++; deg[x]++; } } dfs(1, 0, 0); vector<int> ans; for (int i = 200001; i >= 0; i--) { if (level[i].size() == 0) continue; for (long long j = 0; j < level[i].size(); j++) { int node = level[i][j]; if (deg[node] % 2) { continue; } else { deg[parent[node]] -= 1; queue<int> Q; Q.push(node); while (!Q.empty()) { int fro = Q.front(); Q.pop(); ans.push_back(fro); deg[fro] = -1; for (long long k = 0; k < adj[fro].size(); k++) { if (adj[fro][k] != parent[fro]) { if (deg[adj[fro][k]] != -1) { Q.push(adj[fro][k]); } } } } } } } if (ans.size() != n) { cout << "NO" << endl; return; } cout << "YES" << endl; for (long long i = 0; i < n; i++) cout << ans[i] << endl; } int main() { std::ios::sync_with_stdio(false); ; ios_base::sync_with_stdio(false); cin.tie(NULL); ; int t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; vector<vector<int> > g; vector<vector<bool> > dp; vector<bool> en, used; vector<int> ans, ans1; int st; void dfs(int v) { int c = 0; for (int i = 0; i < g[v].size(); i++) { int to = g[v][i]; dfs(to); if (!used[to]) { c++; } } if (c % 2 == 1) { if (v == st) { cout << "NO"; exit(0); } used[v] = 1; ans.push_back(v); } else { ans1.push_back(v); } } signed main() { int n; cin >> n; used.resize(n); en.resize(n); dp.resize(n, vector<bool>(2)); g.resize(n); for (int i = 0; i < n; i++) { int p; cin >> p; p--; if (p == -1) { st = i; } else { g[p].push_back(i); } } dfs(st); cout << "YES\n"; for (int i = 0; i < ans.size(); i++) { cout << ans[i] + 1 << "\n"; } for (int i = ans1.size() - 1; i >= 0; i--) { cout << ans1[i] + 1 << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n; int rt, du[maxn], fa[maxn], vis[maxn]; vector<int> Map[maxn]; void del(int u) { cout << u << endl; for (auto v : Map[u]) if (!vis[v]) del(v); } void dfs(int dq) { for (auto x : Map[dq]) dfs(x); if (!(du[dq] & 1)) { vis[dq] = 1; du[fa[dq]]--; del(dq); return; } } void work() { if (n % 2 == 0) { cout << "NO" << endl; for (int i = 1; i <= n; i++) { int x; cin >> x; } return; } cout << "YES" << endl; for (int i = 1; i <= n; i++) { int x; cin >> x; if (x == 0) { rt = i; continue; } Map[x].push_back(i); fa[i] = x; ++du[x], ++du[i]; } dfs(rt); } int main() { while (cin >> n) work(); return 0; }
#include <bits/stdc++.h> using namespace std; long long mod = 1000000009; long long fastpow(int a, int b, int MOD) { long long x = 1, y = a; while (b > 0) { if (b % 2 == 1) { x = (x * y) % MOD; } y = (y * y) % MOD; b /= 2; } return x; } long long InverseEuler(int n, int MOD) { return fastpow(n, MOD - 2, MOD); } vector<int> path[222000]; queue<int> one; vector<int> ansdul, ansak; int deg[222000], tmpdeg[222000], simdeg[222000]; bool removed[222000]; int N; int main() { std::ios::sync_with_stdio(false); cin >> N; for (int i = 0; i < N; i++) { int x; cin >> x; if (x == 0) continue; x--; path[x].push_back(i); path[i].push_back(x); deg[x]++; deg[i]++; } if (N == 1) { cout << "YES" << endl; cout << 1 << endl; return 0; } for (int i = 0; i < N; i++) { tmpdeg[i] = deg[i]; simdeg[i] = deg[i]; if (deg[i] == 1) one.push(i); } while (one.size()) { int idx = one.front(); one.pop(); if (removed[idx]) continue; removed[idx] = true; if (tmpdeg[idx] % 2) ansak.push_back(idx); else { ansdul.push_back(idx); for (int x : path[idx]) { tmpdeg[x]--; } } deg[idx] = 0; for (int x : path[idx]) { deg[x]--; if (deg[x] == 1) one.push(x); } } reverse(ansak.begin(), ansak.end()); for (int i : ansak) { ansdul.push_back(i); } for (int a : ansdul) { if (simdeg[a] % 2) { cout << "NO" << endl; return 0; } for (int x : path[a]) { simdeg[x]--; } } cout << "YES" << endl; for (int a : ansdul) cout << a + 1 << endl; }
#include <bits/stdc++.h> using namespace std; int n; int ans[200000 + 5], root, vis[200000 + 5], del[200000 + 5]; int degree[200000 + 5], parent[200000 + 5]; vector<int> edge[200000 + 5]; int top; void Del(int node) { ans[++top] = node; del[node] = 1; for (int i = 0; i < edge[node].size(); i++) { int v = edge[node][i]; if (del[v]) continue; degree[v] -= 1; if (degree[v] % 2 == 0 && parent[node] != v) Del(v); } } void dfs(int node) { vis[node] = 1; for (int i = 0; i < edge[node].size(); i++) { int v = edge[node][i]; if (vis[v]) continue; parent[v] = node; dfs(v); } if (degree[node] % 2 == 0) Del(node); } int main() { cin >> n; for (int i = 1; i <= n; i++) { int v; cin >> v; if (v == 0) root = i; else { edge[i].push_back(v); edge[v].push_back(i); } } for (int i = 1; i <= n; i++) degree[i] = edge[i].size(); dfs(root); if (top == n) { cout << "YES" << endl; for (int i = 1; i <= n; i++) cout << ans[i] << endl; } else cout << "NO"; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> inline void amin(T &x, const T &y) { if (y < x) x = y; } template <class T> inline void amax(T &x, const T &y) { if (x < y) x = y; } template <class Iter> void rprintf(const char *fmt, Iter begin, Iter end) { for (bool sp = 0; begin != end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); } putchar('\n'); } const int MAXN = 200011; int N; vector<int> G[MAXN]; int P[MAXN]; int par[MAXN]; int sz[MAXN]; bool in[MAXN]; int deg[MAXN]; int even[MAXN]; vector<int> ord; bool active(int v) { return deg[v] % 2 == 0 && even[v] == 0; } void MAIN() { scanf("%d", &N); for (int i = 0, i_len = (N); i < i_len; ++i) scanf("%d", P + i); if (N & 1) { for (int i = 0, i_len = (N); i < i_len; ++i) if (P[i]) { int w = P[i] - 1; G[i].push_back(w); G[w].push_back(i); } ord.reserve(N); ord.push_back(0); for (int i_ = 0, i__len = (N); i_ < i__len; ++i_) { int v = ord[i_]; for (__typeof((G[v]).begin()) e = (G[v]).begin(), e_end = (G[v]).end(); e != e_end; ++e) if (*e != par[v]) { ord.push_back(*e); par[*e] = v; } } for (int i_ = 0, i__len = (N); i_ < i__len; ++i_) { int v = ord[N - 1 - i_]; sz[v] = 1; for (__typeof((G[v]).begin()) e = (G[v]).begin(), e_end = (G[v]).end(); e != e_end; ++e) if (*e != par[v]) { sz[v] += sz[*e]; deg[v]++; if (sz[*e] % 2 == 0) even[v]++; } if (v) { deg[v]++; if (sz[v] % 2 == 1) even[v]++; } } stack<int> st; vector<int> ans; for (int i = 0, i_len = (N); i < i_len; ++i) if (active(i)) { in[i] = true; st.push(i); } while (!st.empty()) { int v = st.top(); st.pop(); if (!active(v)) { in[v] = false; continue; } ans.push_back(v); for (__typeof((G[v]).begin()) e = (G[v]).begin(), e_end = (G[v]).end(); e != e_end; ++e) { deg[*e]--; even[*e]--; if (!in[*e] && active(*e)) { in[*e] = true; st.push(*e); } } } if ((int)ans.size() == N) { puts("YES"); for (int i = 0, i_len = (N); i < i_len; ++i) printf("%d\n", ans[i] + 1); } else { puts("NO"); } } else { puts("NO"); } } int main() { int TC = 1; for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN(); return 0; }
#include <bits/stdc++.h> using namespace std; constexpr int INF = 1e9 + 5; constexpr long long INFL = 1e18; const int maxn = 200005; int n, root; vector<int> graf[maxn]; vector<int> skier[maxn]; int par[maxn], degin[maxn], degout[maxn]; void spierpapier() { cout << "NO\n"; exit(0); } void dfs(int a) { for (int& v : graf[a]) dfs(v); if (degout[a] & 1) { if (a == root) spierpapier(); degout[a]++; skier[a].push_back(par[a]); degin[par[a]]++; } else if (a != root) { degin[a]++; skier[par[a]].push_back(a); degout[par[a]]++; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { int p; cin >> p; if (p) { graf[p].push_back(i); par[i] = p; } else root = i; } dfs(root); cout << "YES\n"; queue<int> q; for (int i = 1; i <= n; i++) { if (degin[i] == 0) q.push(i); } while (!q.empty()) { int w = q.front(); q.pop(); cout << w << "\n"; for (int& v : skier[w]) { if (--degin[v] == 0) q.push(v); } } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> v[200001]; int deg[200001]; bool vis[200001]; void dfs(int s) { vis[s] = 1; deg[s] = 1; for (auto i : v[s]) { if (vis[i]) continue; dfs(i); deg[s] += deg[i]; } } void solve(int s) { for (auto i : v[s]) if (deg[i] % 2 == 0) solve(i); cout << s << '\n'; for (auto i : v[s]) if (deg[i] % 2 == 1) solve(i); } int main() { int n; cin >> n; if (n % 2 == 0) { cout << "NO\n"; return 0; } cout << "YES\n"; int rt; for (int i = 1; i <= n; i++) { int x; cin >> x; if (x == 0) rt = i; else v[x].push_back(i); } dfs(rt); solve(rt); return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> g[210000]; int n, x, f[210000], size[210000]; void dfs(int x, int fa) { f[x] = fa; size[x] = 1; for (int i = 0; i < g[x].size(); i++) { int t = g[x][i]; if (t != fa) { dfs(t, x); size[x] += size[t]; } } } void find(int x) { for (int i = 0; i < g[x].size(); i++) { int t = g[x][i]; if (t != f[x]) if (size[t] % 2 == 0) find(t); } cout << x << endl; for (int i = 0; i < g[x].size(); i++) { int t = g[x][i]; if (t != f[x]) if (size[t] % 2 == 1) find(t); } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &x); if (x) { g[x].push_back(i); g[i].push_back(x); } } if (n % 2 == 1) { cout << "YES" << endl; dfs(1, 0); find(1); } else cout << "NO"; return 0; }
#include <bits/stdc++.h> using namespace std; vector<vector<int>> g; vector<int> ans; vector<int> kek; void destroy(int v, int p) { if (kek[v] % 2 == 0) { kek[v] = -99999; ans.push_back(v); for (auto i : g[v]) { --kek[i]; if (i != p) { destroy(i, v); } } } } void dfs(int v, int p) { for (auto i : g[v]) { if (p != i) { dfs(i, v); } } destroy(v, p); } int main() { int n; cin >> n; g.resize(n); kek.resize(n); for (int i = 0; i < n; ++i) { int a; cin >> a; --a; if (a != -1) { g[i].push_back(a); g[a].push_back(i); kek[i]++; kek[a]++; } } if (n % 2 == 0) { cout << "NO" << endl; return 0; } cout << "YES" << endl; dfs(0, -1); for (int i = 0; i < ans.size(); ++i) { cout << ans[i] + 1 << endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int maxn = 2e5, mod = 1e9 + 9; int n, par[maxn]; vector<int> g[maxn], ord; void dfs1(int v) { par[v] ^= 1; ord.push_back(v); for (int to : g[v]) { if (par[to] & 1) dfs1(to); } } void dfs(int v, int p) { for (int to : g[v]) { if (to != p) dfs(to, v); } if (!par[v]) { ord.push_back(v); for (int to : g[v]) { if (to != p && par[to]) dfs1(to); } if (p != -1) par[p] ^= 1; } } int32_t main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 0; i < n; i++) { int v; cin >> v; v--; if (v != -1) { g[i].push_back(v); g[v].push_back(i); } } for (int i = 0; i < n; i++) par[i] = ((int)((g[i]).size()) & 1); dfs(0, -1); if ((int)((ord).size()) != n) cout << "NO"; else { cout << "YES\n"; for (int v : ord) cout << v + 1 << '\n'; } }
#include <bits/stdc++.h> using namespace std; template <typename T> inline void Read(T &x) { char c = getchar(); bool f = false; for (x = 0; !isdigit(c); c = getchar()) { if (c == '-') { f = true; } } for (; isdigit(c); c = getchar()) { x = x * 10 + c - '0'; } if (f) { x = -x; } } template <typename T> inline bool CheckMax(T &a, const T &b) { return a < b ? a = b, true : false; } template <typename T> inline bool CheckMin(T &a, const T &b) { return a > b ? a = b, true : false; } const int N = 200005; int n, h, r, t, d[N], p[N], ans[N]; vector<int> seq, adj[N]; inline void DFS(int x) { seq.push_back(x); for (auto y : adj[x]) { DFS(y); } } int main() { Read(n), t = n + 1; if (!(n & 1)) { puts("NO"); return 0; } puts("YES"); for (int i = 1; i <= n; ++i) { Read(p[i]); if (p[i]) { adj[p[i]].push_back(i), ++d[i], ++d[p[i]]; } else { r = i; } } DFS(r); reverse(seq.begin(), seq.end()); for (auto x : seq) { if (d[x] & 1) { ans[--t] = x; } else { ans[++h] = x, --d[p[x]]; } } for (int i = 1; i <= n; ++i) { printf("%d\n", ans[i]); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 500000 + 5; int head[maxn], to[maxn << 1], nex[maxn << 1], cnt, degree[maxn], parent[maxn], vis[maxn]; stack<int> dfs; queue<int> answer; inline void add_edge(int u, int v) { nex[++cnt] = head[u]; head[u] = cnt; to[cnt] = v; } void dfs1(int u, int fa) { dfs.push(u); parent[u] = fa; for (int v = head[u]; v; v = nex[v]) if (to[v] != fa) dfs1(to[v], u); } void dfs2(int u) { answer.push(u); vis[u] = 1; for (int v = head[u]; v; v = nex[v]) { --degree[to[v]]; if (to[v] == parent[u] || vis[to[v]]) continue; if (degree[to[v]] % 2 == 0) dfs2(to[v]); } } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) { int a; scanf("%d", &a); if (a == 0) continue; add_edge(i, a); add_edge(a, i); ++degree[a], ++degree[i]; } dfs1(1, 0); while (dfs.size()) { int u = dfs.top(); dfs.pop(); if (degree[u] % 2 == 0) dfs2(u); } if (answer.size() == n) { printf("YES\n"); while (!answer.empty()) { printf("%d\n", answer.front()); answer.pop(); } } else printf("NO"); return 0; }
#include <bits/stdc++.h> using namespace std; struct Edge { int to; int next; int val; } edge[400005]; int head[200005]; int cnt = 1; void addedge(int u, int v) { edge[cnt].to = v; edge[cnt].next = head[u]; head[u] = cnt++; } int rt; int num[200005]; bool vis[200005]; int ans[200005]; int tot = 0; void dfs1(int u, int fa) { ans[++tot] = u; vis[u] = 1; for (int i = head[u]; i; i = edge[i].next) { int to = edge[i].to; if (to == fa) continue; if (!vis[to]) dfs1(to, u); } } void dfs(int u, int fa) { for (int i = head[u]; i; i = edge[i].next) { int to = edge[i].to; if (to == fa) continue; dfs(to, u); if (vis[to]) num[u] ^= 1; } if (!num[u]) dfs1(u, fa); } set<int> s0; set<int> s1; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); if (x) { addedge(i, x); addedge(x, i); } else rt = i; } dfs(rt, 0); if (tot < n) return printf("NO\n"), 0; printf("YES\n"); for (int i = tot; i >= 1; i--) printf("%d\n", ans[i]); }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 1000; int du[N]; int fa[N]; vector<int> g[N]; queue<int> q; bool vis[N]; void del(int u) { if (vis[u]) return; q.push(u); vis[u] = 1; for (int i = 0; i < g[u].size(); ++i) { int v = g[u][i]; if (vis[v]) continue; del(v); } } void dfs(int u) { for (int i = 0; i < g[u].size(); ++i) { dfs(g[u][i]); } if (du[u] % 2 == 0) { du[fa[u]]--; del(u); } } int main() { int n; scanf("%d", &n); int root; for (int i = 1; i <= n; ++i) { scanf("%d", &fa[i]); g[fa[i]].push_back(i); if (fa[i] == 0) { root = i; continue; } du[fa[i]]++; du[i]++; } dfs(root); if (q.size() != n) { puts("NO"); return 0; } puts("YES"); while (!q.empty()) { int v = q.front(); printf("%d\n", v); q.pop(); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; vector<vector<int> > g; vector<int> s, d, ans; deque<int> cur; bool nope = false; void dfs(const int pre, const int me) { for (const int son : g[me]) { dfs(me, son); if (nope) return; } if (s[me] % 2 == 0 && pre != -1) { d[pre] += d[me] + 1; ++s[pre]; cur.push_front(me + 1); } else if ((s[me] % 2 == 0 && pre == -1) || (s[me] % 2 == 1 && pre != -1)) { ans.push_back(me + 1); while (d[me] > 0) { --d[me]; ans.push_back(cur.front()); cur.pop_front(); } } else nope = true; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, a, r; cin >> n; g.resize(n); s.resize(n, 0); d.resize(n, 0); for (int i = 0; i < (int)n; ++i) { cin >> a; --a; if (a == -1) r = i; else g[a].push_back(i); } dfs(-1, r); if (nope) cout << "NO" << endl; else { cout << "YES" << endl; for (int i = 0; i < (int)(int)(ans).size(); ++i) cout << ans[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; vector<long long> v[200005]; long long sz[200005]; long long vis[200005]; void pnt(long long n) { long long i, j; vis[n] = 1; long long len = v[n].size(); for (i = 0; i < len; i++) { if (!vis[v[n][i]]) pnt(v[n][i]); sz[n] += sz[v[n][i]]; } } void dfs(long long n) { long long i, j; long long len = v[n].size(); for (i = 0; i < len; i++) { if (sz[v[n][i]] % 2 == 0) dfs(v[n][i]); } printf("%lld\n", n); for (i = 0; i < len; i++) { if (sz[v[n][i]] % 2 == 1) dfs(v[n][i]); } } int main() { long long i, j, n, m, t, src; for (i = 1; i <= 200000; i++) sz[i] = 1; scanf("%lld", &t); if (t % 2 == 0) { printf("NO\n"); return 0; } printf("YES\n"); for (i = 1; i <= t; i++) { scanf("%lld", &n); if (n == 0) src = i; else v[n].push_back(i); } pnt(src); dfs(src); }
#include <bits/stdc++.h> using namespace std; vector<int> gr[200005], before[200005], after[200005]; int n, root; bool go(int now, int prv) { for (int i = 0; i < gr[now].size(); i++) { int to = gr[now][i]; if (to == prv) continue; if (go(to, now)) after[now].push_back(to); else before[now].push_back(to); } return (after[now].size() % 2 == 0); } void dfs(int now) { for (int i = 0; i < before[now].size(); i++) dfs(before[now][i]); printf("%d\n", now); for (int i = 0; i < after[now].size(); i++) dfs(after[now][i]); } int main() { int i, j; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &j); if (j) { gr[i].push_back(j); gr[j].push_back(i); } else root = i; } if (go(root, -1)) { printf("YES\n"); dfs(root); } else printf("NO\n"); }
#include <bits/stdc++.h> using namespace std; const int MM = 2e5 + 5; int n, sz[MM]; vector<int> adj[MM]; void dfs(int u, int p) { sz[u] = 1; for (int v : adj[u]) if (v != p) { dfs(v, u); sz[u] += sz[v]; } } void dfs1(int u, int p) { for (int v : adj[u]) if (v != p) { if (sz[v] % 2 == 0) dfs1(v, u); } cout << u << "\n"; for (int v : adj[u]) if (v != p) { if (sz[v] % 2 == 1) dfs1(v, u); } } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n; if (n % 2 == 0) { cout << "NO\n"; return 0; } cout << "YES\n"; for (int i = 1; i <= n; i++) { int x; cin >> x; if (x == 0) continue; adj[x].emplace_back(i); adj[i].emplace_back(x); } dfs(1, 0); dfs1(1, 0); }
#include <bits/stdc++.h> using namespace std; const long long infl = 1e18 + 5; long long n, m, k, q, x, y, f, val, t, i, j; long long ind, cnt, sz, sm, ans, mx, mn; long long a[1000004], vis[1000004]; vector<int> g[1000004]; void dfs1(int u, int p) { if (vis[u]) return; vis[u] = 1; cout << u << " "; for (auto it : g[u]) { if (it != p) dfs1(it, u); } } int dfs(int u, int p) { int re = 0; if (p != -1) re = 1; for (auto it : g[u]) { if (it != p) { re += dfs(it, u); } } if (re % 2 == 0) { dfs1(u, p); return 0; } return 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); if (fopen("inp.txt", "r")) { freopen("myfile.txt", "w", stdout); freopen("inp.txt", "r", stdin); } cin >> n; for (i = 1; i < n + 1; i++) { cin >> x; if (x == 0) { k = i; continue; } g[i].emplace_back(x); g[x].emplace_back(i); } if (n % 2 == 0) cout << "NO"; else { cout << "YES\n"; dfs(k, -1); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int n; vector<vector<int>> ed; vector<int> subt, answer; void init() { ed = vector<vector<int>>(n + 1); subt = vector<int>(n + 1); answer = vector<int>(n + 1); } void dfs(int fa, int u) { subt[u] = 1; for (auto i : ed[u]) { if (i != fa) { dfs(u, i); subt[u] += subt[i]; } } } void solve(int fa, int u) { for (auto i : ed[u]) { if (i != fa && subt[i] % 2 == 0) solve(u, i); } cout << u << "\n"; for (auto i : ed[u]) { if (i != fa && subt[i] % 2) solve(u, i); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n; init(); vector<int> inp(n); for (int i = 0; i < n; i++) { cin >> inp[i]; } int rt = 1; for (int i = 0; i < n; i++) { if (inp[i]) { int u = i + 1, v = inp[i]; ed[u].push_back(v); ed[v].push_back(u); } else rt = i + 1; } if (n % 2 == 0) { cout << "NO\n"; return 0; } dfs(0, 1); cout << "YES\n"; solve(0, 1); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 200005; vector<int> res; vector<int> dz[N], levels[N]; set<int> visited; int i, j, n, ojciec[N], w, siz[N], root; void go(int w) { res.push_back(w); visited.insert(w); for (int i = 0; i < dz[w].size(); i++) if (visited.find(dz[w][i]) == visited.end()) go(dz[w][i]); } void countLevels(int w, int l) { levels[l].push_back(w); for (int i = 0; i < dz[w].size(); i++) countLevels(dz[w][i], l + 1); } int main() { scanf("%d", &n); for (i = 1; i <= n; i++) { dz[i].clear(); levels[i].clear(); } for (i = 1; i <= n; i++) { scanf("%d", &ojciec[i]); if (ojciec[i] != 0) dz[ojciec[i]].push_back(i); else root = i; } visited.clear(); countLevels(root, 0); for (i = 1; i <= n; i++) siz[i] = dz[i].size() + 1; siz[root]--; for (i = n; i >= 1; i--) { for (j = 0; j < levels[i].size(); j++) { w = levels[i][j]; if (siz[w] % 2 == 0) { go(w); siz[ojciec[w]]--; } } } if (siz[root] % 2 == 0) { go(root); printf("YES\n"); for (i = 0; i < res.size(); i++) printf("%d\n", res[i]); } else { printf("NO\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX = 210000; const int INF = 1000000000; const int MOD = 1000000007; int mod(int a, int b, int m) { int ret = 1; a %= m; while (b) { if (b & 1) ret = ret * a % m; b >>= 1; a = a * a % m; } return ret; } int inv(int a) { return mod(a, MOD - 2, MOD); } int vabs(int x) { return x > 0 ? x : -x; } int gcd(int a, int b) { return a % b ? gcd(b, a % b) : b; } int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; int son[MAX]; bool flag[MAX]; vector<int> e[MAX]; bool used[MAX]; queue<int> q; int calcSon(int r, int pre) { int i; son[r] = 1; for (i = 0; i < e[r].size(); i++) { int v = e[r][i]; if (v == pre || used[v]) continue; son[r] += calcSon(v, r); } return son[r]; } void destruction(int r, int pre) { int i; for (i = 0; i < e[r].size(); i++) { int v = e[r][i]; if (v == pre || son[v] & 1) continue; destruction(v, r); } printf("%d\n", r); for (i = 0; i < e[r].size(); i++) { int v = e[r][i]; if (v == pre || !(son[v] & 1)) continue; destruction(v, r); } } int main() { int n, i, j; cin >> n; for (i = 1; i <= n; i++) { son[i] = 0; e[i].clear(); } for (i = 1; i <= n; i++) { int p; scanf("%d", &p); if (p != 0) { e[p].push_back(i); e[i].push_back(p); } } if (n % 2 == 0) { puts("NO"); return 0; } puts("YES"); calcSon(1, -1); destruction(1, -1); return 0; }
#include <bits/stdc++.h> using namespace std; int const N = 2e5 + 10; int n, s, subtree[N], deg[N], par[N]; vector<int> adj[N]; bool mark[N]; void read_input() { cin >> n; for (int i = 0; i < n; i++) { int a; cin >> a; if (a) { a--; adj[i].push_back(a); adj[a].push_back(i); deg[a]++, deg[i]++; } } } void dstr(int v) { if (!mark[v]) { cout << v + 1 << '\n'; mark[v] = true; } for (int q : adj[v]) if (!mark[q] && q != par[v]) dstr(q); } void dfs(int s) { subtree[s] = 1; for (int u : adj[s]) if (u != par[s]) { par[u] = s; dfs(u); subtree[s] += subtree[u]; } if (!(subtree[s] % 2)) dstr(s); } void solve() { if (n % 2 == 0) { cout << "NO"; exit(0); } for (int i = 0; i < n; i++) if (deg[i] == 1) s = i; cout << "YES\n"; if (n == 1) cout << 1; else { par[s] = -1; dfs(s); cout << s + 1; } } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); read_input(); solve(); }
#include <bits/stdc++.h> using namespace std; vector<int> edges[300000]; int n, m, x, y, z, p, q, r; int sbtree[300000], isTaken[300000], degree[300000], bdchld[300000]; vector<int> anss; void dfs1(int pos) { sbtree[pos]++; for (int to : edges[pos]) { dfs1(to); sbtree[pos] += sbtree[to]; } } void dfs(int pos) { bool flg = true; for (int to : edges[pos]) { if (sbtree[to] % 2 == 0) { dfs(to); } } anss.push_back(pos); for (int to : edges[pos]) { if (sbtree[to] % 2) { dfs(to); } } } int main() { int root = 1; cin >> n; for (int i = 1; i <= n; i++) { scanf("%d", &x); edges[x].push_back(i); degree[x]++; degree[i]++; if (x == 0) root = i; } if (n % 2 == 0) { cout << "NO" << endl; return 0; } cout << "YES" << endl; dfs1(root); dfs(root); for (int xx : anss) printf("%d\n", xx); return 0; }
#include <bits/stdc++.h> using namespace std; const int MaxN = 2e5 + 5; int N, Root; int Size[MaxN]; bool Vis[MaxN]; vector<int> Son[MaxN]; namespace Input { const int L = 1 << 20; char *s, *t, c, buf[L]; char gc() { if (s == t) { t = (s = buf) + fread(buf, 1, L, stdin); if (s == t) return EOF; } return *s++; } void gi(int &x) { x = 0; while (c = gc(), c < '0' || c > '9') ; while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc(); } } // namespace Input using Input::gi; void Del(int u) { Vis[u] = true; printf("%d\n", u); int i; for (i = 0; i < Son[u].size(); i++) if (!Vis[Son[u][i]]) Del(Son[u][i]); } void DFS(int u) { int i; Size[u] = 1; for (i = 0; i < Son[u].size(); i++) { DFS(Son[u][i]); Size[u] += Size[Son[u][i]]; } if (!(Size[u] & 1)) Del(u); } int main() { int i, fa; gi(N); if (!(N & 1)) { puts("NO"); return 0; } puts("YES"); for (i = 1; i <= N; i++) { gi(fa); if (fa) Son[fa].push_back(i); else Root = i; } DFS(Root); Del(Root); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = (int)2e5 + 5; const int inf = (int)1e9 + 7; int n; int s[N]; vector<int> g[N]; void dfs(int v, int p) { s[v] = 1; for (int i : g[v]) { if (i == p) { continue; } dfs(i, v); s[v] += s[i]; } } void dfs2(int v, int p) { for (int i : g[v]) { if (i == p) continue; if (s[i] % 2 == 0) { dfs2(i, v); } } printf("%d\n", v); for (int i : g[v]) { if (i == p) continue; if (s[i] % 2 == 1) { dfs2(i, v); } } } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { int x; scanf("%d", &x); if (x) { g[x].push_back(i); g[i].push_back(x); } } if (n % 2 == 0) { printf("NO\n"); return 0; } printf("YES\n"); dfs(1, 1); dfs2(1, 1); return 0; }
#include <bits/stdc++.h> using namespace std; int n, fa[200005], deg[200005], vis[200005], head[200005], tot; stack<int> sta; int ans[200005], num; struct edge { int to, next; } e[200005 << 1]; inline void add(int u, int v) { e[++tot] = (edge){v, head[u]}; head[u] = tot; } inline int read() { int x = 0; char c = std::getchar(); while (c < '0' || c > '9') c = std::getchar(); while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ 48); c = std::getchar(); } return x; } void dfs1(int x, int f) { fa[x] = f; sta.push(x); for (int i = head[x]; i; i = e[i].next) { int v = e[i].to; if (v == f) continue; dfs1(v, x); } } void dfs2(int x) { vis[x] = 1; ans[++num] = x; for (int i = head[x]; i; i = e[i].next) { int v = e[i].to; deg[v]--; if (v == fa[x] || vis[v]) continue; if (!(deg[v] & 1)) dfs2(v); } } int main() { n = read(); int v; for (int i = 1; i <= n; i++) { v = read(); if (v) { add(i, v); add(v, i); ++deg[v]; ++deg[i]; } } dfs1(1, -1); while (!sta.empty()) { int az = sta.top(); sta.pop(); if (!(deg[az] & 1)) dfs2(az); } if (num == n) { puts("YES"); for (int i = 1; i <= num; i++) printf("%d\n", ans[i]); } else puts("NO"); return ~~(0 ^ 0) * (0 ^ 0); }
#include <bits/stdc++.h> using namespace std; int N, rt; vector<int> Adj[200005]; bool dp[200005]; void dfs0(int u) { for (int k = 0; k < Adj[u].size(); k++) { int v = Adj[u][k]; dfs0(v); if (dp[v] == 0) dp[u] = !dp[u]; } } vector<int> od; void dfs1(int u) { for (int k = 0; k < Adj[u].size(); k++) { int v = Adj[u][k]; if (dp[v] == 1) dfs1(v); } od.push_back(u); for (int k = 0; k < Adj[u].size(); k++) { int v = Adj[u][k]; if (dp[v] == 0) dfs1(v); } } int main() { ios::sync_with_stdio(0); cin >> N; int u, v; for (int i = 1; i <= N; i++) { cin >> v; if (v == 0) rt = i; else Adj[v].push_back(i); } dfs0(rt); if (dp[rt]) { cout << "NO"; return 0; } cout << "YES" << endl; dfs1(rt); for (int k = 0; k < od.size(); k++) cout << od[k] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> v[200009]; vector<int> vv; deque<int> dq; bool be_ve[200009], af_ve[200009]; void dfs(int i, int p) { int be = 0, af = 0; for (auto j : v[i]) { if (j != p) { dfs(j, i); if (be_ve[j]) { be++; } else { af++; } } } if (af % 2 == 0) { af_ve[i] = 1; } else { be_ve[i] = 1; } } void dfs_a(int i, int p) { int be = 0, af = 0; for (auto j : v[i]) { if (j != p) { if (be_ve[j]) { dq.push_front(j); } else { dq.push_back(j); } dfs_a(j, i); } } } int main() { int n, i, j, x, y; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &x); if (x != 0) { v[i].push_back(x); v[x].push_back(i); } } dq.push_back(1); dfs(1, 0); if (af_ve[1]) { dfs_a(1, 0); cout << "YES" << " "; ; cout << endl; for (auto j : dq) { printf("%d\n", j); ; } } else { cout << "NO" << " "; ; } }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target( \ "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") using namespace std; void itval(istream_iterator<string> it) {} template <typename T, typename... Args> void itval(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; itval(++it, args...); } const long long int MOD = 1e9 + 7; template <typename T> inline void print(T x) { cout << x << "\n"; } template <typename T> inline void printvec(T x) { for (auto a : x) cout << a << ' '; cout << '\n'; } struct custom { bool operator()(const pair<long long int, long long int> &p1, const pair<long long int, long long int> &p2) const { return p1.first < p2.first; } }; long long int get_pow(long long int a, long long int b, long long int M = MOD) { long long int res = 1; while (b) { if (b & 1) res = (res * a) % M; a = (a * a) % M; b >>= 1; } return res; } const long long int N = 2e5 + 5, inf = 4e18; int deg[N], done[N]; std::vector<int> adj[N], nxt[N]; vector<int> order; void travel(int s) { order.push_back(s + 1); for (auto x : nxt[s]) travel(x); } void dfs(int s, int p) { for (auto x : adj[s]) { if (x != p) { dfs(x, s); } } int cnt = 0; vector<int> lef; for (auto x : adj[s]) { if (x == p) continue; if (done[x]) cnt++; else lef.push_back(x); } if ((deg[s] - cnt) % 2 == 0) { order.push_back(s + 1); done[s] = 1; for (auto x : lef) travel(x); } else { for (auto x : lef) nxt[s].push_back(x); } } void solve() { int n, x; cin >> n; for (long long int i = (long long int)0; i < (long long int)(n); i++) { cin >> x; x--; if (x != -1) { adj[x].push_back(i); adj[i].push_back(x); deg[x]++; deg[i]++; } } if (n == 1) { cout << "YES\n1\n"; return; } for (long long int i = (long long int)0; i < (long long int)(n); i++) { if (deg[i] == 1) { dfs(i, -1); break; } } if (order.size() != n) { cout << "NO\n"; return; } cout << "YES\n"; for (auto x : order) cout << x << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int test = 1; clock_t z = clock(); for (long long int tes = (long long int)0; tes < (long long int)(test); tes++) { solve(); } fprintf(stderr, "Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC), fflush(stderr); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 200002; int n; struct edge { int to, nxt; } tree[N << 1]; int head[N], cnt = 0; void addEdge(int u, int v) { edge node = {v, head[u]}; tree[head[u] = ++cnt] = node; } int num[N], deg[N]; vector<int> graph[N]; void dfs(int u, int v) { num[u] = 0; for (int i = head[u]; i; i = tree[i].nxt) if (tree[i].to != v) { dfs(tree[i].to, u), num[u] ^= (num[tree[i].to] ^ 1); if (num[tree[i].to]) graph[tree[i].to].push_back(u); else graph[u].push_back(tree[i].to); } } queue<int> q; vector<int> seq; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { int pi; scanf("%d", &pi); if (!pi) continue; addEdge(pi, i), addEdge(i, pi); } if (n % 2 == 0) printf("NO\n"); else { printf("YES\n"); dfs(1, 0); for (int i = 1; i <= n; i++) deg[i] = 0; for (int i = 1; i <= n; i++) for (int j = 0; j < graph[i].size(); j++) deg[graph[i][j]]++; for (int i = 1; i <= n; i++) if (!deg[i]) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(), seq.push_back(u); for (int i = 0; i < graph[u].size(); i++) { deg[graph[u][i]]--; if (!deg[graph[u][i]]) q.push(graph[u][i]); } } for (int i = 1; i <= n; i++) printf("%d\n", seq[i - 1]); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 200100; vector<int> ed[MAXN]; int sz[MAXN]; void dfs1(int cur, int par = -1) { vector<int> &v = ed[cur]; sz[cur] = 1; for (int i = 0; i < v.size(); ++i) { int nxt = v[i]; if (nxt == par) continue; dfs1(nxt, cur); sz[cur] += sz[nxt]; } } vector<int> ans; void dfs2(int cur, int par = -1) { vector<int> &v = ed[cur]; for (int i = 0; i < v.size(); ++i) { int nxt = v[i]; if (nxt == par) continue; if (sz[nxt] % 2 == 0) { dfs2(nxt, cur); } } ans.push_back(cur); for (int i = 0; i < v.size(); ++i) { int nxt = v[i]; if (nxt == par) continue; if (sz[nxt] % 2 == 1) { dfs2(nxt, cur); } } } int n; int main() { cin >> n; if (n % 2 == 0) { cout << "NO" << "\n"; return 0; } for (int i = 1; i <= n; ++i) { int p; cin >> p; if (p) { ed[i].push_back(p); ed[p].push_back(i); } } dfs1(1); dfs2(1); cout << "YES\n"; for (int i = 0; i < n; ++i) { cout << ans[i] << "\n"; } }
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& a) { in >> a.first >> a.second; return in; } template <typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2> a) { out << a.first << " " << a.second; return out; } template <typename T, typename T1> T maxs(T& a, T1 b) { if (b > a) a = b; return a; } template <typename T, typename T1> T mins(T& a, T1 b) { if (b < a) a = b; return a; } long long solve() { long long n; cin >> n; vector<long long> g[n + 1]; long long r; for (long long i = 1; i < n + 1; i++) { long long p; cin >> p; if (p) g[p].push_back(i); if (!p) r = i; } vector<long long> dp(n + 1), h(n + 1); vector<long long> vec; function<void(long long)> dfs = [&](long long u) { long long d = 0; if (u != r) d++; for (auto i : g[u]) { h[i] = h[u] + 1; dfs(i); if (dp[i] == 0) d++; } if (d % 2 == 0) { vec.push_back(u); dp[u] = 1; } else dp[u] = 0; }; dfs(r); if (dp[r]) { cout << "YES" << "\n"; vector<pair<long long, long long> > p; for (long long i = 1; i < n + 1; i++) { if (dp[i] == 0) p.push_back({h[i], i}); } sort(p.begin(), p.end()); for (auto i : vec) cout << i << "\n"; for (auto i : p) cout << i.second << "\n"; } else { cout << "NO" << "\n"; } return 0; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2 * 1e5 + 10; vector<int> vec[maxn]; vector<int> ans; stack<int> st; int pa[maxn]; int deg[maxn]; int vis[maxn]; void dfs(int u, int pre) { pa[u] = pre; st.push(u); for (int i = 0; i < vec[u].size(); i++) { int t = vec[u][i]; if (t == pre) continue; dfs(t, u); } } void dfs2(int u) { vis[u] = 1; ans.push_back(u); for (int i = 0; i < vec[u].size(); i++) { int t = vec[u][i]; deg[t]--; if (t == pa[u] || vis[t]) continue; if (deg[t] % 2 == 0) dfs2(t); } } int main() { int n; while (cin >> n) { for (int i = 0; i <= n; i++) vec[i].clear(); ans.clear(); while (!st.empty()) st.pop(); memset(deg, 0, sizeof(deg)); memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) { int t; cin >> t; if (t) { vec[i].push_back(t); vec[t].push_back(i); deg[t]++; deg[i]++; } } dfs(1, -1); while (!st.empty()) { int t = st.top(); st.pop(); if (deg[t] % 2 == 0) { dfs2(t); } } if (ans.size() == n) { cout << "YES" << endl; for (int i = 0; i < ans.size(); i++) cout << ans[i] << endl; } else cout << "NO" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 9; const long long inf = 0x3f3f3f3f3f3f3f; const int maxn = 2e5 + 5; int n, a, k, rt; int siz[maxn]; bool vis[maxn]; vector<int> g[maxn]; vector<int> son[maxn]; void dfs1(int v) { siz[v] = 1; for (int i = 0; i < g[v].size(); i++) { dfs1(g[v][i]); siz[v] += siz[g[v][i]]; if (siz[g[v][i]] % 2 == 0) { son[v].push_back(g[v][i]); } } } void dfs(int v) { vis[v] = true; for (int i = 0; i < son[v].size(); i++) { dfs(son[v][i]); } printf("%d\n", v); for (int i = 0; i < g[v].size(); i++) { if (!vis[g[v][i]]) dfs(g[v][i]); } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &k); if (k == 0) rt = i; g[k].push_back(i); } if (n % 2 == 0) { printf("NO\n"); } else { printf("YES\n"); dfs1(rt); dfs(rt); } return 0; }
#include <bits/stdc++.h> using namespace std; int n; vector<vector<int>> g, dp; bool dfs(int u, int p, int has_par) { int adj = has_par, any = 0; if (dp[u][has_par] != -1) return dp[u][has_par]; for (int v : g[u]) { if (v == p) continue; bool before_par = dfs(v, u, 1); bool after_par = dfs(v, u, 0); adj += !before_par && after_par; any += before_par && after_par; if (!before_par && !after_par) return dp[u][has_par] = false; } return dp[u][has_par] = 1 - (adj & 1) || any; } void print_choice(int u, int p, int has_par) { int any = 0; vector<int> adj; for (int v : g[u]) { if (v == p) continue; if (!dp[v][1] && dp[v][0]) adj.push_back(v); if (dp[v][1] && dp[v][0]) any = v; } if ((has_par + adj.size()) & 1) adj.push_back(any); for (int v : g[u]) { if (v == p || (adj.size() > 0 && v == adj[adj.size() - 1])) continue; if (dp[v][1]) print_choice(v, u, 1); } cout << u + 1 << "\n"; for (int v : adj) { print_choice(v, u, 0); } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n; g.assign(n, vector<int>()); dp.assign(n, vector<int>(2, -1)); for (int i = 0, j; i < n; ++i) { cin >> j; if (j-- > 0) { g[i].push_back(j); g[j].push_back(i); } } if (dfs(0, -1, 0)) { cout << "YES\n"; print_choice(0, -1, 0); } else { cout << "NO\n"; } }
#include <bits/stdc++.h> using namespace std; const int maxn = 2 * (int)(1e5) + 10; const int inf = 0x3f3f3f3f; const int mod = (int)1e9 + 9; const double eps = 1e-6; vector<int> edge[maxn * 2]; int sz[maxn]; void dfs1(int u) { sz[u] = 1; for (int i = 0; i < edge[u].size(); i++) { int v = edge[u][i]; dfs1(v); sz[u] += sz[v]; } } void dfs2(int u) { for (int i = 0; i < edge[u].size(); i++) { int v = edge[u][i]; if (sz[v] % 2 == 0) dfs2(v); } printf("%d\n", u); for (int i = 0; i < edge[u].size(); i++) { int v = edge[u][i]; if (sz[v] % 2) dfs2(v); } } int main() { int n; while (~scanf("%d", &n)) { for (int i = 1; i <= n; i++) edge[i].clear(); int root, v; for (int u = 1; u <= n; u++) { scanf("%d", &v); if (!v) root = u; edge[v].push_back(u); } if (n % 2 == 0) { printf("NO\n"); continue; } else { memset(sz, 0, sizeof(sz)); printf("YES\n"); dfs1(root); dfs2(root); } } return 0; }
#include <bits/stdc++.h> using namespace std; int p[200001]; vector<int> adj[200001]; int deg[200001]; int pt = 0; int order[200001]; int ret[200001]; void dfs(int cur) { for (auto e : adj[cur]) dfs(e); order[++pt] = cur; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; if (n & 1) cout << "YES\n"; else { cout << "NO\n"; return 0; } int root = 0; for (int i = 1; i <= n; i++) { cin >> p[i]; adj[p[i]].push_back(i); if (p[i] == 0) root = i; else { deg[p[i]]++; deg[i]++; } } dfs(root); vector<int> ret; vector<int> ret2; for (int i = 1; i <= n; i++) { int e = order[i]; if (deg[e] % 2 == 0) { ret.push_back(e); deg[p[e]]--; } else ret2.push_back(e); } ret.insert(ret.end(), ret2.rbegin(), ret2.rend()); for (int i = 0; i < n; i++) cout << ret[i] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 200000 + 10; vector<int> graph[maxn]; int N, sz[maxn]; void init(int u, int p) { sz[u] = 1; for (auto &v : graph[u]) { if (v == p) continue; init(v, u); sz[u] += sz[v]; } } void getRes(int u, int p) { for (auto &v : graph[u]) { if (v == p) continue; if (!(sz[v] & 1)) getRes(v, u); } cout << u << '\n'; for (auto &v : graph[u]) { if (v == p) continue; if (sz[v] & 1) getRes(v, u); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N; for (int i = 0; i < N; i++) { int v; cin >> v; if (!v) continue; graph[i + 1].push_back(v); graph[v].push_back(i + 1); } if (!(N & 1)) { cout << "NO\n"; return 0; } init(1, 1); cout << "YES\n"; getRes(1, 1); return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> E[200005]; int deg[200005]; bool dele[200005]; int n; void del(int u) { deg[u] = 0; dele[u] = 1; printf("%d\n", u); for (int i = 0; i < E[u].size(); i++) { if (deg[E[u][i]] != 0) deg[E[u][i]]--; } return; } void destroy(int fa, int u) { del(u); for (int i = 0; i < E[u].size(); i++) { if (E[u][i] != fa && dele[E[u][i]] == 0) { destroy(u, E[u][i]); } } return; } void dfs(int fa, int u) { for (int i = 0; i < E[u].size(); i++) { if (E[u][i] != fa) { dfs(u, E[u][i]); } } if (deg[u] % 2 == 0 && dele[u] == 0) { destroy(fa, u); } return; } int main() { scanf("%d", &n); if (n % 2 == 0) { printf("NO\n"); return 0; } printf("YES\n"); for (int i = 1; i <= n; i++) { int g; scanf("%d", &g); if (g) { E[g].push_back(i); E[i].push_back(g); deg[g]++; deg[i]++; } } dfs(0, 1); return 0; }
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') { f = -1; } ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, sz[200005], du[200005], h[200005], num = 0, rt; struct edge { int to, next; } data[200005 << 1]; void dfs(int x, int Fa) { sz[x] = 1; int y; for (int i = h[x]; i; i = data[i].next) { y = data[i].to; if (y == Fa) { continue; } dfs(y, x); sz[x] += sz[y]; } } void dfs1(int x, int Fa) { int y; for (int i = h[x]; i; i = data[i].next) { y = data[i].to; if (y == Fa) { continue; } if (!(sz[y] & 1)) { dfs1(y, x); } } printf("%d\n", x); for (int i = h[x]; i; i = data[i].next) { y = data[i].to; if (y == Fa) { continue; } if (sz[y] & 1) { dfs1(y, x); } } } int main() { n = read(); if (!(n & 1)) { puts("NO"); return 0; } puts("YES"); for (int i = 1; i <= n; ++i) { int y = read(); if (!y) { continue; } ++du[i]; ++du[y]; data[++num].to = y; data[num].next = h[i]; h[i] = num; data[++num].to = i; data[num].next = h[y]; h[y] = num; } for (int i = 1; i <= n; ++i) { if (!(du[i] & 1)) { rt = i; break; } } dfs(rt, 0); dfs1(rt, 0); return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 200010; void solve() { long long n, m, i, j, k; cin >> n; vector<vector<long long> > v(n + 1); long long p[n + 1], root; for (i = 1; i <= n; i++) { cin >> p[i]; if (p[i] > 0) { v[i].push_back(p[i]); v[p[i]].push_back(i); } else { root = i; } } if (n % 2 == 0) { cout << "NO\n"; return; } cout << "YES\n"; vector<long long> a; set<pair<long long, long long> > s; queue<long long> q; vector<long long> vis(n + 1), lev(n + 1), sz(n + 1); lev[root] = 1; q.push(root); vis[root] = 1; while (!q.empty()) { long long x = q.front(); q.pop(); if (v[x].size() % 2 == 0) { s.insert({-lev[x], x}); } for (i = 0; i < v[x].size(); i++) { if (!vis[v[x][i]]) { vis[v[x][i]] = 1; lev[v[x][i]] = lev[x] + 1; q.push(v[x][i]); } } } for (i = 1; i <= n; i++) sz[i] = v[i].size(); vector<long long> fr(n + 1); while (s.size() > 0) { set<pair<long long, long long> >::iterator itr; itr = s.begin(); pair<long long, long long> p = *itr; s.erase(itr); long long x = p.second; fr[x] = 1; cout << x << "\n"; for (i = 0; i < v[x].size(); i++) { if (sz[v[x][i]] % 2 == 1 && fr[v[x][i]] == 0) { s.insert({-lev[v[x][i]], v[x][i]}); } else if (s.find({-lev[v[x][i]], v[x][i]}) != s.end()) { s.erase(s.find({-lev[v[x][i]], v[x][i]})); } sz[v[x][i]]--; } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, i, j; long long q = 1; while (q--) solve(); }
#include <bits/stdc++.h> using namespace std; const int MAXN = (int)2e5; vector<int> g[MAXN + 1]; bool dp[MAXN + 1][2]; bool vis[MAXN + 1][2]; bool dfs(int nod, bool t, int p) { if (vis[nod][t]) return dp[nod][t]; int cnt = 0; for (auto it : g[nod]) { if (it != p && 2 * dfs(it, 0, nod) + dfs(it, 1, nod) > 1) cnt++; } vis[nod][t] = 1; if (cnt == 0) { return dp[nod][t] = (t == 0); } if ((cnt + t) % 2 == 1) { for (auto it : g[nod]) { if (it != p && 2 * dp[it][0] + dp[it][1] == 3) { cnt--; break; } } } if ((cnt + t) % 2 == 0) { dp[nod][t] = 1; } return dp[nod][t]; } void solve(int nod, bool t, int p) { int cnt = 0; for (auto it : g[nod]) { if (it != p && 2 * dp[it][0] + dp[it][1] == 1) { solve(it, 1, nod); } if (it != p && 2 * dp[it][0] + dp[it][1] > 1) { cnt++; } } int id = -1; if ((cnt + t) % 2 == 1) { for (auto it : g[nod]) { if (it != p && 2 * dp[it][0] + dp[it][1] == 3) { solve(it, 1, nod); id = it; cnt--; break; } } } cout << nod << "\n"; for (auto it : g[nod]) { if (it != p && it != id && 2 * dp[it][0] + dp[it][1] != 1) { solve(it, 0, nod); } } } int main() { int i, n, x; ios::sync_with_stdio(false); cin >> n; for (i = 1; i <= n; i++) { cin >> x; if (x != 0) { g[x].push_back(i); g[i].push_back(x); } } if (1 - n & 1) { cout << "NO"; return 0; } cout << "YES" << "\n"; dfs(1, 0, 0); solve(1, 0, 0); return 0; }
#include <bits/stdc++.h> using namespace std; const long double PI = 3.141592653589793238462643383; long long int gcd(long long int a, long long int b) { if (a == 0) return b; return gcd(b % a, a); } long long int Ceil(long long int a, long long int b) { if (a % b == 0) return a / b; else return a / b + 1; } const int MAX = 400009; const int MOD = 1e9 + 7; const int inf = 1e9 + 10; long long int deg[MAX], vis[MAX], dep[MAX]; vector<long long int> adj[MAX]; void dfs(int i, int d) { vis[i] = 1; dep[i] = d++; for (int j = 0; j < ((int)((adj[i]).size())); ++j) { int u = adj[i][j]; if (!vis[u]) dfs(u, d); } } priority_queue<pair<long long int, pair<long long int, long long int> > > pq; set<pair<long long int, pair<long long int, long long int> > > st; vector<long long int> ans; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 0; i < (n); ++i) { int p; cin >> p; p--; if (p == -1) continue; adj[i].push_back(p), adj[p].push_back(i); deg[i]++, deg[p]++; } dfs(0, 1); (memset((vis), 0, sizeof(vis))); for (int i = 0; i < (n); ++i) if (deg[i] % 2 == 0) pq.push((make_pair((dep[i]), ((make_pair((i), (deg[i]))))))); long long int cnt = 0; while (!pq.empty()) { pair<long long int, pair<long long int, long long int> > c = pq.top(); pq.pop(); if (st.find(c) != st.end()) { st.erase(c); continue; } int i = c.second.first; vis[i] = 1; cnt++; ans.push_back(i); for (int j = 0; j < ((int)((adj[i]).size())); ++j) { int u = adj[i][j]; if (vis[u]) continue; deg[u]--; if (deg[u] % 2 == 0) pq.push((make_pair((dep[u]), ((make_pair((u), (deg[u]))))))); else st.insert((make_pair((dep[u]), ((make_pair((u), (deg[u] + 1))))))); } } if (cnt == n) { cout << "YES\n"; for (auto&(it) : (ans)) cout << it + 1 << '\n'; } else cout << "NO\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 500000 + 5; int head[maxn], to[maxn << 1], nex[maxn << 1], cnt, degree[maxn], parent[maxn], vis[maxn]; stack<int> dfs; queue<int> answer; inline void add_edge(int u, int v) { nex[++cnt] = head[u]; head[u] = cnt; to[cnt] = v; } void dfs1(int u, int fa) { dfs.push(u); parent[u] = fa; for (int v = head[u]; v; v = nex[v]) if (to[v] != fa) dfs1(to[v], u); } void dfs2(int u) { answer.push(u); vis[u] = 1; for (int v = head[u]; v; v = nex[v]) { --degree[to[v]]; if (to[v] == parent[u] || vis[to[v]]) continue; if (degree[to[v]] % 2 == 0) dfs2(to[v]); } } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) { int a; scanf("%d", &a); if (a == 0) continue; add_edge(i, a); add_edge(a, i); ++degree[a], ++degree[i]; } dfs1(1, 0); while (dfs.size()) { int u = dfs.top(); dfs.pop(); if (degree[u] % 2 == 0) dfs2(u); } if (answer.size() == n) { printf("YES\n"); while (!answer.empty()) { printf("%d\n", answer.front()); answer.pop(); } } else printf("NO"); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; const int INF = 1e9 + 5; int child[MAXN]; bitset<MAXN> done; vector<int> rta; vector<int> G[MAXN]; int dfs1(int st) { done[st] = true; int sum = 0; for (auto &i : G[st]) if (!done[i]) sum += dfs1(i); return child[st] = sum + 1; } void dfs2(int st) { done[st] = true; for (auto &i : G[st]) if (!done[i] && !(child[i] & 1)) dfs2(i); rta.push_back(st); for (auto &i : G[st]) if (!done[i] && (child[i] & 1)) dfs2(i); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ; int n; cin >> n; for (int i = 0; i < int(n); i++) { int x; cin >> x; x--; if (x >= 0) G[i].push_back(x), G[x].push_back(i); } if (!(n & 1)) return cout << "NO", 0; dfs1(0); done.reset(); dfs2(0); cout << "YES\n"; for (auto &i : rta) cout << i + 1 << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; struct Edge { int to, next; } e[404040]; int cnt = 0, head[202020]; void add(int u, int v) { cnt++; e[cnt].to = v; e[cnt].next = head[u]; head[u] = cnt; } int d[202020], d2[202020]; int size[202020]; void dfs(int x, int fa) { size[x] = 1; for (int i = head[x]; i != 0; i = e[i].next) { int to = e[i].to; if (to != fa) { dfs(to, x); size[x] += size[to]; if (size[to] % 2 == 0) printf("%d\n", to); } } } void dfs2(int x, int fa) { if (size[x] % 2 == 1) printf("%d\n", x); for (int i = head[x]; i != 0; i = e[i].next) { int to = e[i].to; if (to != fa) dfs2(to, x); } } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { int pi; scanf("%d", &pi); if (pi != 0) { d[i]++; d[pi]++; add(pi, i); add(i, pi); } } if (n % 2 == 0) { printf("NO\n"); return 0; } printf("YES\n"); dfs(1, 0); dfs2(1, 0); return 0; }
#include <bits/stdc++.h> using namespace std; inline void read(int &x) { char ch; bool flag = false; for (ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') flag = true; for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; x = flag ? -x : x; } inline void read(long long &x) { char ch; bool flag = false; for (ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') flag = true; for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; x = flag ? -x : x; } int n, m; int pre[610000], now[310000], son[610000], tot; int siz[310000]; bool vis[310000]; void build(int a, int b) { pre[++tot] = now[a]; now[a] = tot; son[tot] = b; } int d[310000], w; int du[310000]; void del(int x, int fa) { vis[x] = 1; d[++w] = x; for (int p = now[x]; p; p = pre[p]) if ((son[p] != fa) && (vis[son[p]] == 0)) del(son[p], x); } void dfs(int x, int fa) { int flag = 1; for (int p = now[x]; p; p = pre[p]) if (son[p] != fa) { dfs(son[p], x); if (vis[son[p]] == 0) { siz[x] += siz[son[p]] + 1; if (siz[son[p]] % 2 == 1) flag = 0; } else du[x]--; } if ((flag) && (du[x] % 2 == 0)) del(x, fa); } int main() { read(n); for (int i = 1; i <= n; i++) { int a; read(a); if (a == 0) continue; du[i]++; du[a]++; build(i, a); build(a, i); } if (n % 2 == 0) { puts("NO"); return 0; } dfs(1, 0); puts("YES"); for (int i = 1; i <= w; i++) printf("%d\n", d[i]); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 100; bool vis[N], done[N]; vector<int> res, g[N]; void solve(int u) { res.push_back(u); done[u] = 1; for (int v : g[u]) { if (done[v] == 0) { solve(v); } } } int root; void dfs(int u) { int deg = (u != root); for (int v : g[u]) { dfs(v); deg += (!done[v]); } if (deg % 2 == 0) { solve(u); } } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { int p; cin >> p; if (p) { g[p].push_back(i); } else { root = i; } } if (n % 2 == 0) { cout << "NO" << "\n"; } else { dfs(root); cout << "YES" << "\n"; for (int x : res) { cout << x << " "; } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, tot, e[500001], nt[500001], hd[500001], sz[500001]; void build(int x, int y) { tot++; e[tot] = y; nt[tot] = hd[x]; hd[x] = tot; } void dfs(int x, int fa) { int i; sz[x] = 1; for (i = hd[x]; i; i = nt[i]) { if (e[i] == fa) continue; dfs(e[i], x); sz[x] += sz[e[i]]; } } void get(int x, int fa) { int i; for (i = hd[x]; i; i = nt[i]) { if (e[i] == fa) continue; if (sz[e[i]] % 2 == 0) get(e[i], x); } printf("%d\n", x); for (i = hd[x]; i; i = nt[i]) { if (e[i] == fa) continue; if (sz[e[i]] % 2 == 1) get(e[i], x); } } int main() { int i, x; scanf("%d", &n); if (n % 2 == 0) return puts("NO"), 0; for (i = 1; i <= n; i++) { scanf("%d", &x); if (x) build(x, i), build(i, x); } dfs(1, 0); puts("YES"); get(1, 0); }
#include <bits/stdc++.h> using namespace std; template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>; template <typename T> using maxpq = priority_queue<T, vector<T>, less<T>>; template <typename T1, typename T2> struct pair_hash { size_t operator()(const pair<T1, T2> &p) const { return 31 * hash<T1>{}(p.first) + hash<T2>{}(p.second); } }; int N, dep[200005], deg[200005], root; bool done[200005]; unordered_set<int> adj[200005]; vector<int> ord; set<pair<int, int>, greater<pair<int, int>>> even; void dfs(int v, int prev, int d) { dep[v] = d; for (int w : adj[v]) if (w != prev) dfs(w, v, d + 1); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N; int p; for (auto i = (1); i < (N + 1); i++) { deg[i] = 0; done[i] = false; } for (auto i = (1); i < (N + 1); i++) { cin >> p; if (p == 0) root = i; else { adj[p].insert(i); adj[i].insert(p); deg[p]++; deg[i]++; } } dfs(root, -1, 0); for (auto i = (1); i < (N + 1); i++) if (deg[i] % 2 == 0) even.insert({dep[i], i}); while (!even.empty()) { int v = even.begin()->second; even.erase(even.begin()); ord.push_back(v); done[v] = true; for (int w : adj[v]) { if (--deg[w] % 2 == 0) even.insert({dep[w], w}); else even.erase({dep[w], w}); adj[w].erase(v); } adj[v].clear(); } for (auto i = (1); i < (N + 1); i++) { if (!done[i]) { cout << "NO" << '\n'; return 0; } } cout << "YES" << '\n'; for (int v : ord) cout << v << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, rt, pr[200069], sq[200069], zs = 0; vector<long long> al[200069]; bitset<200069> dg, vtd; void trv(long long x) { long long i, sz = al[x].size(), l; vtd[x] = 1; zs++; sq[zs] = x; for (i = 0; i < sz; i++) { l = al[x][i]; if (!vtd[l]) { trv(l); } } } void dfs(long long x) { long long i, sz = al[x].size(), l; for (i = 0; i < sz; i++) { l = al[x][i]; dfs(l); } if (!dg[x]) { trv(x); dg[pr[x]] = !dg[pr[x]]; } } int main() { long long i; scanf("%lld", &n); for (i = 1; i <= n; i++) { scanf("%lld", pr + i); if (!pr[i]) { rt = i; } else { al[pr[i]].push_back(i); dg[i] = !dg[i]; dg[pr[i]] = !dg[pr[i]]; } } dfs(rt); if (zs < n) { printf("NO\n"); } else { printf("YES\n"); for (i = 1; i <= zs; i++) { printf("%lld\n", sq[i]); } } }
#include <bits/stdc++.h> using namespace std; const int N = 2e5, M = 4e5; int head[N + 10], ver[M + 10], nxt[M + 10], tot, sz[N + 10]; void add(int x, int y) { ver[++tot] = y; nxt[tot] = head[x]; head[x] = tot; ver[++tot] = x; nxt[tot] = head[y]; head[y] = tot; } void dfs1(int x, int fa) { sz[x] = 1; for (int i = head[x]; i; i = nxt[i]) { int y = ver[i]; if (y == fa) continue; dfs1(y, x); sz[x] += sz[y]; } } int ans[N + 10], cnt = 0; void dfs2(int x, int fa) { vector<int> odd, even; for (int i = head[x]; i; i = nxt[i]) { int y = ver[i]; if (y == fa) continue; if (sz[y] & 1) odd.push_back(y); else even.push_back(y); } for (int i = 0; i < even.size(); i++) { int y = even[i]; dfs2(y, x); } ans[++cnt] = x; for (int i = 0; i < odd.size(); i++) { int y = odd[i]; dfs2(y, x); } } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); if (!x) continue; add(i, x); } dfs1(1, -1); if (!(sz[1] & 1)) printf("NO"); else { printf("YES\n"); dfs2(1, -1); for (int i = 1; i <= cnt; i++) printf("%d\n", ans[i]); } return 0; }
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; vector<set<int>> graph; vector<bool> seen; vector<bool> removed; vector<int> removedOrder; void dfs(int from) { seen[from] = true; set<int> edges = graph[from]; for (auto to : edges) { if (!seen[to]) dfs(to); } const int deg = graph[from].size(); if (deg % 2 == 0 && !removed[from]) { removed[from] = true; removedOrder.push_back(from); for (auto to : graph[from]) graph[to].erase(from); graph[from].clear(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; graph.assign(n, set<int>()); for (int i = 0; i < n; ++i) { int p; cin >> p; if (p) { --p; graph[i].insert(p); graph[p].insert(i); } } removed.assign(n, false); for (int i = 0; i < n; ++i) { if (!removed[i]) { seen.assign(n, false); dfs(i); } } if (removedOrder.size() == n) { cout << "YES\n"; for (auto v : removedOrder) cout << v + 1 << '\n'; } else { cout << "NO\n"; } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int MOD = 1000000007; vector<int> vec[200005]; int d[200005], deg[200005]; void dfs(int node, int p) { for (auto it : vec[node]) if (it != p) d[it] = d[node] + 1, dfs(it, node); } struct cmp { bool operator()(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) const { if ((a.first.first & 1) != (b.first.first & 1)) return (a.first.first & 1) < (b.first.first & 1); else if (a.first.second != b.first.second) return a.first.second > b.first.second; else return a.second < b.second; } }; set<pair<pair<int, int>, int>, cmp> myset; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, a; cin >> n; for (int i = 0; i < n; ++i) { cin >> a, a--; if (a == -1) continue; vec[i].push_back(a); vec[a].push_back(i); } dfs(0, 0); for (int i = 0; i < n; ++i) { myset.insert({{(long long)vec[i].size(), d[i]}, i}); deg[i] = (long long)vec[i].size(); } vector<int> ans; while (!myset.empty()) { auto it = myset.begin(); if (it->first.first & 1) { cout << "NO\n"; return 0; } ans.push_back(it->second); for (auto node : vec[it->second]) if (myset.find({{deg[node], d[node]}, node}) != myset.end()) { myset.erase({{deg[node], d[node]}, node}); deg[node]--; myset.insert({{deg[node], d[node]}, node}); } myset.erase(it); } cout << "YES\n"; for (auto it : ans) cout << it + 1 << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; char seen[201010]; vector<int> g[201010]; int deg[201010], lvl[201010]; int n, x; set<pair<int, int>, greater<pair<int, int> > > par; void dfs(int u, int p) { lvl[u] = lvl[p] + 1; for (int v : g[u]) { if (v != p) dfs(v, u); } } int main() { int u; cin >> n; int r; for (int i = 1; i <= n; i++) { cin >> x; if (x) { g[x].push_back(i); g[i].push_back(x); deg[x]++; deg[i]++; } else r = i; } dfs(r, r); for (int i = 1; i <= n; i++) if (deg[i] % 2 == 0) par.insert(make_pair(lvl[i], i)); vector<int> ans; while (par.size()) { pair<int, int> tmp = *par.begin(); par.erase(par.begin()); u = tmp.second; seen[u] = 1; ans.push_back(u); for (int v : g[u]) { if (seen[v]) continue; deg[v]--; if (deg[v] % 2 == 0) par.insert(make_pair(lvl[v], v)); else par.erase(make_pair(lvl[v], v)); } } if (ans.size() == n) { cout << "YES\n"; for (int i = 0; i < ans.size(); i++) cout << ans[i] << "\n"; } else cout << "NO\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000 * 1000 * 1000 + 7; const int N = 2e5 + 6; int n; vector<int> g[N], ng[N], ans; void dest(int v) { ans.push_back(v); for (auto to : ng[v]) { dest(to); } } bool dfs(int v, int root = false) { for (auto to : g[v]) { if (!dfs(to)) ng[v].push_back(to); } if ((ng[v].size() + root) % 2) { dest(v); return true; } return false; } int main() { mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); cin >> n; if (n % 2 == 0) { cout << "NO" << endl; return 0; } cout << "YES" << endl; int root; for (int i = (1); i <= (n); ++i) { int p; cin >> p; if (!p) root = i; g[p].push_back(i); } dfs(root, true); for (int i = (0); i <= (n - 1); ++i) { cout << ans[i] << endl; assert(i < ans.size()); } return 0; }
#include <bits/stdc++.h> using namespace std; void get_destructibility(int v, int par, vector<set<int> >& edges, vector<bool>& is_destructible) { is_destructible[v] = true; for (int w : edges[v]) { if (w != par) { get_destructibility(w, v, edges, is_destructible); if (is_destructible[w]) { is_destructible[v] = !is_destructible[v]; } } } } void get_destruction_order(int v, int par, vector<set<int> >& edges, vector<bool>& is_destructible, list<int>& destruction_order) { for (int w : edges[v]) { if (w != par && !is_destructible[w]) { get_destruction_order(w, v, edges, is_destructible, destruction_order); } } destruction_order.push_back(v); for (int w : edges[v]) { if (w != par && is_destructible[w]) { get_destruction_order(w, v, edges, is_destructible, destruction_order); } } } int main() { int n; cin >> n; vector<set<int> > edges(n); for (int i = 0; i < n; i++) { int p; cin >> p; if (p) { edges[i].insert(p - 1); edges[p - 1].insert(i); } } vector<bool> is_destructible(n); get_destructibility(0, -1, edges, is_destructible); if (is_destructible[0]) { cout << "YES\n"; list<int> destruction_order; get_destruction_order(0, -1, edges, is_destructible, destruction_order); for (int v : destruction_order) { cout << v + 1 << '\n'; } } else { cout << "NO\n"; } }
#include <bits/stdc++.h> const int MAXN = 2e5 + 5; int n, root, p[MAXN], mem[MAXN][2], vis[MAXN]; std::vector<int> adj[MAXN], ch[MAXN][2][2]; std::queue<int> order; int solve(int u, int x) { if (mem[u][x] != -1) return mem[u][x]; if (!vis[u]) { for (int v : adj[u]) { int a = solve(v, 0), b = solve(v, 1); ch[u][a][b].push_back(v); } vis[u] = 1; } if (!ch[u][0][0].empty()) mem[u][x] = 0; else if ((ch[u][1][1].size() + ch[u][1][0].size() + x) % 2 == 0) mem[u][x] = 1; else if (!ch[u][1][1].empty()) mem[u][x] = 1; else mem[u][x] = 0; return mem[u][x]; } void reconstruct(int u, int x) { for (int v : ch[u][0][1]) reconstruct(v, 1); if ((ch[u][1][1].size() + ch[u][1][0].size() + x) % 2 == 0) { order.push(u); for (int v : ch[u][1][1]) reconstruct(v, 0); for (int v : ch[u][1][0]) reconstruct(v, 0); } else { reconstruct(ch[u][1][1][0], 1); order.push(u); for (size_t i = 1; i < ch[u][1][1].size(); ++i) { reconstruct(ch[u][1][1][i], 0); } for (int v : ch[u][1][0]) reconstruct(v, 0); } } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { int par; scanf("%d", &par); if (par == 0) root = i; adj[par].push_back(i); } memset(mem, -1, sizeof mem); if (solve(root, 0)) { printf("YES\n"); reconstruct(root, 0); for (; !order.empty(); order.pop()) printf("%d ", order.front()); printf("\n"); } else { printf("NO\n"); } }
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; const int mod = inf + 7; const double eps = 1e-9; const double pi = acos(-1.0); int n; vector<int> g[200200]; int dp[2][200200]; int calc(int has, int v, int par) { int &res = dp[has][v]; if (res != -1) return res; vector<int> childs; for (__typeof(g[v].begin()) it = g[v].begin(); it != g[v].end(); it++) { if ((*it) != par) childs.push_back(*it); } if (childs.empty()) { if (has) return res = 0; return res = 1; } res = 0; if ((has + (int)childs.size()) % 2 == 0) { int cur = 1; for (__typeof(childs.begin()) it = childs.begin(); it != childs.end(); it++) cur &= calc(0, *it, v); if (cur) return res = 1; } vector<int> good, bad; for (__typeof(childs.begin()) it = childs.begin(); it != childs.end(); it++) { if (calc(0, *it, v)) good.push_back(*it); else bad.push_back(*it); } for (__typeof(bad.begin()) it = bad.begin(); it != bad.end(); it++) { if (!calc(1, *it, v)) return res = 0; } int k = has + (int)childs.size() - (int)bad.size(); if (k % 2 == 0) return res = 1; for (__typeof(good.begin()) it = good.begin(); it != good.end(); it++) { if (calc(1, *it, v)) return res = 1; } return res = 0; } void rec(int has, int v, int par) { vector<int> childs; for (__typeof(g[v].begin()) it = g[v].begin(); it != g[v].end(); it++) { if ((*it) != par) childs.push_back(*it); } if (childs.empty()) { printf("%d\n", v + 1); return; } if ((has + (int)childs.size()) % 2 == 0) { int cur = 1; for (__typeof(childs.begin()) it = childs.begin(); it != childs.end(); it++) cur &= calc(0, *it, v); if (cur) { printf("%d\n", v + 1); for (__typeof(childs.begin()) it = childs.begin(); it != childs.end(); it++) rec(0, *it, v); return; } } vector<int> good, bad; for (__typeof(childs.begin()) it = childs.begin(); it != childs.end(); it++) { if (calc(0, *it, v)) good.push_back(*it); else bad.push_back(*it); } for (__typeof(bad.begin()) it = bad.begin(); it != bad.end(); it++) { rec(1, *it, v); } int k = has + (int)childs.size() - (int)bad.size(); int pos = -1; if (k % 2 == 1) { pos = 0; while (!calc(1, good[pos], v)) pos++; rec(1, good[pos], v); } printf("%d\n", v + 1); for (int i = 0; i < (int)good.size(); i++) { if (i == pos) continue; rec(0, good[i], v); } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); x--; if (x != -1) { g[i].push_back(x); g[x].push_back(i); } } memset(dp, -1, sizeof dp); if (calc(0, 0, -1)) { cout << "YES" << endl; rec(0, 0, -1); } else cout << "NO" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> graph[200002]; int visited[200002], sub[200002]; int subsize(int root) { visited[root] = 1; for (int child : graph[root]) { if (!visited[child]) sub[root] += subsize(child); } return sub[root]; } void destroy(int root, int par) { for (int child : graph[root]) { if (child != par && (sub[child] % 2 == 0)) destroy(child, root); } printf("%d\n", root); for (int child : graph[root]) { if (child != par && (sub[child] & 1)) destroy(child, root); } } int main() { for (int i = 0; i < 200002; i++) sub[i] = 1; int n, x; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &x); if (x) { graph[x].push_back(i); graph[i].push_back(x); } } if (n % 2 == 0) printf("NO"); else { subsize(1); printf("YES\n"); destroy(1, 0); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5, M = 1e9 + 9; const double PI = acos(-1); int n, dest[N]; vector<int> v[N], ans; void go(int u, int p) { dest[u] = 1; ans.push_back(u); for (int i = 0; i < v[u].size(); i++) { int x = v[u][i]; if (x != p && !dest[x]) go(x, u); } } int dfs(int u, int p) { int cnt = 0; for (int i = 0; i < v[u].size(); i++) { int x = v[u][i]; if (x != p) cnt ^= dfs(x, u); } if (p != -1) cnt ^= 1; if (!cnt) go(u, p); return cnt; } int main() { scanf("%d", &n); for (int a, i = 0; i < n; i++) { scanf("%d", &a); if (a) v[--a].push_back(i), v[i].push_back(a); } if (!(n & 1)) return puts("NO"), 0; dfs(0, -1); puts("YES"); for (int i = 0; i < n; i++) printf("%d\n", ans[i] + 1); }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const int maxn = 2e5 + 5; vector<int> a[maxn]; int dp[maxn]; int vis[maxn]; vector<int> ans; void getans(int t, int pre) { if (vis[t]) return; vis[t] = 1; ans.push_back(t); for (auto it : a[t]) { if (it == pre) continue; getans(it, t); } } int dfs(int t, int pre) { int flag = 0; if (pre != -1 && a[t].size() == 1) { dp[t] = 1; return 1; } int cnt = 0; for (auto it : a[t]) { if (it == pre) continue; dfs(it, t); if (dp[it] == 1) cnt++; } if (pre != -1) cnt++; if (cnt & 1) { dp[t] = 1; return 1; } else { dp[t] = 0; getans(t, pre); return 0; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int i, j, k, m, n; cin >> n; for (int i = 1; i <= n; ++i) { cin >> k; if (k == 0) continue; a[i].push_back(k); a[k].push_back(i); } int t = dfs(1, -1); getans(1, -1); if (t == 1) { cout << "NO" << endl; return 0; } cout << "YES" << endl; for (auto it : ans) cout << it << " "; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 4e5 + 5; int last[MAXN + 5], Check[MAXN + 5]; int other[MAXN + 5], pre[MAXN + 5]; int num[MAXN + 5]; int rlast[MAXN + 5], rpre[MAXN + 5]; int rother[MAXN + 5]; int P[MAXN + 5]; queue<int> seq; int all = -1, rall = -1; int n; void build(int u, int v) { pre[++all] = last[u]; last[u] = all; other[all] = v; } void rbuild(int u, int v) { rpre[++rall] = rlast[u]; rlast[u] = rall; rother[rall] = v; } int dfs(int ans, int fa) { int sum = 0, t = 0; int dt = last[ans]; while (dt != -1) { int dr = other[dt]; if (dr != fa) { int c = dfs(dr, ans); if (dr != 0 && ans != 0) { if (c & 1) rbuild(dr, ans), num[ans]++; else rbuild(ans, dr), num[dr]++; } sum += c, t++; } dt = pre[dt]; } if ((t - sum) & 1) return 1; return 0; } void solve() { int tot = 0; for (int i = 1; i <= n; i++) if (num[i] == 0 && Check[i] % 2 == 0) P[++tot] = i, seq.push(i); while (seq.size()) { int ans = seq.front(); int dt = last[ans]; while (dt != -1) { int dr = other[dt]; Check[dr]--; dt = pre[dt]; } int rdt = rlast[ans]; while (rdt != -1) { int rdr = rother[rdt]; if (--num[rdr] == 0 && Check[rdr] % 2 == 0) seq.push(rdr), P[++tot] = rdr; rdt = rpre[rdt]; } seq.pop(); } if (tot != n) printf("NO\n"); else { printf("YES\n"); for (int i = 1; i <= tot; i++) printf("%d\n", P[i]); } } int main() { scanf("%d", &n); memset(last, -1, sizeof(last)); memset(rlast, -1, sizeof(rlast)); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); build(x, i), build(i, x); if (x != 0) Check[x]++, Check[i]++; } dfs(0, -1); solve(); }