이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) (x).begin(), (x).end()
const int mxn = 5e5 + 10;
vector<int> g[mxn], leafs;
void dfs(int cur, int par) {
bool leaf = 1;
for (auto x : g[cur]) {
if (x == par) continue;
leaf = 0;
dfs(x, cur);
}
if (leaf) leafs.push_back(cur);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int f, t;
cin >> f >> t;
g[f].push_back(t);
g[t].push_back(f);
}
int root;
for (int i = 1; i <= n; i++) if (g[i].size() >= 2) root = i;
dfs(root, root);
int sz = leafs.size();
vector<pair<int, int>> v;
for (int i = 0; i < sz / 2; i++) v.push_back({leafs[i], leafs[i + sz / 2]});
if (sz % 2 != 0) v.push_back({leafs[0], leafs.back()});
cout << v.size() << endl;
for (auto x : v) cout << x.first << " " << x.second << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
net.cpp: In function 'int main()':
net.cpp:36:8: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
36 | dfs(root, root);
| ~~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |