QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#372870#7513. Palindromic BeadswendyasifTL 0ms0kbC++144.9kb2024-03-31 20:09:592024-03-31 20:10:00

Judging History

你现在查看的是最新测评结果

  • [2024-03-31 20:10:00]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-03-31 20:09:59]
  • 提交

answer

#include <bits/stdc++.h>
#define debug cerr << "[" << __LINE__ << "] "
#define SZ(x) (int) x.size() - 1
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> inline void chkmax(T& x, T y) { x = max(x, y); }
template <typename T> inline void chkmin(T& x, T y) { x = min(x, y); }
template <typename T> inline void read(T &x) {
    x = 0; int f = 1; char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    x *= f;
}
const int N = 2e5 + 10, M = N / 2 * 20 * 20;
int n, c[N], dfn[N], out[N], dfscnt, dep[N], f[N], g[N], ans;
vector <int> vv[N], v[N];
namespace Seg2 {
#define li ls[num], l, mid
#define ri rs[num], mid + 1, r
    int tot, rt[N << 2], seg[M], ls[M], rs[M];
    void ins(int &num, int l, int r, int x, int y) {
        if (!num) num = ++tot;
        chkmax(seg[num], y);
        if (l == r) return;
        int mid = (l + r) >> 1;
        if (mid >= x) ins(li, x, y);
        else ins(ri, x, y);
    }
    int qry(int num, int l, int r, int L, int R) {
        if (L <= l && r <= R) return seg[num];
        int mid = (l + r) >> 1;
        if (mid >= R) return qry(li, L, R);
        if (mid < L) return qry(ri, L, R);
        return max(qry(li, L, R), qry(ri, L, R));
    }
#undef li
#undef ri
}
namespace Seg1 {
#define ls num << 1
#define rs ls | 1
#define li ls, l, mid
#define ri rs, mid + 1, r
    void ins(int num, int l, int r, int x, int y, int z) {
        Seg2::ins(Seg2::rt[num], 1, n, y, z);
        if (l == r) return;
        int mid = (l + r) >> 1;
        if (mid >= x) ins(li, x, y, z);
        else ins(ri, x, y, z);
    }
    int qry(int num, int l, int r, int L1, int R1, int L2, int R2) {
        if (L1 <= l && r <= R1) return Seg2::qry(Seg2::rt[num], 1, n, L2, R2);
        int mid = (l + r) >> 1;
        if (mid >= R1) return qry(li, L1, R1, L2, R2);
        if (mid < L1) return qry(ri, L1, R1, L2, R2);
        return max(qry(li, L1, R1, L2, R2), qry(ri, L1, R1, L2, R2));
    }
    int qry(int L1, int R1, int L2, int R2) {
        if (L1 > R1 || L2 > R2) return 0;
        return qry(1, 1, n, L1, R1, L2, R2);
    }
#undef ls
#undef rs
#undef li
#undef ri
}
void dfs(int x, int fa) {
    for (auto i = v[x].begin(); i != v[x].end(); ++i)
        if (*i == fa) {
            v[x].erase(i);
            break;
        }
    dfn[x] = ++dfscnt, dep[x] = dep[fa] + 1;
    for (int i: v[x])
//        if (i != fa)
        dfs(i, x);
//        if (i != fa) dfs(i, x);
    out[x] = dfscnt;
}
int query(int x, int y) {
    return *prev(upper_bound(all(v[x]), y, [&](int x, int y) {
        return dfn[x] < dfn[y];
    }));
}
void work(int x) {
    chkmax(ans, 1 + Seg1::qry(1, dfn[x], dfn[x], out[x]));
    // chkmax(ans, 1 + Seg1::qry(dfn[x], rdfn[x], rdfn[x] + 1, n));
    // for (int j: v[x]) chkmax(ans, 1 + Seg1::qry(dfn[j], rdfn[j], rdfn[j] + 1, rdfn[x]));
    for (int j: v[x]) chkmax(ans, 1 + Seg1::qry(dfn[j], out[j], out[j] + 1, n));
}
signed main() {
    freopen("../text.in","r",stdin);
    // cout << sizeof(seg) * 3 / 1024 / 1024 << endl;
    read(n);
    F(i, 1, n) read(c[i]), vv[c[i]].push_back(i);
    F(i, 1, n - 1) {
        int x, y; read(x), read(y);
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(1, 0);
    F(i, 1, n) {
        f[i] = i;
        sort(all(vv[i]), [&] (int x, int y) {
            return dfn[x] < dfn[y];
        });
        for (int j: vv[i]) chkmax(g[i], dep[j]);
    }
    sort(f + 1, f + n + 1, [&] (int x, int y) {
        return g[x] > g[y];
    });
    // sort(all(v[i]), [&]);
    F(i, 1, n) {
        // puts("!");
//        assert(g[f[i]]);
        if (!g[f[i]] || vv[f[i]].size() < 2) continue;
        // for (auto [i, j]: )
        int x = vv[f[i]][0], y = vv[f[i]][1], aa = 0;
//        work(x), work(y);
        if (dfn[x] <= dfn[y] && out[y] <= out[x]) {
            // y is x's child
            int g = query(x, y);
            chkmax(aa, Seg1::qry(1, dfn[g] - 1, dfn[y], out[y]));
            chkmax(aa, Seg1::qry(dfn[y], out[y], out[g] + 1, n));
            // q[1].emplace_back(make_pair(dfn[y], rdfn[y]), w);
            // q[dfn[g]].emplace_back(make_pair(dfn[y], rdfn[y]), -w);
            // q[rdfn[g] + 1].emplace_back(make_pair(dfn[y], rdfn[y]), w);
        }
        else chkmax(aa, Seg1::qry(dfn[x], out[x], dfn[y], out[y]));
        aa += 2;
        // cout << "! " << x << " " << y << " " << dfn[x] << " " << dfn[y] << endl;
        Seg1::ins(1, 1, n, dfn[x], dfn[y], aa);
        chkmax(ans, aa);
    }
    // cout << ans << endl;
    F(x, 1, n)
        if (vv[c[x]].size() == 1)
            work(x);
    cout << ans;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

4
1 1 2 2
1 2
2 3
2 4

output:


result: