Submission #797485

#TimeUsernameProblemLanguageResultExecution timeMemory
797485bonkNetwork (BOI15_net)C++14
100 / 100
375 ms56520 KiB
#include <bits/stdc++.h>

using namespace std;

const int N = 5e5 + 2;
vector<int>adj[N];
vector<pair<int, int>>t;
int timer = 0;

void dfs(int prev, int cur){
    timer++;
    bool leaf = true;
    for(auto &nxt: adj[cur]){
        if(prev == nxt) continue;
        leaf = false;
        dfs(cur, nxt);
    }

    if(leaf) t.emplace_back(timer, cur);
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n; cin >> n;
    for(int i = 0; i < n - 1; i++){
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }


    for(int i = 1; i <= n; i++){
        if(adj[i].size() > 1){
            dfs(-1, i);
            break;
        }
    }

    sort(t.begin(), t.end());
    cout << (t.size() + 1)/2 << '\n';
    int half = t.size()/2;
    for(int i = 0; i < half; i++){
        cout << t[i].second << " " << t[i + half].second << '\n';
    }

    if(half*2 != t.size()){
        cout << t[0].second << " " << t.back().second << '\n';
    }

    return 0;
}

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:46:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |     if(half*2 != t.size()){
      |        ~~~~~~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...