# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
672875 | iosif_andrei_ | Network (BOI15_net) | C++14 | 632 ms | 65612 KiB |
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;
int n;
vector <int> g[500001];
vector <int> v; // leaves
bitset <500001> f;
void dfs(int nod)
{
f[nod] = true;
if (g[nod].size() == 1)
v.push_back(nod);
for (auto& i : g[nod])
if (!f[i])
dfs(i);
}
int main() {
cin >> n;
for (int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
int root;
for (int i = 1; i <= n; i++)
if (g[i].size() == 1)
{
root = i;
break;
}
dfs(root);
cout << (v.size() + 1) / 2 << '\n';
int i, j;
if (v.size() % 2)
{
i = 0, j = v.size() / 2;
while (i < v.size() / 2)
{
cout << v[i] << ' ' << v[j] << '\n';
i++, j++;
}
cout << v.back() << ' ' << v[0];
}
else
{
i = 0, j = v.size() / 2;
while (i < v.size() / 2)
{
cout << v[i] << ' ' << v[j] << '\n';
i++, j++;
}
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |