Submission #959606

# Submission time Handle Problem Language Result Execution time Memory
959606 2024-04-08T13:40:53 Z Blagoj Network (BOI15_net) C++17
0 / 100
4 ms 12196 KB
#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define ll long long
#define all(x) (x).begin(), (x).end()

const int mxn = 5e5 + 10;

vector<int> g[mxn], leafs;

void dfs(int cur, int par) {
    bool leaf = 1;
    for (auto x : g[cur]) {
        if (x == par) continue;
        leaf = 0;
        dfs(x, cur);
    }
    if (leaf) leafs.push_back(cur);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int f, t;
        cin >> f >> t;
        g[f].push_back(t);
        g[t].push_back(f);
    }
    int root;
    for (int i = 1; i <= n; i++) {
        if (g[i].size() >= 2) {
            root = i;
            break;
        }
    }
    dfs(root, root);
    cout << leafs.size() / 2 + (leafs.size() % 2 != 0) << endl;
    for (int i = 0; i < leafs.size() - 1; i += 2) cout << leafs[i] << " " << leafs[i + 1] << endl;
    if (leafs.size() % 2 == 1) cout << leafs[0] << " " << leafs[leafs.size() - 1] << endl;
}

Compilation message

net.cpp: In function 'int main()':
net.cpp:43:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |     for (int i = 0; i < leafs.size() - 1; i += 2) cout << leafs[i] << " " << leafs[i + 1] << endl;
      |                     ~~^~~~~~~~~~~~~~~~~~
net.cpp:41:8: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
   41 |     dfs(root, root);
      |     ~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12124 KB Output is correct
2 Incorrect 4 ms 12196 KB Breaking single line is causing network to disconnect.
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12124 KB Output is correct
2 Incorrect 4 ms 12196 KB Breaking single line is causing network to disconnect.
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12124 KB Output is correct
2 Incorrect 4 ms 12196 KB Breaking single line is causing network to disconnect.
3 Halted 0 ms 0 KB -