QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#576444#7513. Palindromic BeadsMax_s_xaMWA 0ms17988kbC++175.3kb2024-09-19 20:22:072024-09-19 20:22:07

Judging History

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

  • [2024-09-19 20:22:07]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:17988kb
  • [2024-09-19 20:22:07]
  • 提交

answer

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <random>
#include <ctime>
#include <chrono>
#include <numeric>

typedef long long ll;
typedef double lf;

// #define DEBUG 1
struct IO
{
    #define MAXSIZE (1 << 20)
    #define isdigit(x) (x >= '0' && x <= '9')
    char buf[MAXSIZE], *p1, *p2;
    char pbuf[MAXSIZE], *pp;
    #if DEBUG
    #else
    IO() : p1(buf), p2(buf), pp(pbuf) {}
    ~IO() {fwrite(pbuf, 1, pp - pbuf, stdout);}
    #endif
    #define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) ? ' ' : *p1++)
    #define blank(x) (x == ' ' || x == '\n' || x == '\r' || x == '\t')

    template <typename T>
    void Read(T &x)
    {
        #if DEBUG
        std::cin >> x;
        #else
        bool sign = 0; char ch = gc(); x = 0;
        for (; !isdigit(ch); ch = gc())
            if (ch == '-') sign = 1;
        for (; isdigit(ch); ch = gc()) x = x * 10 + (ch ^ 48);
        if (sign) x = -x;
        #endif
    }
    void Read(char *s)
    {
        #if DEBUG
        std::cin >> s;
        #else
        char ch = gc();
        for (; blank(ch); ch = gc());
        for (; !blank(ch); ch = gc()) *s++ = ch;
        *s = 0;
        #endif
    }
    void Read(char &c) {for (c = gc(); blank(c); c = gc());}

    void Push(const char &c)
    {
        #if DEBUG
        putchar(c);
        #else
        if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
        *pp++ = c;
        #endif
    }
    template <typename T>
    void Write(T x)
    {
        if (x < 0) x = -x, Push('-');
        static T sta[35];
        int top = 0;
        do sta[top++] = x % 10, x /= 10; while (x);
        while (top) Push(sta[--top] ^ 48);
    }
    template <typename T>
    void Write(T x, char lst) {Write(x), Push(lst);}
} IO;
#define Read(x) IO.Read(x)
#define Write(x, y) IO.Write(x, y)
#define Put(x) IO.Push(x)

using namespace std;

const int MAXN = 2e5 + 10;

int n, a[MAXN], xs[MAXN], ans = 1;
vector <int> e[MAXN];

bool vis[MAXN];
int rt, tsz, sz[MAXN];
int val[MAXN];
int ff[MAXN], dfn[MAXN], clk;
void Findrt(int u, int fa)
{
    dfn[u] = 0;
    sz[u] = 1; int mx = 0;
    for (auto v : e[u])
        if (v != fa && !vis[v]) Findrt(v, u), sz[u] += sz[v], mx = max(mx, sz[v]);
    mx = max(mx, tsz - sz[u]);
    if (mx <= tsz / 2) rt = u;
}

struct SegTree
{
    int lc[MAXN * 40], rc[MAXN * 40];
    int mx[MAXN * 40];
    int rt[MAXN], tot;
    #define ls lc[p]
    #define rs lc[p]
    void Update(int &p, int q, int l, int r, int x, int y, int k)
    {
        p = ++tot, ls = lc[q], rs = rc[q], mx[p] = max(mx[q], k);
        if (x <= l && r <= y) return;
        int mid = l + r >> 1;
        if (x <= mid) Update(ls, lc[q], l, mid, x, y, k);
        if (y > mid) Update(rs, rc[q], mid + 1, r, x, y, k);
    }
    int Query(int p, int l, int r, int x, int y)
    {
        if (!p) return 0;
        if (x <= l && r <= y) return mx[p];
        int mid = l + r >> 1, ans = 0;
        if (x <= mid) ans = max(ans, Query(ls, l, mid, x, y));
        if (y > mid) ans = max(ans, Query(rs, mid + 1, r, x, y));
        return ans;
    }
}T1, T2;

void DFS2(int u, int fa)
{
    sz[u] = 1, dfn[u] = ++clk;
    for (auto v : e[u])
        if (v != fa && !vis[v]) ff[v] = (ff[u] ? ff[u] : v), DFS2(v, u), sz[u] += sz[v];
    if (xs[a[u]] == u) val[a[u]] = 1;
    else
    {
        int v = xs[a[u]] ^ u;
        if (dfn[v] && dfn[v] < dfn[u]) val[a[u]] = 2 + (fa != v);
    }
}
void DFS3(int u, int fa)
{
    T1.rt[u] = T1.rt[fa], T2.rt[u] = T2.rt[fa];
    if (xs[a[u]] != u)
    {
        int v = xs[a[u]] ^ u;
        // cout << u << ' ' << v << ' ' << a[u] << ' ' << val[a[u]] << '\n';
        if (dfn[v] < dfn[u] && dfn[u] < dfn[v] + sz[v])
        {
            // cout << "##\n";
            val[a[u]] = max(val[a[u]], T2.Query(T2.rt[fa], 1, n, dfn[v], dfn[v] + sz[v] - 1) + 2);
            T2.Update(T2.rt[u], T2.rt[u], 1, n, dfn[v], dfn[v], val[a[u]]);
            // for (int i = 1; i <= n; i++) cout << T2.Query(T2.rt[u], 1, n, i, i) << ' '; cout << "\n";
        }
        else if (ff[u] != ff[v])
        {
            val[a[u]] = max(val[a[u]], T1.Query(T1.rt[fa], 1, n, dfn[v], dfn[v]) + 2);
            val[a[u]] = max(val[a[u]], T2.Query(T2.rt[fa], 1, n, 1, n) + 2);
            T1.Update(T1.rt[u], T1.rt[u], 1, n, dfn[v], dfn[v] + sz[v] - 1, val[a[u]]);
        }
        ans = max(ans, val[a[u]]);
    }
    for (auto v : e[u])
        if (v != fa && !vis[v]) DFS3(v, u);
}

void DFS1(int u)
{
    vis[u] = 1;
    clk = 0, ff[u] = 0, DFS2(u, 0);
    T1.rt[u] = T2.rt[u] = T1.tot = T2.tot = 0;
    // for (int i = 1; i <= n; i++) cout << dfn[i] << ' '; cout << "\n";
    // for (int i = 1; i <= 3; i++) cout << val[i] << ' '; cout << '\n';
    for (auto v : e[u])
        if (!vis[v]) DFS3(v, u);
    for (auto v : e[u])
        if (!vis[v]) tsz = sz[v], Findrt(v, u), DFS1(rt);
}

int main()
{
    #if DEBUG
    #else
    ios::sync_with_stdio(0), cin.tie(0);
    #endif
    Read(n);
    for (int i = 1; i <= n; i++) Read(a[i]), xs[a[i]] ^= i;
    for (int i = 1, u, v; i < n; i++) Read(u), Read(v), e[u].push_back(v), e[v].push_back(u);
    tsz = n, Findrt(1, 0), DFS1(rt);
    cout << ans << '\n';
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 17964kb

input:

4
1 1 2 2
1 2
2 3
2 4

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 0ms
memory: 17932kb

input:

5
1 3 2 2 1
1 2
2 3
3 4
4 5

output:

4

result:

ok single line: '4'

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 17988kb

input:

6
1 1 2 2 3 3
1 2
2 3
3 4
4 5
5 6

output:

4

result:

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