이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//: // ////: /// / : //// / :
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pll;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl;
const ll MAXN = 1e6 + 10;
int n, tin[MAXN], t;
vector<int> adj[MAXN], L;
vector<pll> ans;
void dfs(int v, int p) {
tin[v] = ++t;
if (adj[v].size() == 1)
L.push_back(v);
for (int u : adj[v])
if (u != p)
dfs(u, v);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
if (adj[i].size() > 1) {
dfs(i, 0);
break;
}
}
sort(all(L), [] (int a, int b) {
return tin[a] < tin[b];
});
for (int i = 0; i < L.size() / 2; i++)
ans.push_back({L[i], L[i + L.size() / 2]});
if (L.size() & 1) ans.push_back({L.back(), L.front()});
cout << ans.size() << endl;
for (pll e : ans) cout << e.X << sep << e.Y << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
net.cpp: In function 'int main()':
net.cpp:54:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int i = 0; i < L.size() / 2; i++)
| ~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |