#pragma GCC optimize("O3")
#include<bits/stdc++.h>
//#include <grader.h>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define ld long double
#define st first
#define nd second
#define pb push_back
#define INF INT_MAX
#define db double
using namespace std;
const int N = 5e5 + 10;
vector<int> v;
vector<int> adj[N];
void dfs(int i, int prt) {
for (auto j : adj[i]) if (j != prt) dfs(j, i);
if (adj[i].size() == 1) v.pb(i);
}
void solve() {
int n; cin >> n;
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
adj[u].pb(v); adj[v].pb(u);
}
int root;
for (int i = 1; i <= n; ++i) if (adj[i].size() > 1) root = i;
dfs(root, 0);
int sz = v.size();
cout << (sz+1)/2 << '\n';
for (int i = 0; i < sz/2; ++i) {
cout << v[i] << " " << v[i+sz/2] << '\n';
}
if (sz&1) cout << v[sz-1] << " " << v[0] << '\n';
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
Compilation message
net.cpp: In function 'void solve()':
net.cpp:31:9: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
31 | int root;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
8 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
11988 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
8 ms |
11992 KB |
Output is correct |
6 |
Correct |
7 ms |
12060 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
7 ms |
11988 KB |
Output is correct |
9 |
Correct |
7 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
8 ms |
11988 KB |
Output is correct |
12 |
Correct |
7 ms |
12068 KB |
Output is correct |
13 |
Correct |
7 ms |
12064 KB |
Output is correct |
14 |
Correct |
7 ms |
11988 KB |
Output is correct |
15 |
Correct |
8 ms |
11988 KB |
Output is correct |
16 |
Correct |
7 ms |
11988 KB |
Output is correct |
17 |
Correct |
8 ms |
11964 KB |
Output is correct |
18 |
Correct |
8 ms |
11988 KB |
Output is correct |
19 |
Correct |
7 ms |
11988 KB |
Output is correct |
20 |
Correct |
7 ms |
11988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
8 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
11988 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
8 ms |
11992 KB |
Output is correct |
6 |
Correct |
7 ms |
12060 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
7 ms |
11988 KB |
Output is correct |
9 |
Correct |
7 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
8 ms |
11988 KB |
Output is correct |
12 |
Correct |
7 ms |
12068 KB |
Output is correct |
13 |
Correct |
7 ms |
12064 KB |
Output is correct |
14 |
Correct |
7 ms |
11988 KB |
Output is correct |
15 |
Correct |
8 ms |
11988 KB |
Output is correct |
16 |
Correct |
7 ms |
11988 KB |
Output is correct |
17 |
Correct |
8 ms |
11964 KB |
Output is correct |
18 |
Correct |
8 ms |
11988 KB |
Output is correct |
19 |
Correct |
7 ms |
11988 KB |
Output is correct |
20 |
Correct |
7 ms |
11988 KB |
Output is correct |
21 |
Correct |
7 ms |
11988 KB |
Output is correct |
22 |
Correct |
8 ms |
12076 KB |
Output is correct |
23 |
Correct |
8 ms |
12124 KB |
Output is correct |
24 |
Correct |
8 ms |
12124 KB |
Output is correct |
25 |
Correct |
8 ms |
12052 KB |
Output is correct |
26 |
Correct |
9 ms |
12116 KB |
Output is correct |
27 |
Correct |
8 ms |
12080 KB |
Output is correct |
28 |
Correct |
8 ms |
12072 KB |
Output is correct |
29 |
Correct |
8 ms |
12076 KB |
Output is correct |
30 |
Correct |
8 ms |
12064 KB |
Output is correct |
31 |
Correct |
8 ms |
12100 KB |
Output is correct |
32 |
Correct |
8 ms |
11988 KB |
Output is correct |
33 |
Correct |
7 ms |
11988 KB |
Output is correct |
34 |
Correct |
8 ms |
12000 KB |
Output is correct |
35 |
Correct |
7 ms |
11988 KB |
Output is correct |
36 |
Correct |
8 ms |
11952 KB |
Output is correct |
37 |
Correct |
8 ms |
11988 KB |
Output is correct |
38 |
Correct |
8 ms |
12104 KB |
Output is correct |
39 |
Correct |
7 ms |
11988 KB |
Output is correct |
40 |
Correct |
7 ms |
11988 KB |
Output is correct |
41 |
Correct |
7 ms |
12064 KB |
Output is correct |
42 |
Correct |
7 ms |
12084 KB |
Output is correct |
43 |
Correct |
7 ms |
12064 KB |
Output is correct |
44 |
Correct |
7 ms |
12004 KB |
Output is correct |
45 |
Correct |
8 ms |
11988 KB |
Output is correct |
46 |
Correct |
8 ms |
12068 KB |
Output is correct |
47 |
Correct |
8 ms |
11988 KB |
Output is correct |
48 |
Correct |
7 ms |
11988 KB |
Output is correct |
49 |
Correct |
7 ms |
12116 KB |
Output is correct |
50 |
Correct |
7 ms |
12068 KB |
Output is correct |
51 |
Correct |
7 ms |
12064 KB |
Output is correct |
52 |
Correct |
7 ms |
12064 KB |
Output is correct |
53 |
Correct |
8 ms |
12116 KB |
Output is correct |
54 |
Correct |
7 ms |
12068 KB |
Output is correct |
55 |
Correct |
7 ms |
12064 KB |
Output is correct |
56 |
Correct |
7 ms |
12064 KB |
Output is correct |
57 |
Correct |
8 ms |
12068 KB |
Output is correct |
58 |
Correct |
8 ms |
12160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
8 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
11988 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
8 ms |
11992 KB |
Output is correct |
6 |
Correct |
7 ms |
12060 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
7 ms |
11988 KB |
Output is correct |
9 |
Correct |
7 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
8 ms |
11988 KB |
Output is correct |
12 |
Correct |
7 ms |
12068 KB |
Output is correct |
13 |
Correct |
7 ms |
12064 KB |
Output is correct |
14 |
Correct |
7 ms |
11988 KB |
Output is correct |
15 |
Correct |
8 ms |
11988 KB |
Output is correct |
16 |
Correct |
7 ms |
11988 KB |
Output is correct |
17 |
Correct |
8 ms |
11964 KB |
Output is correct |
18 |
Correct |
8 ms |
11988 KB |
Output is correct |
19 |
Correct |
7 ms |
11988 KB |
Output is correct |
20 |
Correct |
7 ms |
11988 KB |
Output is correct |
21 |
Correct |
7 ms |
11988 KB |
Output is correct |
22 |
Correct |
8 ms |
12076 KB |
Output is correct |
23 |
Correct |
8 ms |
12124 KB |
Output is correct |
24 |
Correct |
8 ms |
12124 KB |
Output is correct |
25 |
Correct |
8 ms |
12052 KB |
Output is correct |
26 |
Correct |
9 ms |
12116 KB |
Output is correct |
27 |
Correct |
8 ms |
12080 KB |
Output is correct |
28 |
Correct |
8 ms |
12072 KB |
Output is correct |
29 |
Correct |
8 ms |
12076 KB |
Output is correct |
30 |
Correct |
8 ms |
12064 KB |
Output is correct |
31 |
Correct |
8 ms |
12100 KB |
Output is correct |
32 |
Correct |
8 ms |
11988 KB |
Output is correct |
33 |
Correct |
7 ms |
11988 KB |
Output is correct |
34 |
Correct |
8 ms |
12000 KB |
Output is correct |
35 |
Correct |
7 ms |
11988 KB |
Output is correct |
36 |
Correct |
8 ms |
11952 KB |
Output is correct |
37 |
Correct |
8 ms |
11988 KB |
Output is correct |
38 |
Correct |
8 ms |
12104 KB |
Output is correct |
39 |
Correct |
7 ms |
11988 KB |
Output is correct |
40 |
Correct |
7 ms |
11988 KB |
Output is correct |
41 |
Correct |
7 ms |
12064 KB |
Output is correct |
42 |
Correct |
7 ms |
12084 KB |
Output is correct |
43 |
Correct |
7 ms |
12064 KB |
Output is correct |
44 |
Correct |
7 ms |
12004 KB |
Output is correct |
45 |
Correct |
8 ms |
11988 KB |
Output is correct |
46 |
Correct |
8 ms |
12068 KB |
Output is correct |
47 |
Correct |
8 ms |
11988 KB |
Output is correct |
48 |
Correct |
7 ms |
11988 KB |
Output is correct |
49 |
Correct |
7 ms |
12116 KB |
Output is correct |
50 |
Correct |
7 ms |
12068 KB |
Output is correct |
51 |
Correct |
7 ms |
12064 KB |
Output is correct |
52 |
Correct |
7 ms |
12064 KB |
Output is correct |
53 |
Correct |
8 ms |
12116 KB |
Output is correct |
54 |
Correct |
7 ms |
12068 KB |
Output is correct |
55 |
Correct |
7 ms |
12064 KB |
Output is correct |
56 |
Correct |
7 ms |
12064 KB |
Output is correct |
57 |
Correct |
8 ms |
12068 KB |
Output is correct |
58 |
Correct |
8 ms |
12160 KB |
Output is correct |
59 |
Correct |
329 ms |
36048 KB |
Output is correct |
60 |
Correct |
380 ms |
38808 KB |
Output is correct |
61 |
Correct |
7 ms |
11988 KB |
Output is correct |
62 |
Correct |
8 ms |
11988 KB |
Output is correct |
63 |
Correct |
261 ms |
33488 KB |
Output is correct |
64 |
Correct |
9 ms |
12372 KB |
Output is correct |
65 |
Correct |
16 ms |
13448 KB |
Output is correct |
66 |
Correct |
128 ms |
23400 KB |
Output is correct |
67 |
Correct |
414 ms |
50040 KB |
Output is correct |
68 |
Correct |
402 ms |
45316 KB |
Output is correct |
69 |
Correct |
13 ms |
12884 KB |
Output is correct |
70 |
Correct |
79 ms |
21188 KB |
Output is correct |
71 |
Correct |
249 ms |
41836 KB |
Output is correct |
72 |
Correct |
259 ms |
40652 KB |
Output is correct |
73 |
Correct |
68 ms |
19020 KB |
Output is correct |
74 |
Correct |
354 ms |
37688 KB |
Output is correct |
75 |
Correct |
26 ms |
15188 KB |
Output is correct |
76 |
Correct |
272 ms |
41592 KB |
Output is correct |
77 |
Correct |
271 ms |
41664 KB |
Output is correct |
78 |
Correct |
31 ms |
15052 KB |
Output is correct |
79 |
Correct |
393 ms |
41440 KB |
Output is correct |
80 |
Correct |
9 ms |
12244 KB |
Output is correct |
81 |
Correct |
106 ms |
21268 KB |
Output is correct |
82 |
Correct |
327 ms |
39528 KB |
Output is correct |
83 |
Correct |
361 ms |
37696 KB |
Output is correct |
84 |
Correct |
397 ms |
37612 KB |
Output is correct |
85 |
Correct |
369 ms |
37600 KB |
Output is correct |
86 |
Correct |
397 ms |
38844 KB |
Output is correct |
87 |
Correct |
430 ms |
38716 KB |
Output is correct |