Submission #895966

#TimeUsernameProblemLanguageResultExecution timeMemory
895966niterCat Exercise (JOI23_ho_t4)C++14
31 / 100
2054 ms29656 KiB
#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
*/

Compilation message (stderr)

Main.cpp: In function 'std::ostream& operator<<(std::ostream&, std::pair<int, int>)':
Main.cpp:18:84: warning: no return statement in function returning non-void [-Wreturn-type]
   18 | ostream& operator<<(ostream& os, pii A){ os << "[" << A.ff << ", " << A.ss << "]"; }
      |                                                                                    ^
Main.cpp: In function 'int main()':
Main.cpp:67:18: warning: 'st' may be used uninitialized in this function [-Wmaybe-uninitialized]
   67 |     cout << dp[st] << '\n';
      |                  ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...