Submission #1235938

#TimeUsernameProblemLanguageResultExecution timeMemory
1235938zerone.Poklon (COCI17_poklon7)C++20
0 / 120
963 ms287932 KiB
#include <bits/stdc++.h>

#define ll long long
#define en "\n"
#define pb push_back
#define mp make_pair
#define pii pair<ll,ll>
#define fi first
#define se second
#define rep(i, a, b) for(ll i = a; i <= b; ++i)
#define per(i, a, b) for(ll i = a; i >= b; --i)
#define kaizokuO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define r0 return 0
#define all(v) v.begin(), v.end()
#define MAX(v) *max_element(all(v))
#define MIN(v) *min_element(all(v))
#define gcd(a,b) __gcd(a,b);
#define lcm(a,b) (a*b)/ __gcd(a, b)
#define bitt(x) __builtin_popcount(x)
#define fill1(arr, i, n) for(ll a = 0; a <= n; ++a) arr[a] = i;
#define fill2(arr, i, n, m) for(ll a = 0; a <= n; ++a) for(ll b = 0; b <= m; ++b) arr[a][b] = i;
#define flsh fflush(stdout)
#define ext exit(0)
#define inp freopen("input.txt","r",stdin)
#define outp freopen("output.txt","w",stdout)
 
using namespace std;
 
//const ll mod = 1e9 + 7;
const ll mod = 998244353;
//const ll mod = 26101991;
//const ll mod = 1e8;
//const ll mod = 1e9+9;
//const double pi = 3.14159265;
vector<ll> adj[1000003];
string w[1000003];
ll w2[1000003];
ll dfs(ll node){
    ll a = -1;
    for(auto i : adj[node]){
        if(i<0){
            if(a==-1){
                a = abs(i);
            } else {
                if(w[abs(i)].length()>w[a].length()) a = abs(i);
                else if(w[abs(i)].length()==w[abs(i)].length()) {
                    if(w2[abs(i)]<w2[a])a = abs(i);
                }
            }
        } else {
            if(a==-1){
                a = dfs(i);
            } else {
                ll t = dfs(i);
                if(w[t].length()>w[a].length()) a = t;
                else if(w[t].length()==w[a].length()) {
                    if(w2[t]<w2[a])a  = t;
                }
            }
        }
    }
    w[a] = w[a]+'0';
    return a;
}
void solve(){
    ll n; cin >> n;
    ll idx = 1;
    for(ll i = 1; i <= n; ++i){
        ll x; cin >> x;
        if(x < 0){
            string t = "";
            x = abs(x);
            w2[idx] = x; 
            while(x){
                t = to_string(x%2)+t;
                x /= 2; 
            }
            w[idx] = t;
            adj[i].pb(-idx);
            idx++;
        } else {
            adj[i].pb(x);
        }
        cin >> x;
        if(x < 0){
            string t = "";
            x = abs(x);
            w2[idx]=x;
            while(x){
                t = to_string(x%2)+t;
                x /= 2; 
            }
            w[idx] = t;
            adj[i].pb(-idx);
            idx++;
        } else {
            adj[i].pb(x);
        }
    }
    cout << w[dfs(1)] << en;

}
int main() {
    kaizokuO;
 
    ll t = 1; //cin >> t;
    while(t--){
        solve();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...