Submission #482470

#TimeUsernameProblemLanguageResultExecution timeMemory
482470SaacootaNetwork (BOI15_net)C++14
100 / 100
431 ms66836 KiB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 5e5 + 10;

vector < int > g[maxn] , leaf;
int cnt[maxn] , par[maxn];

void DFS(int u,int p) {
    if (g[u].size() == 1) leaf.push_back(u);
    for (auto v : g[u])
    if (v != p) {
        par[v] = u;
        DFS(v , u);
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int n , root;
    cin >> n;
    for (int i = 1 ; i < n ; ++i) {
        int u , v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
        if (g[u].size() > 1) root = u;
        if (g[v].size() > 1) root = v;
    }
    DFS(root , 0);
    int half = leaf.size() / 2;
    cout << (leaf.size() + 1) / 2 << '\n';
    for (int j = 0 ; j < (leaf.size() + 1) / 2 ; ++j)
    cout << leaf[j] << ' ' << leaf[half + j] << '\n';
}

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:35:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |     for (int j = 0 ; j < (leaf.size() + 1) / 2 ; ++j)
      |                      ~~^~~~~~~~~~~~~~~~~~~~~~~
net.cpp:32:8: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
   32 |     DFS(root , 0);
      |     ~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...