Submission #605844

# Submission time Handle Problem Language Result Execution time Memory
605844 2022-07-26T03:31:51 Z buidangnguyen05 Network (BOI15_net) C++14
100 / 100
463 ms 87212 KB
//source: https://oj.uz/problem/view/BOI15_net
 
#include<bits/stdc++.h>
using namespace std;
 
typedef long long ll;
 
const int N = 1e6 + 10;
vector<int> adj[N]; 
vector<pair<int, int>> ans;
 
vector<int> dfs(int x, int par) {
	if (adj[x].size() == 1) return vector<int>(1, x);
	vector<int> cur; 
	for (int i : adj[x]) if (i != par) {
		vector<int> v = dfs(i, x);
		if (cur.size() < v.size()) swap(v, cur);
		while (cur.size() + v.size() > 2 && v.size()) {
			int p = cur.back(); cur.pop_back();
			int q = v.back(); v.pop_back();
			ans.emplace_back(p, q);
		}
		for (int j : v) cur.push_back(j);
	}
	return cur;
}
 
signed main() {
	cin.tie(0)->sync_with_stdio(0);
	if (fopen("task.inp", "r")) {
		freopen("task.inp", "r", stdin);
		freopen("task.out", "w", stdout);
	}
	
	int n; cin >> n;
	for (int i = 1; i < n; ++i) {
		int x, y; cin >> x >> y;
		adj[x].push_back(y); adj[y].push_back(x);
		//ans.emplace_back(x, y);
	}
 
	int root = 0;
	for (int i = 1; i <= n; ++i) if (adj[i].size() > 1 && !root) root = i;
	//cerr << root << "\n";
	vector<int> rem = dfs(root, 0);
	if (rem.size() > 1) {
		int u = rem.back(); rem.pop_back();
		int v = rem.back(); rem.pop_back();
		ans.emplace_back(u, v);
	}
	if (rem.size()) ans.emplace_back(rem.back(), root);
	cout << ans.size() << "\n";
	for (pair<int, int> i : ans) cout << i.first << " " << i.second << "\n";
}

Compilation message

