# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
503951 | goodluck2020 | Network (BOI15_net) | C++14 | 2 ms | 2640 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>
#define task "BOI15_net"
#define sz(X) ((int)X.size())
using namespace std;
const int N = 1e5 + 5;
int n;
vector < int > adj[N], Leaf;
vector < pair < int , int > > res;
void DFS(int u, int par)
{
if(sz(adj[u]) == 1) Leaf.push_back(u);
for(int v : adj[u])
if(v != par) DFS(v, u);
}
int main()
{
if(fopen(task ".inp","r"))
{
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
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);
}
DFS(1, 0);
for(int i = 1; i < sz(Leaf); i += 2)
res.push_back({Leaf[i], Leaf[i - 1]});
if(sz(Leaf) % 2 == 1) res.push_back({1, Leaf.back()});
cout << sz(res) << '\n';
for(auto it : res) cout << it.first << " " << it.second << '\n';
}
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... |