#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")
#define LL long long
#define se second
#define fi first
using namespace std;
const int N = 2e5 + 7;
const int mod = 1e9 + 7;
const int B = sqrt(N);
int n;
bool used[N];
vector < int > v[N];
vector < int > ans;
void dfs( int x , int p )
{
if( v[x].size() == 1 && !used[x] ){
ans.push_back(x);
}
used[x] = true;
for( auto y : v[x] ){
if( y == p )continue;
dfs(y , x);
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL);
//freopen( "input.txt" , "r" , stdin );
//freopen( "output.txt" , "w" , stdout );
cin >> n;
for( int i = 1; i < n; i++ ){
int x,y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
for( int i = 1; i <= n; i++ ){
if( v[i].size() > 1 ){
dfs(i , i);
}
}
int k = (ans.size() + 1) / 2;
cout << ( (int)ans.size() + 1 ) / 2 << "\n";
for( int i = 0; i < ( ans.size() + 1) / 2; i++ ){
cout << ans[i] << " " << ans[ (i + k) ] << "\n";
}
}
Compilation message
net.cpp: In function 'int main()':
net.cpp:52:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for( int i = 0; i < ( ans.size() + 1) / 2; i++ ){
~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Incorrect |
6 ms |
5112 KB |
Integer 0 violates the range [1, 10] |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Incorrect |
6 ms |
5112 KB |
Integer 0 violates the range [1, 10] |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Incorrect |
6 ms |
5112 KB |
Integer 0 violates the range [1, 10] |
5 |
Halted |
0 ms |
0 KB |
- |