제출 #741580

#제출 시각아이디문제언어결과실행 시간메모리
741580maomao90Cat Exercise (JOI23_ho_t4)C++17
100 / 100
447 ms48900 KiB

// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h> 
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 200005;
const int MAXL = 20;

int n;
int a[MAXN], inv[MAXN];
vi adj[MAXN];
ll dp[MAXN];

int p[MAXL][MAXN], lvl[MAXN];
void dfs(int u) {
    REP (k, 1, MAXL) {
        if (p[k - 1][u] == -1) {
            p[k][u] = -1;
        } else {
            p[k][u] = p[k - 1][p[k - 1][u]];
        }
    }
    for (int v : adj[u]) {
        if (v == p[0][u]) {
            continue;
        }
        lvl[v] = lvl[u] + 1;
        p[0][v] = u;
        dfs(v);
    }
}
int getd(int a, int b) {
    if (lvl[a] < lvl[b]) {
        swap(a, b);
    }
    int res = 0;
    RREP (k, MAXL - 1, 0) {
        if (p[k][a] != -1 && lvl[p[k][a]] >= lvl[b]) {
            a = p[k][a];
            res += 1 << k;
        }
    }
    if (a == b) {
        return res;
    }
    RREP (k, MAXL - 1, 0) {
        if (p[k][a] != p[k][b]) {
            res += 1 << k + 1;
            a = p[k][a];
            b = p[k][b];
        }
    }
    return res + 2;
}

int up[MAXN], rnk[MAXN], mx[MAXN];
void init() {
    REP (i, 1, n + 1) {
        up[i] = i;
        rnk[i] = 1;
        mx[i] = a[i];
    }
}
int findp(int i) {
    if (up[i] == i) return i;
    return up[i] = findp(up[i]);
}
void join(int a, int b) {
    int pa = findp(a), pb = findp(b);
    if (pa == pb) {
        return;
    }
    if (rnk[pa] < rnk[pb]) {
        swap(pa, pb);
    }
    if (rnk[pa] == rnk[pb]) {
        rnk[pa]++;
    }
    up[pb] = pa;
    mxto(mx[pa], mx[pb]);
}

int main() {
#ifndef DEBUG
    ios::sync_with_stdio(0), cin.tie(0);
#endif
    cin >> n;
    REP (i, 1, n + 1) {
        cin >> a[i];
        inv[a[i]] = i;
    }
    REP (i, 1, n) {
        int u, v; cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    p[0][1] = -1;
    dfs(1);
    init();
    REP (i, 1, n + 1) {
        int u = inv[i];
        cerr << u << '\n';
        for (int v : adj[u]) {
            if (a[v] > i) {
                continue;
            }
            int j = mx[findp(v)];
            int nv = inv[j];
            cerr << ' ' << j << ' ' << nv << ' ' << getd(u, nv) << '\n';
            mxto(dp[i], dp[j] + getd(u, nv));
            join(u, v);
        }
    }
    cout << dp[n] << '\n';
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

Main.cpp: In function 'int getd(int, int)':
Main.cpp:78:27: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   78 |             res += 1 << k + 1;
      |                         ~~^~~
#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...