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/assoc_container.hpp>
#define INF 1000000021
#define MOD 1000000007
#define pb push_back
#define sqr(a) (a)*(a)
#define M(a, b) make_pair(a,b)
#define T(a, b, c) make_pair(a, make_pair(b, c))
#define F first
#define S second
#define all(x) (x.begin(), x.end())
#define deb(x) cerr << #x << " = " << x << '\n'
#define N 222222
using namespace std;
//using namespace __gnu_pbds;
typedef long double ld;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
const ld pi = 2 * acos(0.0);
template<class T> bool umin(T& a, T b){if(a>b){a=b;return 1;}return 0;}
template<class T> bool umax(T& a, T b){if(a<b){a=b;return 1;}return 0;}
template<class T, class TT> bool pal(T a, TT n){int k=0;for(int i=0;i<=n/2;i++){if(a[i]!=a[n-i-1]){k=1;break;}}return k?0:1;}
//int month[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
vi v[N];
int used[N];
vi a;
void dfs(int u)
{
used[u] = 1;
int flag = 0;
for(auto it : v[u])
{
if(used[it])
continue;
dfs(it);
flag = 1;
}
if(!flag)
a.pb(u);
}
int main()
{
int n;
cin >> n;
map<pii, int> mp;
for(int i = 1; i < n; i ++)
{
int x, y;
cin >> x >> y;
v[x].pb(y);
v[y].pb(x);
mp[M(x, y)] = mp[M(y, x)] = 1;
}
int root = 0;
for(int i = 1; i <= n; i ++)
{
if(v[i].size() > 1)
{
root = i;
break;
}
}
dfs(root);
vector<pii> ans;
for(int i = 1; i < a.size(); i += 2)
{
if(mp[M(a[i], a[i - 1])])
continue;
ans.pb(M(a[i], a[i - 1]));
mp[M(a[i], a[i - 1])] = mp[M(a[i - 1], a[i])] = 1;
}
if(a.size() % 2 == 1)
{
int cur = a.back();
for(int i = 1; i <= n; i ++)
if(i != cur)
{
if(mp[M(i, cur)] == 0)
{
ans.pb(M(i, cur));
break;
}
}
}
cout << ans.size() << '\n';
for(auto it : ans)
cout << it.F << ' ' << it.S << '\n';
getchar();
getchar();
return 0;
//ios::sync_with_stdio(false);
//cin.tie(0);
}
Compilation message (stderr)
net.cpp: In function 'int main()':
net.cpp:75:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < a.size(); i += 2)
~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |