#include <bits/stdc++.h>
using namespace std;
int n, h[500001];
vector <int> g[500001];
void dfs(int nod, int lvl)
{
h[nod] = lvl;
for (auto& i : g[nod])
if (!h[i])
dfs(i, lvl + 1);
}
struct nod {
int key;
bool operator < (nod aux)
{
return h[key] < h[aux.key];
}
};
vector <nod> v;
int main() {
cin >> n;
for (int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1, 1);
for (int i = 1; i <= n; i++)
if (g[i].size() == 1)
v.push_back({ i });
cout << (v.size() + 1) / 2 << '\n';
sort(v.begin(), v.end());
int i = 0, j = v.size() - 1;
while (i < j)
{
cout << v[i].key << ' ' << v[j].key << '\n';
i++;
j--;
}
if (i == j)
if (g[v[i].key][0] == 1)
cout << v[i].key << ' ' << 2;
else
cout << v[i].key << ' ' << 1;
return 0;
}
Compilation message
net.cpp: In function 'int main()':
net.cpp:58:8: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
58 | if (i == j)
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
12116 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
8 ms |
11988 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Incorrect |
6 ms |
11988 KB |
Breaking single line is causing network to disconnect. |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
12116 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
8 ms |
11988 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Incorrect |
6 ms |
11988 KB |
Breaking single line is causing network to disconnect. |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
12116 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
8 ms |
11988 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Incorrect |
6 ms |
11988 KB |
Breaking single line is causing network to disconnect. |
12 |
Halted |
0 ms |
0 KB |
- |