답안 #346728

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
346728 2021-01-10T20:18:17 Z evn Network (BOI15_net) C++14
100 / 100
1589 ms 165092 KB
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define sz(a) a.size()
typedef long long ll;
typedef pair<ll, ll> pii;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
vector<int> adj[500005];
bool isLeaf[500005];
set<int> leafs[500005];
vector<pii> links;
int root = -1;
int N;
void dfs(int v, int p){
	//cout << v << endl;
	assert(v >= 0 && v < N);
	for(int u : adj[v]){
		if(u == p)continue;
		isLeaf[v] = false;
		dfs(u,v);
	}
	if(isLeaf[v]){
		//cout << v << '\n';
		leafs[v].insert(v);
	}
	vector<int> one;
	vector<int> two;
	map<int, int> imp;
	for(int u : adj[v]){
		if(u == p)continue;
		if(leafs[u].size() == 1)one.pb(u);
		if(leafs[u].size() == 2)two.pb(u);
		if(v == root){
			imp[u] = *leafs[u].begin();
		}
		assert(leafs[u].size() == 1 || leafs[u].size() == 2);
		for(int x : leafs[u]){
			leafs[v].insert(x);
		}
	}
	//cout << v << " " << leafs[v].size() << " " << (int)leafs[v].size()-2 << endl;
	//cout << v << " " << one.size() << " " << two.size() << "\n";
 	
	while((ll)leafs[v].size() - 2 >= 1LL){
		//cout << "hi\n";
		assert(leafs[v].size() == one.size() + 2 * two.size());
		if(!two.empty()){
			int tw = two[two.size()-1];
			two.pop_back();
			int o = -1;
			if(!two.empty()){
				o = two[two.size()-1];		
				two.pop_back();	
				one.push_back(o);
			}
			else{
				assert(!one.empty());
				o = one[one.size()-1];
				one.pop_back();
			}
 
			one.push_back(tw);
			assert(o != -1 && tw != o);
			//cout << tw << " " << o << '\n';
			//cout << *leafs[tw].begin() << " " << *leafs[o].begin() << '\n';
			//cout << *leafs[tw].begin() << " " << *(--leafs[tw].end()) << '\n';
			//link with back of one
			links.pb({*leafs[tw].begin(), *leafs[o].begin()});
			leafs[v].erase(*leafs[tw].begin());
			leafs[v].erase(*leafs[o].begin());
			leafs[tw].erase(*leafs[tw].begin());
			leafs[o].erase(*leafs[o].begin());
 
		}
		else{
			assert(one.size() >= 2);
			int ba = one[one.size()-1];
			one.pop_back();
			int ba2 = one[one.size()-1];
			one.pop_back();
			assert(ba != ba2);
			links.pb({*leafs[ba].begin(), *leafs[ba2].begin()});
			leafs[v].erase(*leafs[ba].begin());
			leafs[v].erase(*leafs[ba2].begin());
			leafs[ba].erase(*leafs[ba].begin());
			leafs[ba2].erase(*leafs[ba2].begin());
		}
	}
	if(v == root){
		//remove the final ones
		assert(p == -1);
		assert(leafs[v].size() == 1 || leafs[v].size() == 2 || leafs[v].size() == 0);
		if(leafs[v].size() == 0){
			assert(one.size() == 0 && two.size() == 0);
			return;
		}
		else if(leafs[v].size() == 2){
			assert(one.size() == 2 && two.size() == 0);
			assert(one[0] != one[1]);
			links.pb({*leafs[v].begin(), *(--leafs[v].end())});
		}
		else{
			//cout << *leafs[one[0]].begin() << '\n';
			assert(one.size() == 1 && two.size() == 0);
			for(int u : adj[v]){
				if(u != p && u != one[0]){
					//cout << u << '\n';
					//cout << *leafs[u].begin() << '\n'
					assert(imp[u] != *leafs[one[0]].begin());
					links.pb({imp[u], *leafs[one[0]].begin()});
					break;
				}
			}
		}
	}
}
int32_t main(){
 
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> N;
	for(int i =0;i < N-1; i++){
		int a, b;
		cin >> a >> b;
		isLeaf[i]=true;
		a--;
		b--;
		adj[a].pb(b);
		adj[b].pb(a);
	}
	isLeaf[N-1]=true;
	for(int i = 0; i < N; i++){
		if(adj[i].size() > 1){
			root = i;
			isLeaf[root] = false;
			break;
		}
	}
	assert(root != -1);
	dfs(root, -1);
	int leafs = 0;
	for(int i = 0; i < N; i++){
		if(isLeaf[i])leafs++;
	}
	cout << links.size() << '\n';
	assert((ll)links.size() == (1LL * leafs+1)/2);
	for(int i = 0; i < (int)links.size(); i++){
		assert(links[i].f != links[i].s);
		assert(links[i].f >= 0 && links[i].f < N);
		assert(links[i].s >= 0 && links[i].s < N);
		cout << links[i].f + 1 << " " << links[i].s + 1 << '\n';
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 35564 KB Output is correct
2 Correct 23 ms 35564 KB Output is correct
3 Correct 23 ms 35564 KB Output is correct
4 Correct 23 ms 35564 KB Output is correct
5 Correct 25 ms 35692 KB Output is correct
6 Correct 23 ms 35564 KB Output is correct
7 Correct 23 ms 35564 KB Output is correct
8 Correct 23 ms 35564 KB Output is correct
9 Correct 23 ms 35564 KB Output is correct
10 Correct 23 ms 35564 KB Output is correct
11 Correct 24 ms 35692 KB Output is correct
12 Correct 26 ms 35584 KB Output is correct
13 Correct 23 ms 35564 KB Output is correct
14 Correct 23 ms 35584 KB Output is correct
15 Correct 23 ms 35564 KB Output is correct
16 Correct 24 ms 35564 KB Output is correct
17 Correct 24 ms 35564 KB Output is correct
18 Correct 25 ms 35564 KB Output is correct
19 Correct 23 ms 35564 KB Output is correct
20 Correct 23 ms 35564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 35564 KB Output is correct
2 Correct 23 ms 35564 KB Output is correct
3 Correct 23 ms 35564 KB Output is correct
4 Correct 23 ms 35564 KB Output is correct
5 Correct 25 ms 35692 KB Output is correct
6 Correct 23 ms 35564 KB Output is correct
7 Correct 23 ms 35564 KB Output is correct
8 Correct 23 ms 35564 KB Output is correct
9 Correct 23 ms 35564 KB Output is correct
10 Correct 23 ms 35564 KB Output is correct
11 Correct 24 ms 35692 KB Output is correct
12 Correct 26 ms 35584 KB Output is correct
13 Correct 23 ms 35564 KB Output is correct
14 Correct 23 ms 35584 KB Output is correct
15 Correct 23 ms 35564 KB Output is correct
16 Correct 24 ms 35564 KB Output is correct
17 Correct 24 ms 35564 KB Output is correct
18 Correct 25 ms 35564 KB Output is correct
19 Correct 23 ms 35564 KB Output is correct
20 Correct 23 ms 35564 KB Output is correct
21 Correct 23 ms 35564 KB Output is correct
22 Correct 25 ms 35948 KB Output is correct
23 Correct 25 ms 35948 KB Output is correct
24 Correct 25 ms 35948 KB Output is correct
25 Correct 27 ms 35948 KB Output is correct
26 Correct 24 ms 35692 KB Output is correct
27 Correct 25 ms 35820 KB Output is correct
28 Correct 25 ms 35820 KB Output is correct
29 Correct 25 ms 35692 KB Output is correct
30 Correct 23 ms 35564 KB Output is correct
31 Correct 26 ms 35820 KB Output is correct
32 Correct 23 ms 35564 KB Output is correct
33 Correct 27 ms 35564 KB Output is correct
34 Correct 23 ms 35564 KB Output is correct
35 Correct 23 ms 35564 KB Output is correct
36 Correct 23 ms 35564 KB Output is correct
37 Correct 24 ms 35692 KB Output is correct
38 Correct 27 ms 35820 KB Output is correct
39 Correct 23 ms 35564 KB Output is correct
40 Correct 23 ms 35564 KB Output is correct
41 Correct 24 ms 35564 KB Output is correct
42 Correct 24 ms 35692 KB Output is correct
43 Correct 24 ms 35692 KB Output is correct
44 Correct 23 ms 35564 KB Output is correct
45 Correct 24 ms 35564 KB Output is correct
46 Correct 24 ms 35692 KB Output is correct
47 Correct 23 ms 35564 KB Output is correct
48 Correct 23 ms 35564 KB Output is correct
49 Correct 25 ms 35692 KB Output is correct
50 Correct 25 ms 35692 KB Output is correct
51 Correct 23 ms 35564 KB Output is correct
52 Correct 24 ms 35692 KB Output is correct
53 Correct 24 ms 35692 KB Output is correct
54 Correct 23 ms 35564 KB Output is correct
55 Correct 23 ms 35564 KB Output is correct
56 Correct 26 ms 35564 KB Output is correct
57 Correct 24 ms 35692 KB Output is correct
58 Correct 25 ms 35820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 35564 KB Output is correct
2 Correct 23 ms 35564 KB Output is correct
3 Correct 23 ms 35564 KB Output is correct
4 Correct 23 ms 35564 KB Output is correct
5 Correct 25 ms 35692 KB Output is correct
6 Correct 23 ms 35564 KB Output is correct
7 Correct 23 ms 35564 KB Output is correct
8 Correct 23 ms 35564 KB Output is correct
9 Correct 23 ms 35564 KB Output is correct
10 Correct 23 ms 35564 KB Output is correct
11 Correct 24 ms 35692 KB Output is correct
12 Correct 26 ms 35584 KB Output is correct
13 Correct 23 ms 35564 KB Output is correct
14 Correct 23 ms 35584 KB Output is correct
15 Correct 23 ms 35564 KB Output is correct
16 Correct 24 ms 35564 KB Output is correct
17 Correct 24 ms 35564 KB Output is correct
18 Correct 25 ms 35564 KB Output is correct
19 Correct 23 ms 35564 KB Output is correct
20 Correct 23 ms 35564 KB Output is correct
21 Correct 23 ms 35564 KB Output is correct
22 Correct 25 ms 35948 KB Output is correct
23 Correct 25 ms 35948 KB Output is correct
24 Correct 25 ms 35948 KB Output is correct
25 Correct 27 ms 35948 KB Output is correct
26 Correct 24 ms 35692 KB Output is correct
27 Correct 25 ms 35820 KB Output is correct
28 Correct 25 ms 35820 KB Output is correct
29 Correct 25 ms 35692 KB Output is correct
30 Correct 23 ms 35564 KB Output is correct
31 Correct 26 ms 35820 KB Output is correct
32 Correct 23 ms 35564 KB Output is correct
33 Correct 27 ms 35564 KB Output is correct
34 Correct 23 ms 35564 KB Output is correct
35 Correct 23 ms 35564 KB Output is correct
36 Correct 23 ms 35564 KB Output is correct
37 Correct 24 ms 35692 KB Output is correct
38 Correct 27 ms 35820 KB Output is correct
39 Correct 23 ms 35564 KB Output is correct
40 Correct 23 ms 35564 KB Output is correct
41 Correct 24 ms 35564 KB Output is correct
42 Correct 24 ms 35692 KB Output is correct
43 Correct 24 ms 35692 KB Output is correct
44 Correct 23 ms 35564 KB Output is correct
45 Correct 24 ms 35564 KB Output is correct
46 Correct 24 ms 35692 KB Output is correct
47 Correct 23 ms 35564 KB Output is correct
48 Correct 23 ms 35564 KB Output is correct
49 Correct 25 ms 35692 KB Output is correct
50 Correct 25 ms 35692 KB Output is correct
51 Correct 23 ms 35564 KB Output is correct
52 Correct 24 ms 35692 KB Output is correct
53 Correct 24 ms 35692 KB Output is correct
54 Correct 23 ms 35564 KB Output is correct
55 Correct 23 ms 35564 KB Output is correct
56 Correct 26 ms 35564 KB Output is correct
57 Correct 24 ms 35692 KB Output is correct
58 Correct 25 ms 35820 KB Output is correct
59 Correct 1033 ms 109596 KB Output is correct
60 Correct 720 ms 82988 KB Output is correct
61 Correct 23 ms 35692 KB Output is correct
62 Correct 23 ms 35564 KB Output is correct
63 Correct 580 ms 75480 KB Output is correct
64 Correct 27 ms 37356 KB Output is correct
65 Correct 44 ms 43116 KB Output is correct
66 Correct 323 ms 101468 KB Output is correct
67 Correct 814 ms 153324 KB Output is correct
68 Correct 855 ms 165092 KB Output is correct
69 Correct 40 ms 37868 KB Output is correct
70 Correct 289 ms 59872 KB Output is correct
71 Correct 1589 ms 152384 KB Output is correct
72 Correct 1215 ms 113720 KB Output is correct
73 Correct 167 ms 47968 KB Output is correct
74 Correct 704 ms 80988 KB Output is correct
75 Correct 87 ms 42780 KB Output is correct
76 Correct 1054 ms 97100 KB Output is correct
77 Correct 1253 ms 112568 KB Output is correct
78 Correct 78 ms 46828 KB Output is correct
79 Correct 823 ms 146232 KB Output is correct
80 Correct 27 ms 36076 KB Output is correct
81 Correct 246 ms 55264 KB Output is correct
82 Correct 847 ms 93356 KB Output is correct
83 Correct 687 ms 80604 KB Output is correct
84 Correct 699 ms 80484 KB Output is correct
85 Correct 690 ms 80476 KB Output is correct
86 Correct 724 ms 83156 KB Output is correct
87 Correct 715 ms 83032 KB Output is correct