Cod sursa(job #956511)

Utilizator burakbugrulBurak Bugrul burakbugrul Data 3 iunie 2013 12:04:24
Problema Guvern Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>

#define fi first
#define se second
#define pb push_back

using namespace std;

typedef pair<int,int> ii;

const int MAXN=100010;

int N,res;
int ar[MAXN];
int go[MAXN];
int pre[MAXN];

set<ii> s;
vector<int> way[MAXN];

void dfs( int nd ){
	
	s.insert(ii(ar[nd],nd));
	set<ii>::iterator it=s.lower_bound(ii(ar[nd]+1,0));
	
	bool fl=1;
	
	if( it!=s.end() )
		go[nd]=it->se;
	
	for( vector<int>::iterator it2=way[nd].begin() ; it2!=way[nd].end() ; it2++ )
		if( *it2!=pre[nd] ){
			fl=0;
			pre[*it2]=nd;
			dfs(*it2);
		}
	
	it=s.find(ii(ar[nd],nd));
	s.erase(it);
	res+=fl;
}

int main(){
	
	freopen("guvern.in","r",stdin);
	freopen("guvern.out","w",stdout);
	
	scanf(" %d",&N);
	
	for( int a,b,i=1 ; i<N ; i++ ){
		scanf(" %d %d",&a,&b);
		way[a].pb(b);
		way[b].pb(a);
	}
	
	for( int i=1 ; i<=N ; i++ )
		scanf(" %d",ar+i);
	
	dfs(1);
	printf("%d\n",res);
	return 0;
}