# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
932483 | ErJ | Cat Exercise (JOI23_ho_t4) | C++17 | 2086 ms | 26408 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<pair<ll, ll>>
#define pp pair<ll, ll>
#define qi queue<ll>
#define qp queue<pp>
#define pqi priority_queue<ll>
#define pqp priority_queue<pp>
#define mi map<ll, ll>
#define mpi map<pp, ll>
#define mip map<ll, pp>
#define mpp map<pp, pp>
#define mb map<ll, bool>
#define si set<ll>
#define sp set<pp>
#define mod 1000000007
#define rep(a, b) for(int a = 0; a < (b); a++)
#define rep2(a, b) for(int a = 1; a < (b); a++)
#define inf 100000000000000
vvi edges;
vi height;
vb kamen;
pp findMAX(ll v) {//distance, vertex
vp val;
qp q;
q.push({ v, 0 });
vb was(edges.size());
rep(i, was.size()) was[i] = false;
while (!q.empty()) {
ll u = q.front().first;
ll du = q.front().second;
val.push_back({ u, du });
q.pop();
was[u] = true;
rep(i, edges[u].size()) {
ll w = edges[u][i];
if (!(was[w] || kamen[w])) {
was[w] = true;
q.push({ w, du + 1 });
}
}
}
ll MAX = -1, indx = -1;
rep(i, val.size()) {
if (height[val[i].first] > MAX) {
MAX = height[val[i].first];
indx = i;
}
}
return { val[indx].second + 1, val[indx].first};
}
ll solve(ll ans, ll cat) {
kamen[cat] = true;
ll ANS = ans;
rep(i, edges[cat].size()) {
ll v = edges[cat][i];
if (kamen[v]) continue;
pp next = findMAX(v);
ANS = max(ANS, solve(ans + next.first, next.second));
}
return ANS;
}
int main()
{
int n;
cin >> n;
height.resize(n);
kamen.resize(n);
ll cat = 0;
rep(i, n) {
cin >> height[i];
kamen[i] = false;
if (height[i] == n) {
cat = i;
}
}
edges.resize(n);
rep(i, n - 1) {
int u, v;
cin >> u >> v;
u--; v--;
edges[u].push_back(v);
edges[v].push_back(u);
}
cout << solve(0, cat) << endl;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |