Submission #152380

# Submission time Handle Problem Language Result Execution time Memory
152380 2019-09-07T20:25:38 Z Breno_XD Poklon (COCI17_poklon7) C++14
120 / 120
386 ms 72660 KB
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second

const int MAXN = 1000010;
int n, aans, bans;
int nivel[MAXN];
queue<pair<int,int> > fila;

struct nodes{

	int l,r;

};

nodes node[MAXN];

void dfs(int f, int p){

	// cout << "Demonstracao: " << f << "    " << p << endl;

	nivel[f] = nivel[p] + 1;

	if(node[f].r < 0) fila.push(make_pair(abs(node[f].r) , nivel[f]+1));		
	else dfs(node[f].r, f);

	if(node[f].l < 0) fila.push(make_pair(abs(node[f].l) , nivel[f]+1));
	else dfs(node[f].l, f);

}


int main(){

	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

	cin >> n;

	for(int i=1; i<=n; i++) cin >> node[i].l >> node[i].r;

	nivel[0] = -1;
	dfs(1,0);

	while(!fila.empty()){

		pair<int,int> atual = fila.front();
		fila.pop();

		//Vamos compara ans e atual.x

		// cout << "Atual: " << atual.x << "   " << atual.y << endl;
		// cout << "Ans:   " << aans << "   " << bans << endl; 

		//Primeiro verificamos o último bit
		int tam = 32-__builtin_clz(atual.x) + atual.y;
		int tamans = 32-__builtin_clz(aans) + bans;

		if(tamans != tam){

			if(tamans < tam){

				// cout << "Atualizei tamdiff\n";
				aans = atual.x;
				bans = atual.y;

			}

		}else{

			tam = 31-__builtin_clz(atual.x);
			tamans = 31-__builtin_clz(aans);

			int size = min(tam, tamans);

			for(int i=size; i>=0; i--){

				// cout << "i: " << i << endl;
				// cout << "Tam: " << tam << "    tamans: " << tamans << endl; 

				bool ok = (atual.x&(1<<tam));
				bool okans = (aans&(1<<tamans));

				if(ok && !okans){
					//cout << "Atualizei tamiguais\n";
					aans = atual.x;
					bans = atual.y;
					break;
				}
				if(!ok && okans) break;

				tam--;
				tamans--;

			}

		}

	}

	bool verifica = false;
	for(int i=30; i>=0; i--){

		if((aans&(1<<i))) verifica = true;

		if(!verifica) continue;

		bool bit = (aans&(1<<i));

		cout << bit;

	}

	for(int i=1; i<=bans; i++){

		cout << 0;

	}

	cout << "\n";

}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 380 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 376 KB Output is correct
11 Correct 6 ms 1016 KB Output is correct
12 Correct 7 ms 888 KB Output is correct
13 Correct 19 ms 3192 KB Output is correct
14 Correct 36 ms 5496 KB Output is correct
15 Correct 33 ms 3048 KB Output is correct
16 Correct 118 ms 13688 KB Output is correct
17 Correct 274 ms 28988 KB Output is correct
18 Correct 280 ms 31824 KB Output is correct
19 Correct 327 ms 32776 KB Output is correct
20 Correct 386 ms 72660 KB Output is correct