forked from beet-aizu/library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreeconstruction.cpp
81 lines (78 loc) · 1.71 KB
/
treeconstruction.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <bits/stdc++.h>
using namespace std;
//BEGIN CUT HERE
vector <int> treeconstruction(vector <int> d) {
const int MAX = 111;
vector<int> res,NG;
int n=d.size();
int m=0;
int t[MAX]={};
queue<int> q[MAX];
for(int i=0;i<n;i++){
t[d[i]]++;
q[d[i]].push(i);
m=max(m,d[i]);
}
bool f=1;
for(int i=0;i<(m+1)/2;i++) f&=t[m-i]>=2;
if(m%2) f&=t[m/2+1]==2;
else f&=t[m/2]==1;
for(int i=0;i<n;i++){
if(m%2) f&=d[i]>=m/2+1;
else f&=d[i]>=m/2;
}
if(!f) return NG;
bool used[MAX]={};
int b[MAX][2]={};
if(m%2){
for(int i=0;i<=m/2;i++){
b[m-i][0]=q[m-i].front();q[m-i].pop();
b[m-i][1]=q[m-i].front();q[m-i].pop();
used[b[m-i][0]]=1;
used[b[m-i][1]]=1;
if(i){
res.push_back(b[m-i+1][0]);
res.push_back(b[m-i][0]);
res.push_back(b[m-i+1][1]);
res.push_back(b[m-i][1]);
}
if(i==m/2){
res.push_back(b[m-i][0]);
res.push_back(b[m-i][1]);
}
}
for(int i=0;i<n;i++){
if(used[i]) continue;
res.push_back(i);
res.push_back(b[d[i]-1][0]);
}
}else{
for(int i=0;i<m/2;i++){
b[m-i][0]=q[m-i].front();q[m-i].pop();
b[m-i][1]=q[m-i].front();q[m-i].pop();
used[b[m-i][0]]=1;
used[b[m-i][1]]=1;
if(i){
res.push_back(b[m-i+1][0]);
res.push_back(b[m-i][0]);
res.push_back(b[m-i+1][1]);
res.push_back(b[m-i][1]);
}
}
b[m/2][0]=q[m/2].front();q[m/2].pop();
used[b[m/2][0]]=1;
res.push_back(b[m/2+1][0]);
res.push_back(b[m/2][0]);
res.push_back(b[m/2+1][1]);
res.push_back(b[m/2][0]);
for(int i=0;i<n;i++){
if(used[i]) continue;
res.push_back(i);
res.push_back(b[d[i]-1][0]);
}
}
return res;
}
//END CUT HERE
int main(){
}