Submission #536011

#TimeUsernameProblemLanguageResultExecution timeMemory
536011akhan42Uzastopni (COCI15_uzastopni)C++17
88 / 160
40 ms50252 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

#define F first
#define S second
#define forn(i, n) for(int i = 0; i < n; ++i)
#define forbn(i, b, n) for(int i = b; i < n; ++i)
#define sz(v) (int)v.size()
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define min3(a, b, c) min(a, min(b, c))
#define lc v << 1
#define rc (v << 1) + 1

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long ll;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

template <class T> inline void mineq(T &a, T b) { a = min(a, b); }
template <class T> inline void maxeq(T &a, T b) { a = max(a, b); }


const int MX = 10 * 1000 + 10;

vi gr[MX];
int jokes[MX];
vi* gl_gr;
vi att;


void dfs2(int node) {
	att[node] = true;

	for(int nb: gl_gr[node]) {
		if(!att[nb])
			dfs2(nb);
	}
}


vii dfs(int node = 0) {
	vi loc_gr[100];

	for(int nb: gr[node]) {
		vii links = dfs(nb);

		if(jokes[node] < jokes[nb]) {
			for(ii p: links) {
				loc_gr[p.F - 1].pb(p.S);
			}
		}
		else if(jokes[node] > jokes[nb]) {
			for(ii p: links) {
				loc_gr[p.S + 1].pb(p.F);
			}
		}
	}

	att.assign(100, false);
	gl_gr = loc_gr;

	dfs2(jokes[node]);
	vii links;

	forbn(l, 0, jokes[node] + 1)
		if(att[l])
			forbn(r, jokes[node], 100)
				if(att[r])
					links.eb(l, r);

	return links;
}



void solve() {
	int n;
	cin >> n;

	forn(i, n) {
		cin >> jokes[i];
		jokes[i]--;
	}

	forn(_, n - 1) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		gr[a].pb(b);
	}

	vii links = dfs(0);

	cout << sz(links);
}


int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

//	freopen("triangles.in", "r", stdin);
//	freopen("triangles.out", "w", stdout);

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