#include <bits/stdc++.h>
typedef long long ll;
#define FOR(i,x,y) for(ll i=x; i<y; i++)
#define FORNEG(i,x,y) for(ll i=x; i>y; i--)
using namespace std;
const ll maxn = 200005;
ll vals[maxn];
bool visited[maxn];
vector<ll> edges[maxn];
ll root = 0;
ll ans = 0;
vector<ll> dfs2(ll a, ll p){
vector<ll> ans = {a,0};
for (auto&i : edges[a]){
if (!visited[i] && i != p){
vector<ll> node = dfs2(i, a);
node[1]++;
if (vals[ans[0]] < vals[node[0]]) ans = node;
}
}
return ans;
}
void dfs(ll a, ll dist){
//cout << a << " "<<dist << endl;
ans = max(ans, dist);
for (auto&i : edges[a]){
if (!visited[i]){
vector<ll> node = dfs2(i, a);
visited[node[0]] = 1;
dfs(node[0], dist+node[1]+1);
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
FOR(i,0,n) cin >> vals[i];
FOR(i,0,n-1){
ll a,b;
cin >> a >> b;
a--;b--;
edges[a].push_back(b);
edges[b].push_back(a);
}
FOR(i,0,n){
if (vals[i] == n){
visited[i] = 1;
dfs(i,0);
}
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
3 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
3 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5208 KB |
Output is correct |
18 |
Correct |
360 ms |
6292 KB |
Output is correct |
19 |
Correct |
288 ms |
6452 KB |
Output is correct |
20 |
Correct |
280 ms |
6232 KB |
Output is correct |
21 |
Correct |
5 ms |
5980 KB |
Output is correct |
22 |
Correct |
4 ms |
5928 KB |
Output is correct |
23 |
Correct |
4 ms |
5780 KB |
Output is correct |
24 |
Correct |
4 ms |
5724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
3 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5208 KB |
Output is correct |
18 |
Correct |
360 ms |
6292 KB |
Output is correct |
19 |
Correct |
288 ms |
6452 KB |
Output is correct |
20 |
Correct |
280 ms |
6232 KB |
Output is correct |
21 |
Correct |
5 ms |
5980 KB |
Output is correct |
22 |
Correct |
4 ms |
5928 KB |
Output is correct |
23 |
Correct |
4 ms |
5780 KB |
Output is correct |
24 |
Correct |
4 ms |
5724 KB |
Output is correct |
25 |
Correct |
2 ms |
4956 KB |
Output is correct |
26 |
Correct |
232 ms |
6036 KB |
Output is correct |
27 |
Correct |
252 ms |
5976 KB |
Output is correct |
28 |
Correct |
240 ms |
5980 KB |
Output is correct |
29 |
Correct |
235 ms |
5976 KB |
Output is correct |
30 |
Correct |
12 ms |
5464 KB |
Output is correct |
31 |
Correct |
19 ms |
5476 KB |
Output is correct |
32 |
Correct |
13 ms |
5464 KB |
Output is correct |
33 |
Correct |
17 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
3 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5208 KB |
Output is correct |
18 |
Correct |
360 ms |
6292 KB |
Output is correct |
19 |
Correct |
288 ms |
6452 KB |
Output is correct |
20 |
Correct |
280 ms |
6232 KB |
Output is correct |
21 |
Correct |
5 ms |
5980 KB |
Output is correct |
22 |
Correct |
4 ms |
5928 KB |
Output is correct |
23 |
Correct |
4 ms |
5780 KB |
Output is correct |
24 |
Correct |
4 ms |
5724 KB |
Output is correct |
25 |
Execution timed out |
2076 ms |
51028 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Execution timed out |
2085 ms |
18324 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
3 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5208 KB |
Output is correct |
18 |
Correct |
360 ms |
6292 KB |
Output is correct |
19 |
Correct |
288 ms |
6452 KB |
Output is correct |
20 |
Correct |
280 ms |
6232 KB |
Output is correct |
21 |
Correct |
5 ms |
5980 KB |
Output is correct |
22 |
Correct |
4 ms |
5928 KB |
Output is correct |
23 |
Correct |
4 ms |
5780 KB |
Output is correct |
24 |
Correct |
4 ms |
5724 KB |
Output is correct |
25 |
Correct |
2 ms |
4956 KB |
Output is correct |
26 |
Correct |
232 ms |
6036 KB |
Output is correct |
27 |
Correct |
252 ms |
5976 KB |
Output is correct |
28 |
Correct |
240 ms |
5980 KB |
Output is correct |
29 |
Correct |
235 ms |
5976 KB |
Output is correct |
30 |
Correct |
12 ms |
5464 KB |
Output is correct |
31 |
Correct |
19 ms |
5476 KB |
Output is correct |
32 |
Correct |
13 ms |
5464 KB |
Output is correct |
33 |
Correct |
17 ms |
5468 KB |
Output is correct |
34 |
Execution timed out |
2076 ms |
51028 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |