-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path1097 - Code the Tree.cpp
82 lines (68 loc) · 1.57 KB
/
1097 - Code the Tree.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
82
/*
Idea:
- Using recursion we can build the tree from the input.
- When the tree is ready to use we can use priority queue
to track the leaf nodes.
- Each time we remove a node from the tree we can remove it
from the `vector` of the parent node.
*/
#include <bits/stdc++.h>
using namespace std;
char s[10001];
int n, len;
vector<int> sol;
vector<vector<int> > g;
priority_queue<int> pq;
int getInt(int idx) {
int ret = 0;
while(isdigit(s[idx]))
ret *= 10, ret += (s[idx++] - '0');
return ret;
}
void build(int idx, int par) {
int cur = getInt(idx);
n = max(n, cur);
if(par != -1)
g[par].push_back(cur),
g[cur].push_back(par);
for(int i = idx, cnt = 1; i < len; ++i) {
cnt += s[i] == '(';
cnt -= s[i] == ')';
if(cnt == 0)
break;
if(s[i] == '(' && cnt == 2)
build(i + 1, cur);
}
}
void rmv(int from, int target) {
for(int i = 0; i < g[from].size(); ++i)
if(g[from][i] == target) {
g[from].erase(g[from].begin() + i);
break;
}
}
int main() {
while(fgets(s, sizeof s, stdin)) {
n = 0;
len = strlen(s);
g.clear();
g.resize(1001);
build(1, -1);
for(int i = 1; i <= n; ++i)
if(g[i].size() == 1)
pq.push(-i);
sol.clear();
while(!pq.empty()) {
int top = -pq.top();
pq.pop();
sol.push_back(g[top][0]);
rmv(g[top][0], top);
if(g[g[top][0]].size() == 1)
pq.push(-g[top][0]);
}
for(int i = 0; i < int(sol.size()) - 1; ++i)
printf("%s%d", i == 0 ? "" : " ", sol[i]);
puts("");
}
return 0;
}