Submission #1196391

#TimeUsernameProblemLanguageResultExecution timeMemory
1196391InvMODUzastopni (COCI15_uzastopni)C++17
160 / 160
153 ms18264 KiB
#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define eb emplace_back

#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())

template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}

#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"

using ll = long long;
using ld = long double;

template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);}

const int N = 1e4 + 5, V = 105;

/*
    different set mean a different array after sort
    if i go from 1 -> 2 -> 3 and get {1, 2, 3} and i go from 1 - > 4 -> 5 and get {1, 3, 2}
        -> both still the same
*/

int n, a[N];

vector<int> adj[N]; bitset<V> dp[N][V]; // dp[x][l][r]: at x and has set {l, r}

void calc_dp(int x, int p){
    dp[x][a[x]][a[x]] = 1;

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

        for(int i = 1; i <= 100; i++){
            for(int j = i; j <= 100; j++){
                if(dp[v][i][j] && (a[x] > j || a[x] < i)){
                    dp[x][i][j] = 1;
                }
            }
        }
    }

    FOR(i, 1, 100){
        FOR(j, i + 1, 100){
            if(dp[x][i][j - 1]) dp[x][i] |= dp[x][j];
        }
    }

    FOR(i, 1, 100) FOR(j, i, 100){
        if(dp[x][i][j] && (a[x] > j || a[x] < i)) dp[x][i][j] = 0;
    }
}


void Main()
{
    cin >> n;
    FOR(i, 1, n) cin >> a[i];

    FOR(i, 2, n){
        int u,v; cin >> u >> v;
        adj[u].eb(v), adj[v].eb(u);
    }

    calc_dp(1, -1);

    int ans = 0;
    FOR(i, 1, 100) ans += dp[1][i].count();

    cout << ans << el;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP", "r", stdin);
        freopen(name".OUT", "w", stdout);
    }

    int t = 1; while(t--) Main();
    return 0;
}

Compilation message (stderr)

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