This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <climits>
#include <numeric>
#include <cassert>
#include <algorithm>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <vector>
#include <array>
#include <memory>
#define IN(a,b) (a.find(b) != a.end())
#define p(a,b) make_pair(a,b)
#define readVec(a) for (int __i = 0; __i<(int)a.size();__i++){cin>>a[__i];}
// jimjam
template<typename T>
void pMin(T &a, T b) {if (b<a){a=b;}}
template<typename T>
void pMax(T &a, T b) {if (b>a){a=b;}}
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& c);
template<typename A, typename B>
std::ostream& operator<<(std::ostream& os, const std::pair<A,B>& c) {std::cout << "(" << c.first << ", " << c.second << ")";return os;}
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& c) {
if (c.size() == 0) {os << "{}"; return os;}
os << "{" << c[0];
for (int i = 1; i < (int)c.size(); i++) {os <<", "<<c[i];}
os << "}";
return os;
}
const int inf = 2e9;
#define MAXN 500001
using namespace std;
vector<int> adj[MAXN];
vector<int> leaves;
void dfs(int c, int p=-1) {
if (adj[c].size()==1) {
leaves.push_back(c);
}
for (int i: adj[c]) {
if (i!=p) {
dfs(i,c);
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
for (int i = 0; i < n-1; i++) {
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1);
cout << leaves.size()/2 + leaves.size()%2 << "\n";
int cnt=0;
while (cnt<leaves.size()) {
if (cnt+1==leaves.size()) {
assert(leaves[cnt]!=1);
cout << leaves[cnt] << " 1\n";
cnt++;
} else {
cout << leaves[cnt] << " " << leaves[cnt+1] << "\n";
cnt+=2;
}
}
return 0;
}
Compilation message (stderr)
net.cpp: In function 'int main()':
net.cpp:70:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | while (cnt<leaves.size()) {
| ~~~^~~~~~~~~~~~~~
net.cpp:71:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | if (cnt+1==leaves.size()) {
| ~~~~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |