Submission #1277177

#TimeUsernameProblemLanguageResultExecution timeMemory
1277177vuquangsangUzastopni (COCI15_uzastopni)C++20
32 / 160
72 ms131072 KiB
#include <bits/stdc++.h>
using namespace std;

const int N = 105;

const int MAX = 105;

int n, a[N];
vector<int> adj[N];

bool dp[N][MAX][MAX], temp[MAX][MAX];
int lim;
void dfs(int u)
{
    for(int v : adj[u]) {
        dfs(v);
    }
    memset(temp, 0, sizeof temp);
    temp[a[u]][a[u]] = 1;
    for(int v : adj[u]) {
        for(int x = 1; x <= lim; x++) {
            for(int y = x; y <= lim; y++) temp[x][y] |= dp[v][x][y];
        }
    }
    for(int x = 1; x <= lim; x++) for(int y = x; y <= lim; y++) if(temp[x][y]){
        for(int len = y + 1; len <= lim; len++) temp[x][y] |= temp[y + 1][len];
    }
    dp[u][a[u]][a[u]] = 1;
    for(int x = 1; x <= lim; x++) for(int y = x; y <= lim; y++) {
        if(temp[x][a[u] - 1] && temp[a[u] + 1][y]) dp[u][x][y] = 1;
        if(x == a[u] && temp[x + 1][y]) dp[u][x][y] = 1;
        if(y == a[u] && temp[x][y - 1]) dp[u][x][y] = 1;
    }

}
void solve()
{
    cin >> n;
    lim = 100;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i < n; i++) {
        int x, y; cin >> x >> y;
        adj[x].push_back(y);
    }
    dfs(1);

    int ans = 0;
    for(int x = 1; x <= lim; x++) for(int y = x; y <= lim; y++) if(dp[1][x][y]) ans++;
    cout << ans;
}
main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

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

    solve();

    cerr << "\nTime" << 0.001 * clock() << "s "; return 0;


}

Compilation message (stderr)

uzastopni.cpp:51:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   51 | main()
      | ^~~~
uzastopni.cpp: In function 'int main()':
uzastopni.cpp:57:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
uzastopni.cpp:58:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |         freopen(TASK".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...