#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#define watch(x) cout<<(#x)<<"="<<(x)<<'\n'
#define mset(d,val) memset(d,val,sizeof(d))
#define cbug if(DEBUG) cout
#define setp(x) cout<<fixed<<setprecision(x)
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define forn(i,a,b) for(int i=(a);i<(b);i++)
#define fore(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
#define F first
#define S second
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
//template<typename T>
//using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void SD(int t=0){ cout<<"PASSED "<<t<<endl; }
ostream& operator<<(ostream &out, ii x){ out<<"("<<x.F<<","<<x.S<<")"; return out; }
template<typename T> void amax(T &a, T b){ a=max(a,b); }
template<typename T> void amin(T &a, T b){ a=min(a,b); }
struct Hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
const ll INF = ll(1e18);
const int MOD = 998244353;
const bool DEBUG = 0;
const int MAXN = 100005;
const int LG = 21;
vector<vector<int>> adj;
vector<ll> dist;
vector<ll> dp;
vector<bool> vst;
void dfs(int u, int p, int r)
{
vst[u] = 1;
for (int v : adj[u])
{
if (v == p || v > r) continue;
dist[v]=dist[u]+1;
dfs(v, u, r);
}
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n; cin>>n;
adj.resize(n);
dp.resize(n, 0);
vst.resize(n, 0);
dist.resize(n);
int p[n];
forn(i,0,n)
{
cin>>p[i];
p[i]--;
}
forn(i,0,n-1)
{
int u,v; cin>>u>>v; u--; v--;
u=p[u];
v=p[v];
adj[u].pb(v);
adj[v].pb(u);
}
forn(i,1,n)
{
forn(j,0,n) vst[j] = 0;
dist[i]=0;
dfs(i, -1, i);
forn(j,0,i)
{
if (vst[j]) dp[i] = max(dp[i], dp[j] + dist[j]);
}
}
cout<<dp[n-1]<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
324 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 |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
324 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 |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
159 ms |
1044 KB |
Output is correct |
19 |
Correct |
149 ms |
1096 KB |
Output is correct |
20 |
Correct |
148 ms |
1112 KB |
Output is correct |
21 |
Correct |
49 ms |
1004 KB |
Output is correct |
22 |
Correct |
47 ms |
856 KB |
Output is correct |
23 |
Correct |
49 ms |
884 KB |
Output is correct |
24 |
Correct |
49 ms |
864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
324 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 |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
159 ms |
1044 KB |
Output is correct |
19 |
Correct |
149 ms |
1096 KB |
Output is correct |
20 |
Correct |
148 ms |
1112 KB |
Output is correct |
21 |
Correct |
49 ms |
1004 KB |
Output is correct |
22 |
Correct |
47 ms |
856 KB |
Output is correct |
23 |
Correct |
49 ms |
884 KB |
Output is correct |
24 |
Correct |
49 ms |
864 KB |
Output is correct |
25 |
Correct |
0 ms |
316 KB |
Output is correct |
26 |
Correct |
138 ms |
892 KB |
Output is correct |
27 |
Correct |
131 ms |
1124 KB |
Output is correct |
28 |
Correct |
144 ms |
1044 KB |
Output is correct |
29 |
Correct |
125 ms |
932 KB |
Output is correct |
30 |
Correct |
52 ms |
756 KB |
Output is correct |
31 |
Correct |
60 ms |
756 KB |
Output is correct |
32 |
Correct |
53 ms |
708 KB |
Output is correct |
33 |
Correct |
55 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
324 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 |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
159 ms |
1044 KB |
Output is correct |
19 |
Correct |
149 ms |
1096 KB |
Output is correct |
20 |
Correct |
148 ms |
1112 KB |
Output is correct |
21 |
Correct |
49 ms |
1004 KB |
Output is correct |
22 |
Correct |
47 ms |
856 KB |
Output is correct |
23 |
Correct |
49 ms |
884 KB |
Output is correct |
24 |
Correct |
49 ms |
864 KB |
Output is correct |
25 |
Execution timed out |
2033 ms |
18752 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Execution timed out |
2068 ms |
18072 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
324 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 |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
159 ms |
1044 KB |
Output is correct |
19 |
Correct |
149 ms |
1096 KB |
Output is correct |
20 |
Correct |
148 ms |
1112 KB |
Output is correct |
21 |
Correct |
49 ms |
1004 KB |
Output is correct |
22 |
Correct |
47 ms |
856 KB |
Output is correct |
23 |
Correct |
49 ms |
884 KB |
Output is correct |
24 |
Correct |
49 ms |
864 KB |
Output is correct |
25 |
Correct |
0 ms |
316 KB |
Output is correct |
26 |
Correct |
138 ms |
892 KB |
Output is correct |
27 |
Correct |
131 ms |
1124 KB |
Output is correct |
28 |
Correct |
144 ms |
1044 KB |
Output is correct |
29 |
Correct |
125 ms |
932 KB |
Output is correct |
30 |
Correct |
52 ms |
756 KB |
Output is correct |
31 |
Correct |
60 ms |
756 KB |
Output is correct |
32 |
Correct |
53 ms |
708 KB |
Output is correct |
33 |
Correct |
55 ms |
724 KB |
Output is correct |
34 |
Execution timed out |
2033 ms |
18752 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |