vector<vector<Node>> g; vector<int> ret; int down = 0, up = 0; int ans = 0; voiddfs1(int u, int fa) { for (int i = 0; i < g[u].size(); ++i) { int v = g[u][i].v, w = g[u][i].w; if (v == fa) continue; if (w == 1) down++; else up++; dfs1(v, u); } } voiddfs2(int u, int fa) { for (int i = 0; i < g[u].size(); ++i) { int v = g[u][i].v, w = g[u][i].w; if (v == fa) continue; if (w == 1) ret[v] = ret[u] + 1; else ret[v] = ret[u] - 1; dfs2(v, u); } } vector<int> minEdgeReversals(int n, vector<vector<int>> &edges) { g.resize(n); ret.resize(n, 0); for (auto &edge : edges) { int u = edge[0], v = edge[1]; g[u].push_back({v, 1}); g[v].push_back({u, -1}); } dfs1(0, -1); ret[0] = up; dfs2(0, -1); return ret; } };