# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
895966 | niter | Cat Exercise (JOI23_ho_t4) | C++14 | 2054 ms | 29656 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define loop(i,a,b) for(int i = (a); i < (b); i ++)
#define pb push_back
#define ins insert
#define pii pair<int,int>
#define ff first
#define ss second
#define op(x) cerr << #x << " = " << x << endl;
#define opa(x) cerr << #x << " = " << x << ", ";
#define spac cerr << ' ';
#define entr cerr << endl;
#define STL(x) cerr << #x << " : "; for(auto &qwe:x) cerr << qwe << ' '; cerr << endl;
#define ARR(x, nnn) cerr << #x << " : "; loop(qwe,0,nnn) cerr << x[qwe] << ' '; cerr << endl;
#define bye exit(0);
using namespace std;
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
inline int rng(int l, int r){ return uniform_int_distribution<int>(l, r)(RNG); }
ostream& operator<<(ostream& os, pii A){ os << "[" << A.ff << ", " << A.ss << "]"; }
const int mxn = (int)(2e5) + 10;
int a[mxn];
vector<int> E[mxn];
bool took[mxn];
long long dp[mxn];
void find_max(int v, int par, int dis, int& pos_dis, int& pos, int& mx){
if(a[v] > mx){
mx = a[v];
pos = v;
pos_dis = dis;
}
for(auto &i:E[v]){
if(i == par) continue;
if(!took[i]){
find_max(i, v, dis + 1, pos_dis, pos, mx);
}
}
}
void dfs(int v){
took[v] = true;
for(auto &i:E[v]){
if(!took[i]){
int mx_pos, mx_val = -999, det;
find_max(i, v, 1, det, mx_pos, mx_val);
dfs(mx_pos);
dp[v] = max(dp[v], dp[mx_pos] + det);
}
}
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
loop(i,0,n) cin >> a[i];
int st;
loop(i,0,n) if(a[i] == n) st = i;
loop(i,0,n-1){
int u, v;
cin >> u >> v;
u--; v--;
E[u].pb(v);
E[v].pb(u);
}
dfs(st);
cout << dp[st] << '\n';
}
/*
5
1 5 3 4 5
*/
컴파일 시 표준 에러 (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... |