Submission #769136

# Submission time Handle Problem Language Result Execution time Memory
769136 2023-06-29T08:35:27 Z danikoynov Cat Exercise (JOI23_ho_t4) C++14
31 / 100
94 ms 1040 KB
#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int maxn = 5010;

int n, a[maxn], used[maxn], dp[maxn], pos[maxn];
vector < int > adj[maxn];

int rec(int val)
{
    if (used[val])
        return dp[val];
    used[val] = 1;
    int left = 0, right = n + 1;
    for (int i = n; i > val; i --)
    {
        if (pos[i] > pos[val])
        {
            right = min(right, pos[i]);
        }
        else
        {
            left = max(left, pos[i]);
        }
    }

    int mx = -1;
    for (int i = pos[val] + 1; i < right; i ++)
    {
        if (a[i] > mx)
        {
            mx = a[i];
            dp[val] = max(dp[val], i - pos[val] + rec(a[i]));
        }
    }
    mx = -1;
    for (int i = pos[val] - 1; i > left; i --)
    {
        if (a[i] > mx)
        {
            mx = a[i];
            dp[val] = max(dp[val], pos[val] - i + rec(a[i]));
        }
    }
    return dp[val];
}

int dfs(int v, int p, int mx, int dis, int val)
{
    if (mx > val)
        return 0;

    int ans = 0;
    if (a[v] == mx)
        ans = max(ans, dis + dp[v]);

    for (int u : adj[v])
    {
        if (u == p)
            continue;
            ans = max(ans, dfs(u, v, max(mx, a[u]), dis + 1, val));

    }

    return ans;
}
void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i ++)
    {
        cin >> a[i];
        pos[a[i]] = i;
    }


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

    for (int i = 1; i <= n; i ++)
    {
        int v = pos[i];
        for (int u : adj[v])
        {
            dp[v] = max(dp[v], dfs(u, v, a[u], 1, a[v]));
        }
        ///cout << v << " : " << dp[v] << endl;
    }


    cout << dp[pos[n]] << endl;

}

int main()
{
    solve();
    return 0;
}

Compilation message

Main.cpp: In function 'int dfs(int, int, int, int, int)':
Main.cpp:69:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   69 |         if (u == p)
      |         ^~
Main.cpp:71:13: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   71 |             ans = max(ans, dfs(u, v, max(mx, a[u]), dis + 1, val));
      |             ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 436 KB Output is correct
18 Correct 90 ms 956 KB Output is correct
19 Correct 82 ms 1040 KB Output is correct
20 Correct 81 ms 960 KB Output is correct
21 Correct 4 ms 980 KB Output is correct
22 Correct 4 ms 900 KB Output is correct
23 Correct 4 ms 852 KB Output is correct
24 Correct 4 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 436 KB Output is correct
18 Correct 90 ms 956 KB Output is correct
19 Correct 82 ms 1040 KB Output is correct
20 Correct 81 ms 960 KB Output is correct
21 Correct 4 ms 980 KB Output is correct
22 Correct 4 ms 900 KB Output is correct
23 Correct 4 ms 852 KB Output is correct
24 Correct 4 ms 852 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 87 ms 844 KB Output is correct
27 Correct 94 ms 916 KB Output is correct
28 Correct 86 ms 880 KB Output is correct
29 Correct 87 ms 888 KB Output is correct
30 Correct 9 ms 596 KB Output is correct
31 Correct 14 ms 712 KB Output is correct
32 Correct 10 ms 708 KB Output is correct
33 Correct 12 ms 680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 436 KB Output is correct
18 Correct 90 ms 956 KB Output is correct
19 Correct 82 ms 1040 KB Output is correct
20 Correct 81 ms 960 KB Output is correct
21 Correct 4 ms 980 KB Output is correct
22 Correct 4 ms 900 KB Output is correct
23 Correct 4 ms 852 KB Output is correct
24 Correct 4 ms 852 KB Output is correct
25 Runtime error 1 ms 596 KB Execution killed with signal 11
26 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Runtime error 1 ms 692 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 436 KB Output is correct
18 Correct 90 ms 956 KB Output is correct
19 Correct 82 ms 1040 KB Output is correct
20 Correct 81 ms 960 KB Output is correct
21 Correct 4 ms 980 KB Output is correct
22 Correct 4 ms 900 KB Output is correct
23 Correct 4 ms 852 KB Output is correct
24 Correct 4 ms 852 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 87 ms 844 KB Output is correct
27 Correct 94 ms 916 KB Output is correct
28 Correct 86 ms 880 KB Output is correct
29 Correct 87 ms 888 KB Output is correct
30 Correct 9 ms 596 KB Output is correct
31 Correct 14 ms 712 KB Output is correct
32 Correct 10 ms 708 KB Output is correct
33 Correct 12 ms 680 KB Output is correct
34 Runtime error 1 ms 596 KB Execution killed with signal 11
35 Halted 0 ms 0 KB -