Submission #1274436

#TimeUsernameProblemLanguageResultExecution timeMemory
1274436duongquanghai08Uzastopni (COCI15_uzastopni)C++20
0 / 160
2 ms580 KiB
#include <bits/stdc++.h>

using namespace std;

const int N = 1e4 + 5;
const int M = 101;

int n;
int a[N];
vector<int> adj[N];
bool dp[N][M];

void calc_dp(int x, int p) {
    for (int v : adj[x]) {
        if (v != p) {
            calc_dp(v, x);
        }
    }

    sort(adj[x].begin(), adj[x].end(), [&](int u, int v) {
        return a[u] < a[v];
    });

    int ptr = a[x] + 1;
    dp[x][a[x]] = true;
    for (int v : adj[x]) {
        if (a[v] <= a[x] || v == p) {
            continue;
        }

        if (dp[v][ptr]) {
            ptr = max(ptr, a[v]);
            while (ptr <= 100 && dp[v][ptr]) {
                dp[x][ptr] = true;
                ptr++;
            }
        }
    }

    sort(adj[x].begin(), adj[x].end(), [&](int u, int v) {
        return a[u] > a[v];
    });

    ptr = a[x] - 1;
    for (int v : adj[x]) {
        if (a[v] >= a[x] || v == p) {
            continue;
        }

        if (dp[v][ptr]) {
            ptr = min(ptr, a[v]);
            while (ptr >= 1 && dp[v][ptr]) {
                dp[x][ptr] = true;
                ptr--;
            }
        }
    }
}

void Solve() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    for (int i = 2; i <= n; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    calc_dp(1, -1);

    long long l = 1, r = 1;
    for (int i = 1; i < a[1]; i++) {
        if (dp[1][i]) {
            l++;
        }
    }
    for (int i = a[1] + 1; i <= 100; i++) {
        if (dp[1][i]) {
            r++;
        }
    }

    cout << l * r;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    freopen("GRAPH.INP", "r", stdin);
    freopen("GRAPH.OUT", "w", stdout);
    Solve();
}

Compilation message (stderr)

uzastopni.cpp: In function 'int main()':
uzastopni.cpp:93:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |     freopen("GRAPH.INP", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
uzastopni.cpp:94:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |     freopen("GRAPH.OUT", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...