Submission #475848

#TimeUsernameProblemLanguageResultExecution timeMemory
475848MohamedAhmed04Network (BOI15_net)C++14
100 / 100
516 ms47960 KiB
#include <bits/stdc++.h> using namespace std; const int MAX = 500005 ; int n ; vector< vector<int> >adj(MAX) ; stack<int>s ; vector<int>v ; void dfs(int node , int par) { if(adj[node].size() == 1) v.push_back(node) ; for(auto &child : adj[node]) { if(child == par) continue; dfs(child , node) ; } } int main() { scanf("%d" , &n) ; for(int i = 0 ; i < n-1 ; ++i) { int x , y ; scanf("%d %d" , &x , &y) ; adj[x].push_back(y) ; adj[y].push_back(x) ; } int leaves = 0 ; for(int i = 1 ; i <= n ; ++i) { if(adj[i].size() == 1) leaves++; } int start = -1 ; for(int i = 1 ; i <= n ; ++i) { if(adj[i].size() > 1) { start = i ; dfs(start , -1) ; break; } } int edges = (leaves + 1) / 2 ; cout<<edges<<"\n"; if(leaves & 1) { cout<<v[edges-1]<<" "<<start<<"\n"; } for(int i = 0 ; v.size()/2 ; ++i) { if(i + edges >= v.size()) break; cout<<v[i]<<" "<<v[i+edges]<<"\n"; } return 0 ; }

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:59:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |         if(i + edges >= v.size())
      |            ~~~~~~~~~~^~~~~~~~~~~
net.cpp:27:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     scanf("%d" , &n) ;
      |     ~~~~~^~~~~~~~~~~
net.cpp:31:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |         scanf("%d %d" , &x , &y) ;
      |         ~~~~~^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...