# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
113191 | davitmarg | Network (BOI15_net) | C++17 | 705 ms | 43380 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.
/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <ctype.h>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin (),v.end()
using namespace std;
int n,used[500005],start;
vector<int> g[500005],t;
vector<pair<int, int>> ans;
void dfs(int v, int d = 0)
{
used[v] = 1;
for (int i = 0; i < g[v].size(); i++)
{
int to = g[v][i];
if (used[to])
continue;
if (g[to].size() == 1)
t.PB(to);
}
for (int i = 0; i < g[v].size(); i++)
{
int to = g[v][i];
if (used[to] || g[to].size() == 1)
continue;
dfs(to,d+1);
}
}
int main()
{
cin >> n;
if (n == 0)
{
printf("0\n");
return 0;
}
else if (n == 2)
{
printf("1\n");
printf("1 2\n");
return 0;
}
for (int i = 1; i < n; i++)
{
int a, b;
scanf("%d%d", &a, &b);
g[a].PB(b);
g[b].PB(a);
}
for(int i=1;i<=n;i++)
if (g[i].size() != 1)
{
start = i;
break;
}
dfs(start);
for (int i = 0; i + t.size() / 2 < t.size(); i++)
ans.PB(MP(t[i], t[i+ t.size() / 2]));
printf("%d\n",(int)ans.size());
for (int i = 0; i < ans.size(); i++)
printf("%d %d\n", ans[i].first, ans[i].second);
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... |