QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#422217#7513. Palindromic BeadspandapythonerWA 322ms114672kbC++175.6kb2024-05-27 04:55:532024-05-27 04:55:54

Judging History

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

  • [2024-05-27 04:55:54]
  • 评测
  • 测评结果:WA
  • 用时:322ms
  • 内存:114672kb
  • [2024-05-27 04:55:53]
  • 提交

answer

#include <bits/stdc++.h>


using namespace std;


#define ll long long
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()


const ll inf = 1e18;
mt19937 rnd(234);


struct SGT_mx {
    int n;
    vector<int> t;


    void build(int _n) {
        n = 1;
        while (n < _n) {
            n *= 2;
        }
        t.assign(n * 2, 0);
    }


    void change(int i, int x) {
        i += n;
        for (; i >= 1; i /= 2) {
            t[i] = max(t[i], x);
        }
    }


    int get(int l, int r) {
        l += n;
        r += n + 1;
        int rs = 0;
        while (l < r) {
            if (l & 1) {
                rs = max(rs, t[l]);
                l += 1;
            }
            if (r & 1) {
                r -= 1;
                rs = max(rs, t[r]);
            }
            l /= 2;
            r /= 2;
        }
        return rs;
    }
};


struct SGT_2d {
    int n;
    vector<vector<int>> p;
    vector<SGT_mx> t;


    void build(vector<vector<int>> a) {
        n = 1;
        while (n < (int)a.size()) {
            n *= 2;
        }
        p.assign(n * 2, vector<int>());
        t.resize(n * 2);
        for (int i = 0; i < (int)a.size(); i += 1) {
            p[i + n] = a[i];
            sort(all(p[i + n]));
            p[i + n].resize(unique(all(p[i + n])) - p[i + n].begin());
        }
        for (int i = n - 1; i >= 1; i -= 1) {
            p[i].resize(p[i + i].size() + p[i + i + 1].size());
            merge(all(p[i + i]), all(p[i + i + 1]), p[i].begin());
            p[i].resize(unique(all(p[i])) - p[i].begin());
        }
        for (int i = 1; i < n * 2; i += 1) {
            t[i].build((int)p[i].size());
        }
    }


    void change(int x, int y, int val) {
        x += n;
        for (; x >= 1; x /= 2) {
            int i = lower_bound(all(p[x]), y) - p[x].begin();
            t[x].change(i, val);
        }
    }


    int get(int lx, int rx, int ly, int ry) {
        lx += n;
        rx += n + 1;
        int rs = 0;
        while (lx < rx) {
            if (lx & 1) {
                int l = lower_bound(all(p[lx]), ly) - p[lx].begin();
                int r = upper_bound(all(p[lx]), ry) - p[lx].begin() - 1;
                rs = max(rs, t[lx].get(l, r));
                lx += 1;
            }
            if (rx & 1) {
                rx -= 1;
                int l = lower_bound(all(p[rx]), ly) - p[rx].begin();
                int r = upper_bound(all(p[rx]), ry) - p[rx].begin() - 1;
                rs = max(rs, t[rx].get(l, r));
            }
            lx /= 2;
            rx /= 2;
        }
        return rs;
    }
};

int n;
vector<int> c;
vector<vector<int>> g;
vector<vector<int>> f;
vector<int> tin, tout;
vector<int> e;
vector<int> par;


void dfs(int v, int p) {
    par[v] = p;
    tin[v] = (int)e.size();
    e.push_back(v);
    for (auto to : g[v]) {
        if (to == p) {
            continue;
        }
        dfs(to, v);
    }
    tout[v] = (int)e.size();
}


bool is_son(int v, int p) {
    return tin[p] <= tin[v] and tin[v] < tout[p];
}

int solve() {
    tin.resize(n);
    tout.resize(n);
    e.clear();
    e.reserve(n);
    par.resize(n);
    dfs(0, -1);
    f.assign(n, vector<int>());
    for (auto v : e) {
        f[c[v]].push_back(v);
    }
    int rs = 1;
    sort(all(f), [&](const vector<int>& a, const vector<int>& b) {
        if ((int)a.size() != (int)b.size()) {
            return (int)a.size() < (int)b.size();
        }
        if ((int)a.size() != 2) {
            return false;
        }
        int u = a[0], v = a[1];
        int s = b[0], t = b[1];
        bool para = is_son(u, v) || is_son(v, u);
        bool parb = is_son(s, t) || is_son(t, s);
        if (para != parb) {
            return para < parb;
        }
        if (para) {
            return abs(tin[u] - tin[v]) > abs(tin[s] - tin[t]);
        }
        return tin[u] < tin[s];
        });
    vector<array<int, 3>> aboba;
    SGT_2d sgt;
    vector<vector<int>> sgt_init(n);
    for (auto x : f) {
        if ((int)x.size() != 2) {
            continue;
        }
        int u = x[0], v = x[1];
        sgt_init[tin[u]].push_back(tin[v]);
    }
    sgt.build(sgt_init);
    auto add_point = [&](int x, int y, int val) {
        sgt.change(x, y, val);
        };
    auto get = [&](int lx, int rx, int ly, int ry) {
        return sgt.get(lx, rx, ly, ry);
        };
    for (auto x : f) {
        if ((int)x.size() != 2) {
            continue;
        }
        int u = x[0], v = x[1];
        int mx = 2;
        if (is_son(v, u)) {
            mx = max(mx, get(0, tin[u] - 1, tin[v], tout[v] - 1) + 2);
            mx = max(mx, get(tin[v], tout[v] - 1, tout[u], n - 1) + 2);
        } else {
            mx = max(mx, get(tin[u], tout[u] - 1, tin[v], tout[v] - 1) + 2);
        }
        if (par[v] == u) {
            rs = max(rs, mx);
        } else {
            rs = max(rs, mx + 1);
        }
        add_point(tin[u], tin[v], mx);
    }
    return rs;
}


int32_t main() {
    if (1) {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    }
    cin >> n;
    c.resize(n);
    for (int i = 0; i < n; i += 1) {
        cin >> c[i];
        --c[i];
    }
    g.assign(n, vector<int>());
    for (int i = 0; i < n - 1; i += 1) {
        int u, v;
        cin >> u >> v;
        --u;
        --v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    int rs = solve();
    cout << rs << "\n";
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3888kb

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: 3488kb

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: 0
Accepted
time: 0ms
memory: 3820kb

input:

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

output:

2

result:

ok single line: '2'

Test #4:

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

input:

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

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 2ms
memory: 4548kb

input:

2000
845 1171 345 282 1181 625 754 289 681 493 423 840 1494 318 266 1267 967 379 135 14 39 191 60 972 116 1216 1205 19 194 185 1360 861 379 430 1262 1151 756 65 389 488 277 53 1283 1438 101 1465 195 714 737 980 80 298 961 1326 163 1163 1317 1152 992 35 334 802 1502 486 710 234 555 88 1278 146 46 696...

output:

5

result:

ok single line: '5'

Test #6:

score: 0
Accepted
time: 317ms
memory: 114052kb

input:

200000
48015 47923 20609 71806 43752 68214 95683 89449 25809 58110 19878 52931 7845 45206 86245 82945 62977 37876 12456 105915 10509 92943 66950 88545 26442 26545 42278 66977 3970 9631 21524 43638 7979 58240 25719 56260 276 89721 9553 16550 52161 30307 82748 108443 36676 48581 59069 57412 62453 7965...

output:

5

result:

ok single line: '5'

Test #7:

score: 0
Accepted
time: 319ms
memory: 114072kb

input:

200000
13011 51198 65374 107045 66506 14385 35784 94265 71449 41817 24646 60714 53382 68358 9354 840 3139 71282 72215 69550 2121 41498 13675 76444 67690 40513 56439 12832 51976 35333 47208 59602 98993 9383 77866 10464 41517 89125 58804 91741 66160 74208 70991 63865 84870 14282 2441 78046 73372 36311...

output:

7

result:

ok single line: '7'

Test #8:

score: -100
Wrong Answer
time: 322ms
memory: 114672kb

input:

200000
38715 33241 65919 39407 27500 36200 2259 42301 79147 57505 20 81399 69499 23658 14534 86934 14352 69558 59763 43318 35360 3281 38188 40058 40571 103709 75625 8434 53802 87159 98628 69421 53711 47986 18350 6079 37362 39377 71936 89573 25983 66882 48999 58918 66432 17453 82515 9588 95375 87287 ...

output:

43

result:

wrong answer 1st lines differ - expected: '45', found: '43'