Submission #291593

#TimeUsernameProblemLanguageResultExecution timeMemory
291593FischerNetwork (BOI15_net)C++14
0 / 100
11 ms12032 KiB
#include <bits/stdc++.h>
using namespace std;

const int maxn = 5e5 + 10;
vector<int> g[maxn];
int n;

void dfs(vector<int>& nodes, int x, int p=0) {
    if (g[x].size() == 1) nodes.emplace_back(x);
    for (int v : g[x]) {
        if (v==p) continue;
        dfs(nodes, v, x);
    }
}

int main() {
    scanf("%d", &n);
    for (int i=0; i<n-1; ++i) {
        int a, b;
        scanf("%d%d", &a, &b);
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }
    vector<int> nodes;
    int root;
    for (int i=1; i<=n; ++i) {
        if (g[i].size()>1) {
            dfs(nodes, root=i);
            break;
        }
    }
    cout << (nodes.size() + 1) / 2 << endl;
    for (int i = 0; i < nodes.size() / 2; ++i) {
        cout << nodes[i] << " " << nodes[nodes.size() - i - 1] << endl;
    }
    if (nodes.size() & 1) cout << root << " " << nodes[nodes.size() / 2] << endl;
    return 0;
}

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:33:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |     for (int i = 0; i < nodes.size() / 2; ++i) {
      |                     ~~^~~~~~~~~~~~~~~~~~
net.cpp:17:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   17 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
net.cpp:20:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   20 |         scanf("%d%d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~
net.cpp:36:43: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
   36 |     if (nodes.size() & 1) cout << root << " " << nodes[nodes.size() / 2] << endl;
      |                                           ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...