#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define in insert
#define all(x) x.begin(),x.end()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define int long long
typedef long long ll;
typedef vector<int> vi;
typedef set<int> si;
typedef multiset<int> msi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef pair<int, int> P;
template<typename T, typename U>
ostream & operator << (ostream &out, const pair<T, U> &c) {
out << c.first << ' ' << c.second;
return out;
}
template<typename T>
ostream & operator << (ostream &out, vector<T> &v) {
const int sz = v.size();
for (int i = 0; i < sz; i++) {
if (i) out << ' ';
out << v[i];
}
return out;
}
template<typename T>
istream & operator >> (istream &in, vector<T> &v) {
for (T &x : v) in >> x;
return in;
}
template<typename T, typename U>
istream & operator >> (istream &in, pair<T, U> &c) {
in >> c.first;
in >> c.second;
return in;
}
template<typename T>
void mxx(T &a, T b){if(b > a) a = b;}
template<typename T>
void mnn(T &a, T b){if(b < a) a = b;}
const int mxn = 2e5 + 10, inf = 1e18 + 10;
int p[mxn], vis[mxn];
vector<int> g[mxn];
pair<int, int> mx;
void dfs(int to, int fr, int dp = 1) {
if(p[mx.ff] < p[to]) mx = {to, dp};
for(int x : g[to]) {
if(vis[x] || x == fr) continue;
dfs(x, to, dp + 1);
}
}
int solve(int n) {
vis[n] = 1;
int ret = 0;
for(int x : g[n]) {
if(vis[x]) continue;
mx = {0, 0};
dfs(x, n);
mxx(ret, mx.ss + solve(mx.ff));
}
return ret;
}
void go() {
int n, root = 0;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> p[i];
if(p[i] == n) root = i;
}
for(int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
g[x].pb(y);
g[y].pb(x);
}
cout << solve(root) << '\n';
}
signed main() {
fast;
int t = 1;
// cin >> t;
while(t--) {
go();
}
return 0;
}
Compilation message
Main.cpp:23:39: warning: bad option '-fwhole-program' to pragma 'optimize' [-Wpragmas]
23 | #pragma GCC optimize("-fwhole-program")
| ^
Main.cpp:30:41: warning: bad option '-fstrict-overflow' to pragma 'optimize' [-Wpragmas]
30 | #pragma GCC optimize("-fstrict-overflow")
| ^
Main.cpp:32:41: warning: bad option '-fcse-skip-blocks' to pragma 'optimize' [-Wpragmas]
32 | #pragma GCC optimize("-fcse-skip-blocks")
| ^
Main.cpp:46:51: warning: bad option '-funsafe-loop-optimizations' to pragma 'optimize' [-Wpragmas]
46 | #pragma GCC optimize("-funsafe-loop-optimizations")
| ^
Main.cpp:71:57: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
71 | ostream & operator << (ostream &out, const pair<T, U> &c) {
| ^
Main.cpp:71:57: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
Main.cpp:71:57: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
Main.cpp:71:57: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
Main.cpp:77:50: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
77 | ostream & operator << (ostream &out, vector<T> &v) {
| ^
Main.cpp:77:50: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
Main.cpp:77:50: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
Main.cpp:77:50: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
Main.cpp:87:49: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
87 | istream & operator >> (istream &in, vector<T> &v) {
| ^
Main.cpp:87:49: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
Main.cpp:87:49: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
Main.cpp:87:49: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
Main.cpp:93:51: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
93 | istream & operator >> (istream &in, pair<T, U> &c) {
| ^
Main.cpp:93:51: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
Main.cpp:93:51: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
Main.cpp:93:51: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
Main.cpp:100:19: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
100 | void mxx(T &a, T b){if(b > a) a = b;}
| ^
Main.cpp:100:19: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
Main.cpp:100:19: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
Main.cpp:100:19: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
Main.cpp:102:19: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
102 | void mnn(T &a, T b){if(b < a) a = b;}
| ^
Main.cpp:102:19: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
Main.cpp:102:19: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
Main.cpp:102:19: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
Main.cpp:109:36: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
109 | void dfs(int to, int fr, int dp = 1) {
| ^
Main.cpp:109:36: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
Main.cpp:109:36: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
Main.cpp:109:36: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
Main.cpp:117:16: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
117 | int solve(int n) {
| ^
Main.cpp:117:16: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
Main.cpp:117:16: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
Main.cpp:117:16: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
Main.cpp:129:9: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
129 | void go() {
| ^
Main.cpp:129:9: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
Main.cpp:129:9: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
Main.cpp:129:9: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
Main.cpp:145:13: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
145 | signed main() {
| ^
Main.cpp:145:13: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
Main.cpp:145:13: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
Main.cpp:145:13: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
4948 KB |
Output is correct |
18 |
Correct |
81 ms |
5604 KB |
Output is correct |
19 |
Correct |
74 ms |
5564 KB |
Output is correct |
20 |
Correct |
70 ms |
5524 KB |
Output is correct |
21 |
Correct |
4 ms |
5276 KB |
Output is correct |
22 |
Correct |
4 ms |
5332 KB |
Output is correct |
23 |
Correct |
4 ms |
5332 KB |
Output is correct |
24 |
Correct |
4 ms |
5332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
4948 KB |
Output is correct |
18 |
Correct |
81 ms |
5604 KB |
Output is correct |
19 |
Correct |
74 ms |
5564 KB |
Output is correct |
20 |
Correct |
70 ms |
5524 KB |
Output is correct |
21 |
Correct |
4 ms |
5276 KB |
Output is correct |
22 |
Correct |
4 ms |
5332 KB |
Output is correct |
23 |
Correct |
4 ms |
5332 KB |
Output is correct |
24 |
Correct |
4 ms |
5332 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
26 |
Correct |
91 ms |
5408 KB |
Output is correct |
27 |
Correct |
92 ms |
5524 KB |
Output is correct |
28 |
Correct |
108 ms |
5512 KB |
Output is correct |
29 |
Correct |
89 ms |
5520 KB |
Output is correct |
30 |
Correct |
10 ms |
5204 KB |
Output is correct |
31 |
Correct |
17 ms |
5300 KB |
Output is correct |
32 |
Correct |
12 ms |
5296 KB |
Output is correct |
33 |
Correct |
15 ms |
5296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
4948 KB |
Output is correct |
18 |
Correct |
81 ms |
5604 KB |
Output is correct |
19 |
Correct |
74 ms |
5564 KB |
Output is correct |
20 |
Correct |
70 ms |
5524 KB |
Output is correct |
21 |
Correct |
4 ms |
5276 KB |
Output is correct |
22 |
Correct |
4 ms |
5332 KB |
Output is correct |
23 |
Correct |
4 ms |
5332 KB |
Output is correct |
24 |
Correct |
4 ms |
5332 KB |
Output is correct |
25 |
Execution timed out |
2072 ms |
19100 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Execution timed out |
2068 ms |
16172 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
4948 KB |
Output is correct |
18 |
Correct |
81 ms |
5604 KB |
Output is correct |
19 |
Correct |
74 ms |
5564 KB |
Output is correct |
20 |
Correct |
70 ms |
5524 KB |
Output is correct |
21 |
Correct |
4 ms |
5276 KB |
Output is correct |
22 |
Correct |
4 ms |
5332 KB |
Output is correct |
23 |
Correct |
4 ms |
5332 KB |
Output is correct |
24 |
Correct |
4 ms |
5332 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
26 |
Correct |
91 ms |
5408 KB |
Output is correct |
27 |
Correct |
92 ms |
5524 KB |
Output is correct |
28 |
Correct |
108 ms |
5512 KB |
Output is correct |
29 |
Correct |
89 ms |
5520 KB |
Output is correct |
30 |
Correct |
10 ms |
5204 KB |
Output is correct |
31 |
Correct |
17 ms |
5300 KB |
Output is correct |
32 |
Correct |
12 ms |
5296 KB |
Output is correct |
33 |
Correct |
15 ms |
5296 KB |
Output is correct |
34 |
Execution timed out |
2072 ms |
19100 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |