Submission #672875

#TimeUsernameProblemLanguageResultExecution timeMemory
672875iosif_andrei_Network (BOI15_net)C++14
100 / 100
632 ms65612 KiB
#include <bits/stdc++.h> 
using namespace std;

int n;
vector <int> g[500001];
vector <int> v; // leaves
bitset <500001> f;

void dfs(int nod)
{
    f[nod] = true;

    if (g[nod].size() == 1)
        v.push_back(nod);

    for (auto& i : g[nod])
        if (!f[i])
            dfs(i);
}

int main() {
    
    cin >> n;

    for (int i = 1; i < n; i++)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    int root;
    for (int i = 1; i <= n; i++)
        if (g[i].size() == 1)
        {
            root = i;
            break;
        }

    dfs(root);

    cout << (v.size() + 1) / 2 << '\n';

    int i, j;

    if (v.size() % 2)
    {
        i = 0, j = v.size() / 2;

        while (i < v.size() / 2)
        {
            cout << v[i] << ' ' << v[j] << '\n';
            i++, j++;
        }

        cout << v.back() << ' ' << v[0];
    }
    else
    {
        i = 0, j = v.size() / 2;

        while (i < v.size() / 2)
        {
            cout << v[i] << ' ' << v[j] << '\n';
            i++, j++;
        }
    }

    return 0;
}

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:51:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |         while (i < v.size() / 2)
      |                ~~^~~~~~~~~~~~~~
net.cpp:63:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |         while (i < v.size() / 2)
      |                ~~^~~~~~~~~~~~~~
net.cpp:41:8: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
   41 |     dfs(root);
      |     ~~~^~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...