Submission #536027

# Submission time Handle Problem Language Result Execution time Memory
536027 2022-03-12T07:10:07 Z akhan42 Uzastopni (COCI15_uzastopni) C++17
160 / 160
30 ms 24848 KB
#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);

		for(ii p: links) {
			if(jokes[node] < min(p.F, p.S))
				loc_gr[p.F - 1].pb(p.S);
			else if(jokes[node] > max(p.F, p.S))
				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 time Memory Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 8 ms 724 KB Output is correct
12 Correct 8 ms 724 KB Output is correct
13 Correct 8 ms 816 KB Output is correct
14 Correct 30 ms 24844 KB Output is correct
15 Correct 30 ms 24848 KB Output is correct
16 Correct 28 ms 24788 KB Output is correct
17 Correct 8 ms 724 KB Output is correct
18 Correct 8 ms 724 KB Output is correct
19 Correct 10 ms 768 KB Output is correct
20 Correct 9 ms 724 KB Output is correct