标签:ons end code com second 图片 color lse img
题解:构造策略,将最大值n当作根,其它出现的节点作为叶子节点,然后计算叶子节点出现了几次,则对应这条链上应该有几个节点。
int n; int use[1005]; P ans[1004]; int main() { cin >> n; vector<int> a; rep(i, 1, n) { int u, v; cin >> u >> v; if (!use[u]) a.pb(u); use[u]++; use[v]++; } sort(Range(a)); if (use[n] != n - 1) { puts("NO"); } else { int cnt = 0; bool flag = false; for (auto v : a) { int t = use[v] - 1, tt = 0, last = n; rep(i, 1, v) if (!use[i] && tt < t) { tt++; use[i] = 1; ans[++cnt] = P(last, i); last = i; } ans[++cnt] = P(last, v); if (tt != t) { flag = true; break; } } if (flag) { puts("NO"); } else { puts("YES"); rep(i, 1, n) cout << ans[i].first << " " << ans[i].second << endl; } } return 0; }
Tree Reconstruction - codeforce
标签:ons end code com second 图片 color lse img
原文地址:https://www.cnblogs.com/zgglj-com/p/9682795.html