# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
655830 | as111 | Network (BOI15_net) | C++14 | 573 ms | 47704 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.
//simple dfs
#include <vector>
#include <iostream>
#define MAXN 500000
#define pb push_back
#define fi first
#define se second
using namespace std;
vector<int> adj[MAXN + 5];
vector<int> leaf;
void dfs(int u, int par)
{
if (adj[u].size() == 1) leaf.pb(u);
for (int v : adj[u])
{
if (v == par) continue;
dfs(v, u);
}
}
void Solve()
{
dfs(1, 0);
int l = 0, step = (int)(leaf.size() / 2);
if (leaf.size() % 2 == 0)
{
cout << leaf.size() / 2 << '\n';
while (l + step < (int)leaf.size())
{
cout << leaf[l] << " " << leaf[l + step] << '\n';
l++;
}
return;
}
leaf.pb(leaf[0]);
step++;
cout << leaf.size() / 2 << '\n';
while (l + step < leaf.size())
{
cout << leaf[l] << " " << leaf[l + step] << '\n';
l++;
}
}
int main() {
int N;
cin >> N;
for (int i = 1; i < N; i++)
{
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
Solve();
}
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... |