# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
246224 | bibabas | Uzastopni (COCI15_uzastopni) | C++14 | 35 ms | 21532 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define vi vector<ll>
#define vvi vector<vi>
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define ld long double
#define pii pair<ll, ll>
#define mt make_tuple
#define mn(a, b) a = min(a, b)
#define mx(a, b) a = max(a, b)
using namespace std;
const ll INF = (ll)2e9;
const ll inf = (ll)2e18;
const ld eps = (ld)1e-8;
const ll mod = (ll)998244353;
const ll MAXN = (ll)1e4 + 1;
const ll MAXC = (ll)1e6 + 1;
const ll MAXE = (ll)1000;
const ll MAXLOG = 21;
const ll maxlen = (ll)1e5;
const ll asci = (ll)256;
const ll block = 480;
const ld PI = acos(-1);
const ld e = 2.7182818284;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<
pii,
null_type,
less<pii>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;*/
template <class T>
istream& operator >>(istream &in, vector<T> &arr){
for (T &cnt : arr) {
in >> cnt;
}
return in;
};
bool comp(const pii &v, const pii &u) {
if (v.second < u.second) return 1;
return 0;
}
vector<pii> graph[MAXN];
ll le[MAXN][101], re[MAXN][101];
void dfs(ll v, vi &a) {
for (pii u : graph[v]) {
dfs(u.first, a);
}
graph[v].push_back(mp(v, a[v]));
sort(all(graph[v]), comp);
le[v][a[v]] = 1, re[v][a[v]] = 1;
ll pos = 0;
for (ll i = 0; i < graph[v].size(); ++i) {
if (graph[v][i].first == v) pos = i;
}
for (ll i = pos + 1; i < graph[v].size(); ++i) {
bool fnd = 0;
for (ll j = 1; j <= 100; ++j) {
if (le[graph[v][i].first][j] && re[v][j - 1]) fnd = 1;
}
for (ll j = 0; j <= 100; ++j) {
re[v][j] |= re[graph[v][i].first][j] & fnd;
}
}
for (ll i = pos - 1; i >= 0; --i) {
bool fnd = 0;
for (ll j = 0; j < 100; ++j) {
if (re[graph[v][i].first][j] && le[v][j + 1]) fnd = 1;
}
for (ll j = 0; j <= 100; ++j) {
le[v][j] |= le[graph[v][i].first][j] & fnd;
}
}
}
void solve() {
ll n; cin >> n;
vi a(n); cin >> a;
for (ll i = 0; i < n - 1; ++i) {
ll v, u; cin >> v >> u;
v--, u--;
graph[v].push_back(mp(u, a[u]));
}
dfs(0, a);
ll x1 = 0, x2 = 0;
for (ll j = 0; j <= 100; ++j) {
x1 += le[0][j];
x2 += re[0][j];
}
cout << x1 * x2;
}
int main() {
srand(time(0));
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#endif
cout.precision(30);
solve();
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |