Submission #705324

#TimeUsernameProblemLanguageResultExecution timeMemory
705324anha3k25cvpNetwork (BOI15_net)C++14
100 / 100
411 ms49836 KiB
#include <bits/stdc++.h>
#define ll long long
#define dl double
#define st first
#define nd second
#define II pair <int, int>

using namespace std;

const int N = 1 + 1e5;
const int inf = 7 + 1e9;

vector <int> h, q;
vector <vector <int>> g;

void dfs(int u) {
    if (g[u].size() == 1)
        q.push_back(u);
    for (int v : g[u])
        if (!h[v]) {
            h[v] = h[u] + 1;
            dfs(v);
        }
}

int main() {
#define TASKNAME "net"
    ios_base :: sync_with_stdio (0);
    cin.tie (0);
    if ( fopen( TASKNAME".inp", "r" ) ) {
        freopen( TASKNAME".inp", "r", stdin );
        freopen( TASKNAME".out", "w", stdout );
    }
    int n;
    cin >> n;
    g.resize(n + 1);
    for (int i = 1; i < n; i ++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    h.assign(n + 1, 0);
    h[1] = 1;
    dfs(1);
    int m = q.size();
    vector <II> ans;
    for (int i = 0; i < m / 2; i ++)
        ans.push_back({q[i], q[(m / 2) + i]});
    if (m & 1)
        ans.push_back({q[0], q.back()});
    cout << ans.size() << '\n';
    for (auto z : ans)
        cout << z.st << ' ' << z.nd << '\n';
    return 0;
}

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:31:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |         freopen( TASKNAME".inp", "r", stdin );
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
net.cpp:32:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |         freopen( TASKNAME".out", "w", stdout );
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...