This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define lesgooo ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl '\n'
#define int long long
vector<int> adj[2006];
int dist[2006][2006];
void dfs(int i, int sum, int src, int par)
{
dist[src][i] = sum;
for (auto nxt : adj[i]) if (nxt != par) dfs(nxt, sum+1, src, i);
}
signed main()
{
lesgooo;
int n, u, v;
cin >> n;
for (int i = 1; i < n; i++) cin >> u >> v, adj[--u].push_back(--v), adj[v].push_back(u);
set<int> leaf;
for (int i = 0;i < n; i++)
{
dfs(i, 0, i, -1);
if (adj[i].size() == 1) leaf.insert(i);
}
cout << (leaf.size()+1)/2 << endl;
int temp = *leaf.begin();
while (leaf.size() > 1)
{
int cur = *leaf.begin(), mx = 0, x;
for (int i = 0; i < n; i++) if (i != cur && leaf.find(i) != leaf.end() && dist[cur][i] >= mx) mx = dist[cur][i], x = i;
cout << cur+1 << " " << x+1 << endl;
leaf.erase(leaf.begin()), leaf.erase(x);
}
if (leaf.size()) cout << *leaf.begin()+1 << " " << (*leaf.begin()+1)%n+1 << endl, leaf.erase(leaf.begin());
return 0;
}
Compilation message (stderr)
net.cpp: In function 'int main()':
net.cpp:27:9: warning: unused variable 'temp' [-Wunused-variable]
27 | int temp = *leaf.begin();
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |