Submission #677199

#TimeUsernameProblemLanguageResultExecution timeMemory
677199dooompyNetwork (BOI15_net)C++17
100 / 100
416 ms48164 KiB
#include "bits/stdc++.h"
 
using namespace std;
 
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
    cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
    while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
    return o >> a.first >> a.second;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
    return o << '(' << a.first << ", " << a.second << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
    bool is = false;
    for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
    return o << '}';
}
 
#ifdef local
#define test(args...) abc("[" + string(#args) + "]", args)
#else
#define test(args...) void(0)
#endif
 
using ll = long long;
 
vector<int> adj[500005];
bool seen[500005];
vector<int> order;
 
void dfs(int node) {
    seen[node] = true;
    bool leaf = true;
    for (auto nxt : adj[node]) {
        if (seen[nxt]) continue;
        dfs(nxt);
        leaf = false;
    }
    if (leaf) order.push_back(node);
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
//    freopen("", "r", stdin);
//    freopen("", "w", stdout);
    int n; cin >> n;
    for (int i = 0; i < n-1; i++) {
        int a, b; cin >>a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
 
    for (int i = 1; i <= n; i++) {
        if (adj[i].size() >= 2) {
            dfs(i);
            break;
        }
    }
 
    int s = order.size();
 
    cout << ceil(s / 2.0) << endl;
 
    for (int i = 0; i < (s + 1) /2; i++) {
        cout << order[i] << " " << order[min(i + s/2, s-1)] << "\n";
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...