#include <bits/stdc++.h>
using namespace std;
void maximize(vector<int>& ans, vector<int>& res) {
for (int i = 0; i < (int)min(ans.size(), res.size()); ++ i) {
if (ans[i] != res[i]) {
if (ans[i] < res[i]) {
swap(ans, res);
return;
}
if (ans[i] > res[i]) return;
}
}
if (ans.size() < res.size()) swap(ans, res);
}
int n;
pair<int, int> scale[1000005];
vector<int> ans;
int tmp[30];
void update(int u, int d, int cnt = 0) {
for (int i = 29; i >= 0; -- i) if ((u >> i) & 1)
tmp[++cnt] = i+d;
vector<int> res(tmp+1, tmp+1+cnt);
maximize(ans, res);
}
inline void calc() {
stack<pair<int, int>> st;
st.push({1, 1});
while (!st.empty()) {
int u = st.top().first;
int d = st.top().second;
st.pop();
if (scale[u].first > 0)
st.push({scale[u].first, d + 1});
else update(-scale[u].first, d);
if (scale[u].second > 0)
st.push({scale[u].second, d + 1});
else update(-scale[u].second, d);
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; ++ i)
cin >> scale[i].first
>> scale[i].second;
calc();
ans.push_back(-1);
for (int j = 0; j + 1 < (int)ans.size(); ++ j) {
cout << 1;
for (int h = ans[j] - 1; h > ans[j+1]; -- h) cout << 0;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
260 KB |
Output is correct |
2 |
Correct |
0 ms |
316 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
0 ms |
208 KB |
Output is correct |
7 |
Correct |
0 ms |
208 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
5 ms |
564 KB |
Output is correct |
12 |
Correct |
5 ms |
592 KB |
Output is correct |
13 |
Correct |
22 ms |
1536 KB |
Output is correct |
14 |
Correct |
38 ms |
2796 KB |
Output is correct |
15 |
Correct |
37 ms |
2648 KB |
Output is correct |
16 |
Correct |
126 ms |
9048 KB |
Output is correct |
17 |
Correct |
298 ms |
20936 KB |
Output is correct |
18 |
Correct |
322 ms |
21308 KB |
Output is correct |
19 |
Correct |
367 ms |
25944 KB |
Output is correct |
20 |
Correct |
382 ms |
26416 KB |
Output is correct |