답안 #894205

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
894205 2023-12-28T02:54:18 Z box Cat Exercise (JOI23_ho_t4) C++17
컴파일 오류
0 ms 0 KB
#include "bits/stdc++.h"

using namespace std;

const int N = 2e5,
    L = 18;

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

int n, p[N];
int pp[L][N], d[N];
vector < int > g[N];
long long dp[N];
int f[N];

int uf(int i) {
    return f[i] == i ? i : f[i] = uf(f[i]);
}

void dfs(int i) {
    for (int l = 1; l < L; l++) pp[l][i] = pp[l - 1][pp[l - 1][i]];
    for (int j: g[i])
        if (pp[0][i] != j) {
            pp[0][j] = i;
            d[j] = d[i] + 1;
            dfs(j);
        }
}
int lca(int i, int j) {
    if (d[i] < d[j]) swap(i, j);
    int k = d[i] - d[j];
    for (int l = 0; l < L; l++)
        if (k >> l & 1) i = pp[l][i];
    if (i == j) return i;
    for (int l = L - 1; l >= 0; l--)
        if (pp[l][i] != pp[l][j]) i = pp[l][i], j = pp[l][j];
    return pp[0][i];
}
int dis(int i, int j) {
    return d[i] + d[j] - 2 * d[lca(i, j)];
}

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> p[i], p[i]--;
    for (int h = 0; h < n - 1; h++) {
        int i, j;
        cin >> i >> j, i--, j--;
        i = p[i], j = p[j];
        cerr << "edge " << i << " " << j << endl;
        g[i].push_back(j);
        g[j].push_back(i);
    }
    pp[0][0] = 0;
    dfs(0);
    for (int i = 0; i < n; i++) f[i] = i;
    for (int i = 0; i < n; i++) {
        for (int j: g[i])
            if (j < i) {
                int c = uf(j);
                cerr << c << " " << i << "|" << dis(c, i) << endl;
                dp[i] = max(dp[i], dp[c] + dis(c, i));
                f[c] = i;
            }
        cerr << "dp[" << i << "]=" << dp[i] << endl;
    }
    cout << * max_element(dp, dp + n) << '\n';
}

Compilation message

Main.cpp:10:1: error: expected unqualified-id before 'if'
   10 | if (0) cerr
      | ^~
Main.cpp: In function 'int main()':
Main.cpp:47:12: error: 'n' was not declared in this scope
   47 |     cin >> n;
      |            ^
Main.cpp:48:40: error: 'p' was not declared in this scope
   48 |     for (int i = 0; i < n; i++) cin >> p[i], p[i]--;
      |                                        ^
Main.cpp:52:13: error: 'p' was not declared in this scope
   52 |         i = p[i], j = p[j];
      |             ^
Main.cpp:53:14: error: expected primary-expression before '<<' token
   53 |         cerr << "edge " << i << " " << j << endl;
      |              ^~
Main.cpp:64:22: error: expected primary-expression before '<<' token
   64 |                 cerr << c << " " << i << "|" << dis(c, i) << endl;
      |                      ^~
Main.cpp:68:14: error: expected primary-expression before '<<' token
   68 |         cerr << "dp[" << i << "]=" << dp[i] << endl;
      |              ^~