net.cpp: In function 'int main()':
net.cpp:31:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |   freopen("task.inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
net.cpp:32:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |   freopen("task.out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23764 KB Output is correct
2 Correct 12 ms 23772 KB Output is correct
3 Correct 11 ms 23692 KB Output is correct
4 Correct 11 ms 23692 KB Output is correct
5 Correct 11 ms 23764 KB Output is correct
6 Correct 13 ms 23748 KB Output is correct
7 Correct 11 ms 23736 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 13 ms 23764 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Correct 14 ms 23792 KB Output is correct
12 Correct 13 ms 23764 KB Output is correct
13 Correct 12 ms 23728 KB Output is correct
14 Correct 11 ms 23804 KB Output is correct
15 Correct 11 ms 23764 KB Output is correct
16 Correct 12 ms 23760 KB Output is correct
17 Correct 11 ms 23764 KB Output is correct
18 Correct 11 ms 23800 KB Output is correct
19 Correct 11 ms 23764 KB Output is correct
20 Correct 12 ms 23892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23764 KB Output is correct
2 Correct 12 ms 23772 KB Output is correct
3 Correct 11 ms 23692 KB Output is correct
4 Correct 11 ms 23692 KB Output is correct
5 Correct 11 ms 23764 KB Output is correct
6 Correct 13 ms 23748 KB Output is correct
7 Correct 11 ms 23736 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 13 ms 23764 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Correct 14 ms 23792 KB Output is correct
12 Correct 13 ms 23764 KB Output is correct
13 Correct 12 ms 23728 KB Output is correct
14 Correct 11 ms 23804 KB Output is correct
15 Correct 11 ms 23764 KB Output is correct
16 Correct 12 ms 23760 KB Output is correct
17 Correct 11 ms 23764 KB Output is correct
18 Correct 11 ms 23800 KB Output is correct
19 Correct 11 ms 23764 KB Output is correct
20 Correct 12 ms 23892 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 12 ms 23908 KB Output is correct
23 Correct 13 ms 23892 KB Output is correct
24 Correct 11 ms 23892 KB Output is correct
25 Correct 12 ms 23892 KB Output is correct
26 Correct 12 ms 23764 KB Output is correct
27 Correct 12 ms 23892 KB Output is correct
28 Correct 12 ms 23888 KB Output is correct
29 Correct 11 ms 23764 KB Output is correct
30 Correct 11 ms 23764 KB Output is correct
31 Correct 12 ms 23892 KB Output is correct
32 Correct 14 ms 23764 KB Output is correct
33 Correct 11 ms 23680 KB Output is correct
34 Correct 14 ms 23764 KB Output is correct
35 Correct 12 ms 23800 KB Output is correct
36 Correct 11 ms 23752 KB Output is correct
37 Correct 11 ms 23784 KB Output is correct
38 Correct 11 ms 23848 KB Output is correct
39 Correct 11 ms 23716 KB Output is correct
40 Correct 11 ms 23804 KB Output is correct
41 Correct 11 ms 23764 KB Output is correct
42 Correct 11 ms 23744 KB Output is correct
43 Correct 11 ms 23832 KB Output is correct
44 Correct 13 ms 23764 KB Output is correct
45 Correct 12 ms 23764 KB Output is correct
46 Correct 13 ms 23844 KB Output is correct
47 Correct 12 ms 23760 KB Output is correct
48 Correct 11 ms 23784 KB Output is correct
49 Correct 13 ms 23860 KB Output is correct
50 Correct 11 ms 23764 KB Output is correct
51 Correct 11 ms 23764 KB Output is correct
52 Correct 13 ms 23764 KB Output is correct
53 Correct 11 ms 23764 KB Output is correct
54 Correct 13 ms 23764 KB Output is correct
55 Correct 13 ms 23828 KB Output is correct
56 Correct 11 ms 23764 KB Output is correct
57 Correct 12 ms 23764 KB Output is correct
58 Correct 11 ms 23892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23764 KB Output is correct
2 Correct 12 ms 23772 KB Output is correct
3 Correct 11 ms 23692 KB Output is correct
4 Correct 11 ms 23692 KB Output is correct
5 Correct 11 ms 23764 KB Output is correct
6 Correct 13 ms 23748 KB Output is correct
7 Correct 11 ms 23736 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 13 ms 23764 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Correct 14 ms 23792 KB Output is correct
12 Correct 13 ms 23764 KB Output is correct
13 Correct 12 ms 23728 KB Output is correct
14 Correct 11 ms 23804 KB Output is correct
15 Correct 11 ms 23764 KB Output is correct
16 Correct 12 ms 23760 KB Output is correct
17 Correct 11 ms 23764 KB Output is correct
18 Correct 11 ms 23800 KB Output is correct
19 Correct 11 ms 23764 KB Output is correct
20 Correct 12 ms 23892 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 12 ms 23908 KB Output is correct
23 Correct 13 ms 23892 KB Output is correct
24 Correct 11 ms 23892 KB Output is correct
25 Correct 12 ms 23892 KB Output is correct
26 Correct 12 ms 23764 KB Output is correct
27 Correct 12 ms 23892 KB Output is correct
28 Correct 12 ms 23888 KB Output is correct
29 Correct 11 ms 23764 KB Output is correct
30 Correct 11 ms 23764 KB Output is correct
31 Correct 12 ms 23892 KB Output is correct
32 Correct 14 ms 23764 KB Output is correct
33 Correct 11 ms 23680 KB Output is correct
34 Correct 14 ms 23764 KB Output is correct
35 Correct 12 ms 23800 KB Output is correct
36 Correct 11 ms 23752 KB Output is correct
37 Correct 11 ms 23784 KB Output is correct
38 Correct 11 ms 23848 KB Output is correct
39 Correct 11 ms 23716 KB Output is correct
40 Correct 11 ms 23804 KB Output is correct
41 Correct 11 ms 23764 KB Output is correct
42 Correct 11 ms 23744 KB Output is correct
43 Correct 11 ms 23832 KB Output is correct
44 Correct 13 ms 23764 KB Output is correct
45 Correct 12 ms 23764 KB Output is correct
46 Correct 13 ms 23844 KB Output is correct
47 Correct 12 ms 23760 KB Output is correct
48 Correct 11 ms 23784 KB Output is correct
49 Correct 13 ms 23860 KB Output is correct
50 Correct 11 ms 23764 KB Output is correct
51 Correct 11 ms 23764 KB Output is correct
52 Correct 13 ms 23764 KB Output is correct
53 Correct 11 ms 23764 KB Output is correct
54 Correct 13 ms 23764 KB Output is correct
55 Correct 13 ms 23828 KB Output is correct
56 Correct 11 ms 23764 KB Output is correct
57 Correct 12 ms 23764 KB Output is correct
58 Correct 11 ms 23892 KB Output is correct
59 Correct 352 ms 41764 KB Output is correct
60 Correct 398 ms 43836 KB Output is correct
61 Correct 11 ms 23764 KB Output is correct
62 Correct 10 ms 23756 KB Output is correct
63 Correct 284 ms 40040 KB Output is correct
64 Correct 12 ms 24788 KB Output is correct
65 Correct 20 ms 27700 KB Output is correct
66 Correct 120 ms 58644 KB Output is correct
67 Correct 424 ms 85608 KB Output is correct
68 Correct 424 ms 87212 KB Output is correct
69 Correct 16 ms 24532 KB Output is correct
70 Correct 77 ms 30852 KB Output is correct
71 Correct 251 ms 46888 KB Output is correct
72 Correct 327 ms 46160 KB Output is correct
73 Correct 70 ms 29068 KB Output is correct
74 Correct 409 ms 42780 KB Output is correct
75 Correct 30 ms 26312 KB Output is correct
76 Correct 282 ms 46576 KB Output is correct
77 Correct 314 ms 46828 KB Output is correct
78 Correct 34 ms 29268 KB Output is correct
79 Correct 463 ms 85724 KB Output is correct
80 Correct 12 ms 23948 KB Output is correct
81 Correct 107 ms 33048 KB Output is correct
82 Correct 349 ms 51300 KB Output is correct
83 Correct 403 ms 49392 KB Output is correct
84 Correct 385 ms 49384 KB Output is correct
85 Correct 444 ms 49320 KB Output is correct
86 Correct 385 ms 50424 KB Output is correct
87 Correct 414 ms 50424 KB Output is correct