Submission #503958

#TimeUsernameProblemLanguageResultExecution timeMemory
503958goodluck2020Network (BOI15_net)C++14
100 / 100
433 ms47816 KiB
#include <bits/stdc++.h>
#define task "BOI15_net"
#define sz(X) ((int)X.size())

using namespace std;
const int N = 5e5 + 5;
int n;
vector < int > adj[N], Leaf;
vector < pair < int , int > > res;
void DFS(int u, int par)
{
    if(sz(adj[u]) == 1) Leaf.push_back(u);
    for(int v : adj[u])
        if(v != par) DFS(v, u);
}
int main()
{
    if(fopen(task ".inp","r"))
    {
        freopen(task ".inp","r",stdin);
        freopen(task ".out","w",stdout);
    }
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for(int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    DFS(1, 0);
    for(int i = 0; i < (sz(Leaf) + 1)/2; i++)
        res.push_back({Leaf[i], Leaf[i + sz(Leaf)/2]});
    cout << sz(res) << '\n';
    for(auto it : res) cout << it.first << " " << it.second << '\n';
}

Compilation message (stderr)

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