# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
394633 | ali_tavakoli | Network (BOI15_net) | C++14 | 8 ms | 11980 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//In The Name Of Allah
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define F first
#define S second
//#pragma GCC optimize("Ofast")
const int maxn = 5e5 + 5;
int n;
vector<int> adj[maxn], leaf;
void dfs(int v, int p = 0)
{
if(adj[v].size() == 1)
leaf.pb(v);
for(auto u : adj[v])
if(u != p)
dfs(u, v);
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 0; i < n - 1; i++)
{
int a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
for(int i = 1; i <= n; i++)
if(adj[i].size() > 1)
{
dfs(i);
break;
}
cout << (leaf.size() + 1) / 2 << '\n';
//cout << leaf.size() << '\n';
//return 0;
for(int i = 0; i < (leaf.size() + 1) / 2; i ++)
{
if(leaf[i] == leaf[leaf.size() - i - 1])
{
bool ans = 0;
for(int j = 1; j <= 3; j++)
if(j != adj[leaf[i]][0]&& j != leaf[i])
{
cout << leaf[i] << " " << j << '\n';
break;
}
}
else
cout << leaf[i] << " " << leaf[leaf.size() - i - 1] << '\n';
}
}
/*
6
1 2
2 3
2 4
5 4
6 4
8
1 2
2 3
3 4
4 5
3 6
3 7
3 8
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |