# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
705324 | anha3k25cvp | Network (BOI15_net) | C++14 | 411 ms | 49836 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 ll long long
#define dl double
#define st first
#define nd second
#define II pair <int, int>
using namespace std;
const int N = 1 + 1e5;
const int inf = 7 + 1e9;
vector <int> h, q;
vector <vector <int>> g;
void dfs(int u) {
if (g[u].size() == 1)
q.push_back(u);
for (int v : g[u])
if (!h[v]) {
h[v] = h[u] + 1;
dfs(v);
}
}
int main() {
#define TASKNAME "net"
ios_base :: sync_with_stdio (0);
cin.tie (0);
if ( fopen( TASKNAME".inp", "r" ) ) {
freopen( TASKNAME".inp", "r", stdin );
freopen( TASKNAME".out", "w", stdout );
}
int n;
cin >> n;
g.resize(n + 1);
for (int i = 1; i < n; i ++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
h.assign(n + 1, 0);
h[1] = 1;
dfs(1);
int m = q.size();
vector <II> ans;
for (int i = 0; i < m / 2; i ++)
ans.push_back({q[i], q[(m / 2) + i]});
if (m & 1)
ans.push_back({q[0], q.back()});
cout << ans.size() << '\n';
for (auto z : ans)
cout << z.st << ' ' << z.nd << '\n';
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... |