Submission #1275303

#TimeUsernameProblemLanguageResultExecution timeMemory
1275303nthvnNetwork (BOI15_net)C++20
100 / 100
281 ms49580 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define endl '\n'
#define task "BOI15_net"
const int MN = 5e5+5;
int n,u,v;
vector<int> adj[MN],leaves;
void dfs(int u,int p){
    if(adj[u].size()==1) leaves.push_back(u);
    for(auto v:adj[u]) if(v!=p) dfs(v,u);
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if(fopen(task".inp","r")){
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    cin >> n;
    for(int i=1; i<n; i++){
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1,0);
    cout << (leaves.size()+1)/2 << endl;
    int dak = leaves.size()/2;
    for(int i=0; i+dak<leaves.size() and i<dak; i++){
        cout << leaves[i] << ' ' << leaves[i+dak] << endl;
    }
    if(leaves.size()%2) cout << leaves[0] << ' ' << leaves.back() << endl;
    cerr << "\nThoi gian loj: " << clock() << endl;
}

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...