Cod sursa(job #1474069)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 20 august 2015 21:05:07
Problema Asmax Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <fstream>
#include <vector>
#include <bitset>

const int MAX_N = 16000;
const char IN[ ] = "asmax.in";
const char OUT[ ] = "asmax.out";

using namespace std;

ifstream fin ( IN );
ofstream fout ( OUT );

vector < int > adj[ MAX_N ];

bitset < MAX_N > seen;

int cost[ MAX_N ];

void dfs( int node )
{
	seen[ node ] = 1;
	for ( auto son : adj[ node ] )
	{
		if ( !seen[ son ] )
		{
			cost[ node ] += cost[ son ] & -( cost[ son ] > 0 );
			dfs( son );
		}
	}
}

int main ( void )
{
	int n;
	int u, v;
	int ans;

	fin >> n;
	for ( int i = 0; i < n; i++ )
	{
		fin >> cost[ i ];
	}
	for (int i = 1; i < n; i++ )
	{
		fin >> u >> v;
		adj[ u - 1 ].push_back( v - 1 );
		adj[ v - 1 ].push_back( u - 1 );
	}
	fin.close( );

	ans = 0;
	for ( int i = 0; i < n; i++ )
	{
		dfs( i );
		ans = ans ^ ( ( ans ^ cost[ i ] ) & -( cost[ i ] > ans ) );
	}

	fout << ans << '\n';
	fout.close( );
	return 0;
}