답안 #476183

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
476183 2021-09-25T09:06:45 Z elgamalsalman Network (BOI15_net) C++14
100 / 100
476 ms 47428 KB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;

int timer, n;
vvi adj;
vii leaves;

void dfs(int u, int p) {
  //cerr << "// dfs(" << u << ", " << p << ")\n";
  timer++;
  if (adj[u].size() == 1) leaves.push_back({timer, u});
  for (int v : adj[u]) if (v != p) dfs(v, u);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  adj.assign(n + 20, vi());
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--; b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  dfs(0, -1);
  sort(leaves.begin(), leaves.end());
  
  //cerr << "// leaves :";
  //for (ii leaf : leaves) cerr << " {" << leaf.fi << ", " << leaf.se << '}';
  //cerr << '\n';

  int leafCount = leaves.size();
  int edgeCount = (leaves.size() + 1) / 2;
  cout << edgeCount << '\n';

  for (int i = 0; i < edgeCount; i++) {
    int j = edgeCount + i;
    if (i == j) j = 0;
    cout << leaves[i].se + 1 << ' ' << leaves[j].se + 1 << '\n';
  }
}

Compilation message

net.cpp: In function 'int main()':
net.cpp:45:7: warning: unused variable 'leafCount' [-Wunused-variable]
   45 |   int leafCount = leaves.size();
      |       ^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 2 ms 460 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 2 ms 452 KB Output is correct
28 Correct 2 ms 464 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 2 ms 460 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 0 ms 308 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 0 ms 204 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 1 ms 316 KB Output is correct
39 Correct 0 ms 204 KB Output is correct
40 Correct 0 ms 204 KB Output is correct
41 Correct 0 ms 204 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 1 ms 332 KB Output is correct
50 Correct 0 ms 204 KB Output is correct
51 Correct 0 ms 204 KB Output is correct
52 Correct 1 ms 312 KB Output is correct
53 Correct 1 ms 332 KB Output is correct
54 Correct 1 ms 204 KB Output is correct
55 Correct 1 ms 332 KB Output is correct
56 Correct 0 ms 204 KB Output is correct
57 Correct 1 ms 332 KB Output is correct
58 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 2 ms 460 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 2 ms 452 KB Output is correct
28 Correct 2 ms 464 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 2 ms 460 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 0 ms 308 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 0 ms 204 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 1 ms 316 KB Output is correct
39 Correct 0 ms 204 KB Output is correct
40 Correct 0 ms 204 KB Output is correct
41 Correct 0 ms 204 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 1 ms 332 KB Output is correct
50 Correct 0 ms 204 KB Output is correct
51 Correct 0 ms 204 KB Output is correct
52 Correct 1 ms 312 KB Output is correct
53 Correct 1 ms 332 KB Output is correct
54 Correct 1 ms 204 KB Output is correct
55 Correct 1 ms 332 KB Output is correct
56 Correct 0 ms 204 KB Output is correct
57 Correct 1 ms 332 KB Output is correct
58 Correct 1 ms 460 KB Output is correct
59 Correct 357 ms 31660 KB Output is correct
60 Correct 435 ms 35372 KB Output is correct
61 Correct 0 ms 204 KB Output is correct
62 Correct 0 ms 204 KB Output is correct
63 Correct 322 ms 28652 KB Output is correct
64 Correct 2 ms 844 KB Output is correct
65 Correct 13 ms 3020 KB Output is correct
66 Correct 118 ms 22332 KB Output is correct
67 Correct 476 ms 46236 KB Output is correct
68 Correct 460 ms 47428 KB Output is correct
69 Correct 7 ms 1612 KB Output is correct
70 Correct 88 ms 13480 KB Output is correct
71 Correct 295 ms 38940 KB Output is correct
72 Correct 328 ms 37612 KB Output is correct
73 Correct 73 ms 10692 KB Output is correct
74 Correct 453 ms 33880 KB Output is correct
75 Correct 24 ms 5060 KB Output is correct
76 Correct 385 ms 38584 KB Output is correct
77 Correct 294 ms 38936 KB Output is correct
78 Correct 23 ms 5196 KB Output is correct
79 Correct 464 ms 43992 KB Output is correct
80 Correct 2 ms 588 KB Output is correct
81 Correct 100 ms 13448 KB Output is correct
82 Correct 370 ms 36268 KB Output is correct
83 Correct 447 ms 33808 KB Output is correct
84 Correct 424 ms 33816 KB Output is correct
85 Correct 401 ms 33860 KB Output is correct
86 Correct 474 ms 35252 KB Output is correct
87 Correct 428 ms 35324 KB Output is correct