#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int MAX = 5e5 + 10;
int N;
vector<int> adj[MAX];
vector<int> leaves;
void dfs(int u, int p = -1){
if(sz(adj[u]) == 1) leaves.push_back(u);
for(int v: adj[u]){
if(v == p) continue;
dfs(v, u);
}
}
void solve(){
cin >> N;
for(int i = 1; i < N; i++){
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
clock_t start = clock();
if(N == 2){
cout << 1 << '\n' << "1 2\n";
return;
}
int root;
for(int i = 1; i <= N; i++){
if(sz(adj[i]) > 1){
root = i;
break;
}
}
dfs(root);
vector<pair<int, int>> ans;
int n = sz(leaves);
for(int i = 0; i < n / 2; i++){
ans.push_back({leaves[i], leaves[i + n / 2]});
}
if(sz(leaves) & 1){
ans.push_back({leaves[0], leaves.back()});
}
cout << sz(ans) << '\n';
for(auto o: ans){
cout << o.fi << ' ' << o.se << '\n';
}
cerr << "Time elapsed: " << clock() - start << " ms\n";
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("walk.inp","r",stdin);
// freopen("walk.out","w",stdout);
int t = 1;
// cin >> t;
while(t--)
solve();
return 0;
}
Compilation message
net.cpp: In function 'void solve()':
net.cpp:67:8: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
67 | dfs(root);
| ~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12124 KB |
Output is correct |
2 |
Correct |
5 ms |
12124 KB |
Output is correct |
3 |
Correct |
6 ms |
12124 KB |
Output is correct |
4 |
Correct |
5 ms |
12376 KB |
Output is correct |
5 |
Correct |
6 ms |
12124 KB |
Output is correct |
6 |
Correct |
6 ms |
12124 KB |
Output is correct |
7 |
Correct |
5 ms |
12124 KB |
Output is correct |
8 |
Correct |
6 ms |
12124 KB |
Output is correct |
9 |
Correct |
6 ms |
12124 KB |
Output is correct |
10 |
Correct |
5 ms |
12120 KB |
Output is correct |
11 |
Correct |
5 ms |
12124 KB |
Output is correct |
12 |
Correct |
6 ms |
12124 KB |
Output is correct |
13 |
Correct |
6 ms |
12124 KB |
Output is correct |
14 |
Correct |
5 ms |
12120 KB |
Output is correct |
15 |
Correct |
6 ms |
12124 KB |
Output is correct |
16 |
Correct |
5 ms |
12124 KB |
Output is correct |
17 |
Correct |
5 ms |
12196 KB |
Output is correct |
18 |
Correct |
5 ms |
12120 KB |
Output is correct |
19 |
Correct |
5 ms |
12124 KB |
Output is correct |
20 |
Correct |
5 ms |
12124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12124 KB |
Output is correct |
2 |
Correct |
5 ms |
12124 KB |
Output is correct |
3 |
Correct |
6 ms |
12124 KB |
Output is correct |
4 |
Correct |
5 ms |
12376 KB |
Output is correct |
5 |
Correct |
6 ms |
12124 KB |
Output is correct |
6 |
Correct |
6 ms |
12124 KB |
Output is correct |
7 |
Correct |
5 ms |
12124 KB |
Output is correct |
8 |
Correct |
6 ms |
12124 KB |
Output is correct |
9 |
Correct |
6 ms |
12124 KB |
Output is correct |
10 |
Correct |
5 ms |
12120 KB |
Output is correct |
11 |
Correct |
5 ms |
12124 KB |
Output is correct |
12 |
Correct |
6 ms |
12124 KB |
Output is correct |
13 |
Correct |
6 ms |
12124 KB |
Output is correct |
14 |
Correct |
5 ms |
12120 KB |
Output is correct |
15 |
Correct |
6 ms |
12124 KB |
Output is correct |
16 |
Correct |
5 ms |
12124 KB |
Output is correct |
17 |
Correct |
5 ms |
12196 KB |
Output is correct |
18 |
Correct |
5 ms |
12120 KB |
Output is correct |
19 |
Correct |
5 ms |
12124 KB |
Output is correct |
20 |
Correct |
5 ms |
12124 KB |
Output is correct |
21 |
Correct |
6 ms |
12120 KB |
Output is correct |
22 |
Correct |
5 ms |
12124 KB |
Output is correct |
23 |
Correct |
5 ms |
12124 KB |
Output is correct |
24 |
Correct |
5 ms |
12124 KB |
Output is correct |
25 |
Correct |
6 ms |
12124 KB |
Output is correct |
26 |
Correct |
6 ms |
12124 KB |
Output is correct |
27 |
Correct |
6 ms |
12124 KB |
Output is correct |
28 |
Correct |
7 ms |
12120 KB |
Output is correct |
29 |
Correct |
6 ms |
12124 KB |
Output is correct |
30 |
Correct |
6 ms |
12124 KB |
Output is correct |
31 |
Correct |
5 ms |
12124 KB |
Output is correct |
32 |
Correct |
6 ms |
12124 KB |
Output is correct |
33 |
Correct |
5 ms |
12124 KB |
Output is correct |
34 |
Correct |
7 ms |
12196 KB |
Output is correct |
35 |
Correct |
6 ms |
12376 KB |
Output is correct |
36 |
Correct |
5 ms |
12124 KB |
Output is correct |
37 |
Correct |
5 ms |
12124 KB |
Output is correct |
38 |
Correct |
7 ms |
12216 KB |
Output is correct |
39 |
Correct |
5 ms |
12124 KB |
Output is correct |
40 |
Correct |
4 ms |
12204 KB |
Output is correct |
41 |
Correct |
5 ms |
12124 KB |
Output is correct |
42 |
Correct |
5 ms |
12124 KB |
Output is correct |
43 |
Correct |
5 ms |
12124 KB |
Output is correct |
44 |
Correct |
5 ms |
12196 KB |
Output is correct |
45 |
Correct |
6 ms |
12376 KB |
Output is correct |
46 |
Correct |
5 ms |
12120 KB |
Output is correct |
47 |
Correct |
5 ms |
12124 KB |
Output is correct |
48 |
Correct |
5 ms |
12124 KB |
Output is correct |
49 |
Correct |
6 ms |
12140 KB |
Output is correct |
50 |
Correct |
5 ms |
12124 KB |
Output is correct |
51 |
Correct |
6 ms |
12124 KB |
Output is correct |
52 |
Correct |
5 ms |
12124 KB |
Output is correct |
53 |
Correct |
5 ms |
12124 KB |
Output is correct |
54 |
Correct |
5 ms |
12120 KB |
Output is correct |
55 |
Correct |
5 ms |
12124 KB |
Output is correct |
56 |
Correct |
5 ms |
12124 KB |
Output is correct |
57 |
Correct |
5 ms |
12120 KB |
Output is correct |
58 |
Correct |
5 ms |
12124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12124 KB |
Output is correct |
2 |
Correct |
5 ms |
12124 KB |
Output is correct |
3 |
Correct |
6 ms |
12124 KB |
Output is correct |
4 |
Correct |
5 ms |
12376 KB |
Output is correct |
5 |
Correct |
6 ms |
12124 KB |
Output is correct |
6 |
Correct |
6 ms |
12124 KB |
Output is correct |
7 |
Correct |
5 ms |
12124 KB |
Output is correct |
8 |
Correct |
6 ms |
12124 KB |
Output is correct |
9 |
Correct |
6 ms |
12124 KB |
Output is correct |
10 |
Correct |
5 ms |
12120 KB |
Output is correct |
11 |
Correct |
5 ms |
12124 KB |
Output is correct |
12 |
Correct |
6 ms |
12124 KB |
Output is correct |
13 |
Correct |
6 ms |
12124 KB |
Output is correct |
14 |
Correct |
5 ms |
12120 KB |
Output is correct |
15 |
Correct |
6 ms |
12124 KB |
Output is correct |
16 |
Correct |
5 ms |
12124 KB |
Output is correct |
17 |
Correct |
5 ms |
12196 KB |
Output is correct |
18 |
Correct |
5 ms |
12120 KB |
Output is correct |
19 |
Correct |
5 ms |
12124 KB |
Output is correct |
20 |
Correct |
5 ms |
12124 KB |
Output is correct |
21 |
Correct |
6 ms |
12120 KB |
Output is correct |
22 |
Correct |
5 ms |
12124 KB |
Output is correct |
23 |
Correct |
5 ms |
12124 KB |
Output is correct |
24 |
Correct |
5 ms |
12124 KB |
Output is correct |
25 |
Correct |
6 ms |
12124 KB |
Output is correct |
26 |
Correct |
6 ms |
12124 KB |
Output is correct |
27 |
Correct |
6 ms |
12124 KB |
Output is correct |
28 |
Correct |
7 ms |
12120 KB |
Output is correct |
29 |
Correct |
6 ms |
12124 KB |
Output is correct |
30 |
Correct |
6 ms |
12124 KB |
Output is correct |
31 |
Correct |
5 ms |
12124 KB |
Output is correct |
32 |
Correct |
6 ms |
12124 KB |
Output is correct |
33 |
Correct |
5 ms |
12124 KB |
Output is correct |
34 |
Correct |
7 ms |
12196 KB |
Output is correct |
35 |
Correct |
6 ms |
12376 KB |
Output is correct |
36 |
Correct |
5 ms |
12124 KB |
Output is correct |
37 |
Correct |
5 ms |
12124 KB |
Output is correct |
38 |
Correct |
7 ms |
12216 KB |
Output is correct |
39 |
Correct |
5 ms |
12124 KB |
Output is correct |
40 |
Correct |
4 ms |
12204 KB |
Output is correct |
41 |
Correct |
5 ms |
12124 KB |
Output is correct |
42 |
Correct |
5 ms |
12124 KB |
Output is correct |
43 |
Correct |
5 ms |
12124 KB |
Output is correct |
44 |
Correct |
5 ms |
12196 KB |
Output is correct |
45 |
Correct |
6 ms |
12376 KB |
Output is correct |
46 |
Correct |
5 ms |
12120 KB |
Output is correct |
47 |
Correct |
5 ms |
12124 KB |
Output is correct |
48 |
Correct |
5 ms |
12124 KB |
Output is correct |
49 |
Correct |
6 ms |
12140 KB |
Output is correct |
50 |
Correct |
5 ms |
12124 KB |
Output is correct |
51 |
Correct |
6 ms |
12124 KB |
Output is correct |
52 |
Correct |
5 ms |
12124 KB |
Output is correct |
53 |
Correct |
5 ms |
12124 KB |
Output is correct |
54 |
Correct |
5 ms |
12120 KB |
Output is correct |
55 |
Correct |
5 ms |
12124 KB |
Output is correct |
56 |
Correct |
5 ms |
12124 KB |
Output is correct |
57 |
Correct |
5 ms |
12120 KB |
Output is correct |
58 |
Correct |
5 ms |
12124 KB |
Output is correct |
59 |
Correct |
166 ms |
37312 KB |
Output is correct |
60 |
Correct |
192 ms |
40096 KB |
Output is correct |
61 |
Correct |
5 ms |
12124 KB |
Output is correct |
62 |
Correct |
5 ms |
12124 KB |
Output is correct |
63 |
Correct |
133 ms |
34692 KB |
Output is correct |
64 |
Correct |
6 ms |
12636 KB |
Output is correct |
65 |
Correct |
12 ms |
13912 KB |
Output is correct |
66 |
Correct |
65 ms |
27984 KB |
Output is correct |
67 |
Correct |
264 ms |
47096 KB |
Output is correct |
68 |
Correct |
249 ms |
47952 KB |
Output is correct |
69 |
Correct |
9 ms |
13148 KB |
Output is correct |
70 |
Correct |
48 ms |
21960 KB |
Output is correct |
71 |
Correct |
179 ms |
43976 KB |
Output is correct |
72 |
Correct |
169 ms |
42520 KB |
Output is correct |
73 |
Correct |
54 ms |
19412 KB |
Output is correct |
74 |
Correct |
213 ms |
38656 KB |
Output is correct |
75 |
Correct |
18 ms |
15448 KB |
Output is correct |
76 |
Correct |
184 ms |
43464 KB |
Output is correct |
77 |
Correct |
161 ms |
43556 KB |
Output is correct |
78 |
Correct |
21 ms |
15708 KB |
Output is correct |
79 |
Correct |
250 ms |
45136 KB |
Output is correct |
80 |
Correct |
6 ms |
12376 KB |
Output is correct |
81 |
Correct |
54 ms |
21872 KB |
Output is correct |
82 |
Correct |
186 ms |
41136 KB |
Output is correct |
83 |
Correct |
199 ms |
38736 KB |
Output is correct |
84 |
Correct |
228 ms |
38704 KB |
Output is correct |
85 |
Correct |
223 ms |
38616 KB |
Output is correct |
86 |
Correct |
238 ms |
40012 KB |
Output is correct |
87 |
Correct |
190 ms |
40000 KB |
Output is correct |