This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
_________ .---""" """---.
:______.-': : .--------------. :
| ______ | | : : |
|:______B:| | | Little Error: | |
|:______B:| | | | |
|:______B:| | | Power not | |
| | | | found. | |
|:_____: | | | | |
| == | | : : |
| O | : '--------------' :
| o | :'---...______...---'
| o |-._.-i___/' \._
|'-.____o_| '-. '-...______...-' `-._
:_________: `.____________________ `-.___.-.
.'.eeeeeeeeeeeeeeeeee.'. :___:
fsc .'.eeeeeeeeeeeeeeeeeeeeee.'.
:____________________________:
*/
//in the name of god
//if you read this code please search about imam hussain
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define endl "\n"
#define X first
#define Y second
#define pii pair<int,int>
#define migmig ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define read freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout)
const int maxn=2e5+5;
const int mod=1e9+7;
const int inf=1e9;
const int del=728729;
ll poww(ll a, ll b, ll md) {return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));}
ll n ;
ll deg ;
ll DD[maxn];
ll D[maxn] ;
vector< int > B ;
vector<int> adj[maxn];
ll cnt ;
void dfs(int v, int P = 0){
DD[v] = deg++;
for(auto u : adj[v]){
if(u == P){
continue;
}
dfs(u, v);
}
}
bool cmp(int i, int j){
return DD[i] < DD[j];
}
int main(){
cin >> n ;
ll root ;
for(int i = 1; i < n ;i ++){
int x ;
int y ;
cin >> x >> y ;
adj[x].push_back(y) ;
adj[y].push_back(x) ;
D[x]++ ;
D[y]++ ;
if(D[x] > 1){
root = x ;
}
if(D[y] > 1){
root = y ;
}
}
for(int i = 1; i <= n; i ++){
if(adj[i].size() == 1){
B.pb(i);
cnt++;
}
}
dfs(root);
cout<<(cnt + 1 ) / 2 <<endl ;
cnt = (cnt + 1) / 2 ;
for(int i = 0 ; i + cnt < B.size() ; i ++ ){
cout<<B[i] <<" " << B[i + cnt] << endl ;
}
if(B.size() % 2 == 1){
cout<<B[cnt-1] <<" " << B[0] << endl ;
}
}
Compilation message (stderr)
net.cpp: In function 'int main()':
net.cpp:96:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for(int i = 0 ; i + cnt < B.size() ; i ++ ){
| ~~~~~~~~^~~~~~~~~~
net.cpp:93:5: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
93 | dfs(root);
| ~~~^~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |