-
Notifications
You must be signed in to change notification settings - Fork 2
/
Prüfer Code.cpp
46 lines (38 loc) · 930 Bytes
/
Prüfer Code.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ll long long
vector<pair<int, int>> pruefer_decode(vector<int> const& code) {
int n = code.size() + 2;
vector<int> degree(n, 1);
for (int i : code)
degree[i]++;
set<int> leaves;
for (int i = 0; i < n; i++) {
if (degree[i] == 1)
leaves.insert(i);
}
vector<pair<int, int>> edges;
for (int v : code) {
int leaf = *leaves.begin();
leaves.erase(leaves.begin());
edges.emplace_back(leaf, v);
if (--degree[v] == 1)
leaves.insert(v);
}
edges.emplace_back(*leaves.begin(), n-1);
return edges;
}
int main(){
int n;
cin>>n;
vector<int> codes;
for(int i=0;i<n-2;i++){
int a;
cin>>a;
a--;
codes.push_back(a);
}
for(auto [u,v]:pruefer_decode(codes))
cout<<u+1<<" "<<v+1<<endl;
}