Submission #255424

# Submission time Handle Problem Language Result Execution time Memory
255424 2020-08-01T00:29:02 Z aZvezda Uzastopni (COCI15_uzastopni) C++14
160 / 160
47 ms 28032 KB
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
template<class T> inline void fix(T &x) {if(x >= mod | x <= -mod) {x %= mod;} if(x < 0) {x += mod;}}
#define out(x) cout << __LINE__ << ": " << (#x) << " = " << (x) << endl

const int MAX_N = 1e4 + 10, MAX_VAL = 1e2 + 10;
vector<int> g[MAX_N];
int arr[MAX_N];
bool used[MAX_VAL];

vector<pair<short, short> > dfs(int x, int p) {
    vector<short> fake[MAX_VAL];
    for(int i = 0; i < MAX_VAL; i ++) {
        fake[i].resize(0);
    }
    for(auto it : g[x]) {
        if(it == p) {continue;}
        auto dp = dfs(it, x);
        for(auto &inter : dp) {
            int i = inter.first;
            int j = inter.second;
            if(i <= arr[x] && j >= arr[x]) {continue;}
            if(i == arr[x] || j == arr[x]) {continue;}
            if(i < arr[x]) {
                fake[j].push_back(i - 1);
            } else {
                fake[i].push_back(j + 1);
            }
        }
    }

    for(int i = 0; i < MAX_VAL; i ++) {
        used[i] = false;
    }
    vector<int> foundl, foundr;
    vector<pair<short, short> > ret;
    ret.resize(0);
    queue<short> q;
    q.push(arr[x] - 1);
    q.push(arr[x] + 1);
    used[arr[x] + 1] = true;
    used[arr[x] - 1] = true;
    foundl.push_back(arr[x] - 1);
    foundr.push_back(arr[x] + 1);
    while(!q.empty()) {
        auto curr = q.front(); q.pop();
        for(auto it : fake[curr]) {
            if(!used[it]) {
                used[it] = true;
                if(it < arr[x]) {
                    foundl.push_back(it);
                } else {
                    foundr.push_back(it);
                }
                q.push(it);
            }
        }
    }

    for(auto l : foundl) {
        for(auto r : foundr) {
            ret.push_back({l + 1, r - 1});
        }
    }
    return ret;
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int n;
    cin >> n;
    for(int i = 1; i <= n; i ++) {
        cin >> arr[i];
    }
    for(int i = 0; i < n - 1; i ++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    cout << dfs(1, 0).size() << endl;
    return 0;
}


# Verdict Execution time Memory Grader output
1 Correct 1 ms 640 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 640 KB Output is correct
4 Correct 1 ms 640 KB Output is correct
5 Correct 1 ms 640 KB Output is correct
6 Correct 1 ms 768 KB Output is correct
7 Correct 1 ms 768 KB Output is correct
8 Correct 1 ms 768 KB Output is correct
9 Correct 1 ms 640 KB Output is correct
10 Correct 1 ms 640 KB Output is correct
11 Correct 12 ms 1024 KB Output is correct
12 Correct 14 ms 1024 KB Output is correct
13 Correct 13 ms 1024 KB Output is correct
14 Correct 44 ms 28032 KB Output is correct
15 Correct 47 ms 28032 KB Output is correct
16 Correct 47 ms 28032 KB Output is correct
17 Correct 13 ms 1024 KB Output is correct
18 Correct 12 ms 1024 KB Output is correct
19 Correct 17 ms 1024 KB Output is correct
20 Correct 14 ms 1024 KB Output is correct