Submission #1274977

#TimeUsernameProblemLanguageResultExecution timeMemory
1274977lamlamlamNetwork (BOI15_net)C++20
0 / 100
3 ms14604 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);
    sort(leaves.begin(),leaves.end());
    cout << (leaves.size()+1)/2 << endl;
    for(int i=0; i+1<leaves.size(); i+=2){
        cout << leaves[i] << ' ' << leaves[i+1] << 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:22:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
net.cpp:23:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |         freopen(task".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...