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 <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T>using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;//find_by_order(ind);//order_of_key()
#define int long long
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define pb emplace_back
#define pf emplace_front
#define yes "YES"
#define no "NO"
#define bigg INT_MAX
#define mp make_pair
#define mt make_tuple
#define debug(x) cout<<(#x)<<" = " <<x<<endl;
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define nn '\n'
#define mms(x,y) memset(x,y,sizeof(x))
#define forr(i,j,n) for (int i=j; i<n; i++)
#define forn(i,j,n) for (int i=j; i>n; i--)
#define cinn(x,y) for(int i=0; i<y; i++) cin>>x[i];
#define fi first
#define se second
#define la "LA"
#define pii pair<int,int>
// to use cmd ctrl+shift+c then g++ name.cpp -o name2.exe then name2 then input
//ctrl+shift+p to open search
//shift+alt+a stack comment2
///power function works in log2b
// int pwr(int a, int b)
// {
// int po = 1;
// while ( b )
// {
// if ( b & 1 )
// {
// po = po * a;
// --b;
// }
// a = a*a;
// b = b/2;
// }
// return po;
// }
vector <int> a[500001];
vector <int> leaf;
bool vis[500001];
void dfs(int i)
{
vis[i]=1;
if (a[i].sz==1) leaf.pb(i);
forr(j,0,a[i].sz)
{
if(!vis[a[i][j]]) dfs(a[i][j]);
}
return;
}
signed main()
{
fast
// #ifndef LOCAL
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
// #endif
int n;
cin>>n;
forr(i,0,n-1)
{
int x,y;
cin>>x >>y;
a[x].pb(y);
a[y].pb(x);
}
dfs(1);
int cnt=leaf.sz;
// cout<<leaf.sz<<endl<<endl;
int i=0;
//j=cnt-1;
cout<<(cnt/2)+(cnt%2)<<nn;
while(cnt>0)
{
// cout<<yes<<cnt<<endl;
if (cnt==1)
{
cout<<leaf[i]<<' '<<leaf[0]<<nn;
}
else
{
//not 2 adjacent
//if wrong try idea 2
//cout<<i<<' '<<j<<endl;
cout<<leaf[i]<<' '<<leaf[(leaf.sz/2)+(leaf.sz%2)+i]<<nn;
i++;
}
cnt-=2;
}
return 0;
}
Compilation message (stderr)
net.cpp: In function 'void dfs(long long int)':
net.cpp:22:36: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | #define forr(i,j,n) for (int i=j; i<n; i++)
| ^
net.cpp:55:5: note: in expansion of macro 'forr'
55 | forr(j,0,a[i].sz)
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |