#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int N=1000005;
int n,L[N],R[N];
pair<int,int>val[N];
pair<int,int>Max(pair<int,int>a,pair<int,int>b){
if(a.first==0)return b;
if(b.first==0)return a;
while(a.first<b.first&&a.second){
a.first*=2;
a.second--;
}
if(a.first>=b.first&&a.second>=b.second){
while((a.first&1)==0){
a.first>>=1;
a.second++;
}
return a;
}else return b;
}
void dfs(int v){
pair<int,int>l={0,0},r={0,0};
if(L[v]>0){
dfs(L[v]);
l=val[L[v]];
}else l={-L[v],0};
if(R[v]>0){
dfs(R[v]);
r=val[R[v]];
}else r={-R[v],0};
val[v]=Max(l,r);
val[v].second++;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)cin>>L[i]>>R[i];
dfs(1);
string s="";
while(val[1].first){
s+=char('0'+(val[1].first&1));
val[1].first/=2;
}
reverse(s.begin(),s.end());
cout<<s;
for(int i=0;i<val[1].second;i++)cout<<0;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
11 |
Incorrect |
3 ms |
852 KB |
Output isn't correct |
12 |
Incorrect |
3 ms |
852 KB |
Output isn't correct |
13 |
Correct |
12 ms |
3028 KB |
Output is correct |
14 |
Incorrect |
25 ms |
5964 KB |
Output isn't correct |
15 |
Incorrect |
22 ms |
3452 KB |
Output isn't correct |
16 |
Correct |
86 ms |
17352 KB |
Output is correct |
17 |
Incorrect |
187 ms |
38648 KB |
Output isn't correct |
18 |
Incorrect |
199 ms |
41192 KB |
Output isn't correct |
19 |
Incorrect |
226 ms |
41704 KB |
Output isn't correct |
20 |
Correct |
293 ms |
81348 KB |
Output is correct |