QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#584#194356#7513. Palindromic Beadsucup-team2227ucup-team1191Success!2024-03-27 16:09:052024-03-27 16:34:41

Details

Extra Test:

Wrong Answer
time: 3ms
memory: 13840kb

input:

9
1 4 5 6 7 2 1 3 2
1 2
2 3
3 4
4 5
5 6
1 7
7 8
8 9

output:

3

result:

wrong answer 1st lines differ - expected: '4', found: '3'

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#194356#7513. Palindromic Beadsucup-team1191#WA 273ms73048kbC++204.8kb2023-09-30 20:19:252024-10-14 18:02:28

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define TIME (clock() * 1.0 / CLOCKS_PER_SEC)

const int M = 2e5 + 239;
const int T = (1 << 19) + 239;
const int L = 18;
const int BIG = 1e9 + 239;

int mx[T];

void build(int i, int l, int r) {
    mx[i] = -BIG;
    if (r - l == 1) {
        return;
    }
    int mid = (l + r) / 2;
    build(2 * i + 1, l, mid);
    build(2 * i + 2, mid, r);
}

void upd(int i, int l, int r, int p, int x) {
    if (r - l == 1) {
        mx[i] = x;
        return;
    }
    int mid = (l + r) / 2;
    if (p < mid) {
        upd(2 * i + 1, l, mid, p, x);
    } else {
        upd(2 * i + 2, mid, r, p, x);
    }
    mx[i] = max(mx[2 * i + 1], mx[2 * i + 2]);
}

int getmax(int i, int l, int r, int ql, int qr) {
    if (r <= ql || qr <= l) {
        return -BIG;
    }
    if (ql <= l && r <= qr) {
        return mx[i];
    }
    int mid = (l + r) / 2;
    return max(getmax(2 * i + 1, l, mid, ql, qr), getmax(2 * i + 2, mid, r, ql, qr));
}

int n, c[M];
vector<int> v[M];

int dp[M][L];
int h[M];
int sz[M];

void dfs_pre(int p, int ls) {
    if (ls == -1) {
        h[p] = 0;
        for (int i = 0; i < L; i++) {
            dp[p][i] = p;
        }
    } else {
        h[p] = h[ls] + 1;
        dp[p][0] = ls;
        for (int i = 1; i < L; i++) {
            dp[p][i] = dp[dp[p][i - 1]][i - 1];
        }
    }
    sz[p] = 1;
    for (int i : v[p]) {
        if (i != ls) {
            dfs_pre(i, p);
            sz[p] += sz[i];
        }
    }
}

int tin[M], tout[M], timer, et[M], go[M];
vector<int> to[M];

void dfs_hld(int p, int ls) {
    et[timer] = p;
    tin[p] = timer++;

    int best = -1;
    for (int i : v[p]) {
        if (i != ls) {
            if (best == -1 || sz[best] < sz[i]) {
                best = i;
            }
        }
    }

    if (best != -1) {
        to[p].emplace_back(best);
        go[best] = go[p];
        dfs_hld(best, p);
        for (int i : v[p]) {
            if (i != ls && i != best) {
                go[i] = i;
                dfs_hld(i, p);
                to[p].emplace_back(i);
            }
        }
    }

    tout[p] = timer;
}

bool upper(int s, int f) {
    return tin[s] <= tin[f] && tout[f] <= tout[s];
}

int lca(int s, int f) {
    if (upper(s, f)) {
        return s;
    }
    for (int i = L - 1; i >= 0; i--) {
        if (!upper(dp[s][i], f)) {
            s = dp[s][i];
        }
    }
    return dp[s][0];
}

vector<int> in[M];
bool is_main[M];
int lc[M];

int ans[M];

int getmax(int s, int f) {
    int res = -BIG;
    while (true) {
        bool stop = false;
        int x = go[f];
        if (upper(x, s)) {
            x = s;
            stop = true;
        }
        res = max(res, getmax(0, 0, n, tin[x], tin[f] + 1));
        if (stop) {
            return res;
        }
        f = dp[x][0];
    }
}

void dfs_ans(int p) {
    if (is_main[p]) {
        ans[p] = 2;
        if (in[c[p]][1] != dp[p][0]) {
            ans[p] = 3;
        }
        int u = max(getmax(lc[c[p]], p), getmax(lc[c[p]], in[c[p]][1]));
        if (u >= 0) {
            ans[p] = max(ans[p], u + 2);
        }
        upd(0, 0, n, tin[in[c[p]][1]], ans[p]);
    }

    for (int i : to[p]) {
        dfs_ans(i);
    }

    if (is_main[p]) {
        upd(0, 0, n, tin[in[c[p]][1]], -BIG);
    }
}

void solve() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> c[i];
        c[i]--;
        in[c[i]].emplace_back(i);
    }
    for (int i = 0; i < n - 1; i++) {
        int s, f;
        cin >> s >> f;
        s--, f--;
        v[s].emplace_back(f);
        v[f].emplace_back(s);
    }
    dfs_pre(0, -1);
    dfs_hld(0, -1);
    for (int x = 0; x < n; x++) {
        if (in[x].size() == 2) {
            int s = in[x][0];
            int f = in[x][1];
            lc[x] = lca(s, f);
            if (upper(s, f)) {
                in[x][0] = f;
                in[x][1] = s;
                is_main[f] = true;
                continue;
            }
            if (upper(f, s)) {
                is_main[s] = true;
                continue;
            }
            if (tin[s] < tin[f]) {
                is_main[s] = true;
            } else {
                in[x][0] = f;
                in[x][1] = s;
                is_main[f] = true;
            }
        }
    }
    build(0, 0, n);
    dfs_ans(0);
    int cur = 1;
    for (int i = 0; i < n; i++) {
        cur = max(cur, ans[i]);
    }
    cout << cur << "\n";
}

int main() {
#ifdef ONPC
    freopen("input", "r", stdin);
#endif
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}