QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#348905#8134. LCA Countingucup-team228#WA 2ms9884kbC++205.2kb2024-03-09 22:14:122024-03-09 22:14:12

Judging History

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

  • [2024-03-09 22:14:12]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:9884kb
  • [2024-03-09 22:14:12]
  • 提交

answer

//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx2") // doesn't work on Yandex
//#pragma GCC target("avx") // for old judges
//#pragma GCC target("bmi,bmi2,lzcnt,popcnt") // fast bit operations

#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <cmath>
#include <algorithm>
#include <cassert>
#include <chrono>
#include <random>
#include <string>
#include <numeric>
#include <complex>
#include <tuple>
#include <utility>
#include <bitset>
#include <array>
#include <stack>
#include <sstream>
#include <unordered_set>

using namespace std;
typedef long long ll;

string to_string(string a) { return '"' + a + '"'; }
string to_string(char a) { return "'" + string(1, a) + "'"; }
string to_string(const char* a) { return to_string((string) a); }
string to_string(bool a) { return a ? "true" : "false"; }
template <class T1, class T2>
string to_string(pair<T1, T2> a) {
    return "(" + to_string(a.first) + ", " + to_string(a.second) + ")";
}
template <class T>
string to_string(T a) {
    bool first = true; string res = "{";
    for (const auto& i : a) {
        if (!first) res += ", ";
        first = false;
        res += to_string(i);
    }
    res += "}";
    return res;
}
void debug_out() { cerr << endl; }
template <class T1, class... T2>
void debug_out(T1 a, T2... b) {
    cerr << " " << to_string(a);
    debug_out(b...);
}

#ifdef LOCAL
#define out(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define out(...) 42
#endif

clock_t start_time; void start_timer() { start_time = clock(); }
double get_time() { return (double) (clock() - start_time) / CLOCKS_PER_SEC; }

void Solve();

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
    freopen("usr/share/man/man1/input.txt", "r", stdin);
#endif
    start_timer();
    Solve();
#ifdef LOCAL
    cerr << fixed << setprecision(3);
    cerr << endl << "Time spent: " << get_time() << endl;
#endif
    return 0;
}

// do something, stay focused
// look for stupid bugs
// guess, slow, stress
// don't overgeneralize
// don't rush

// don't waste time on standings

// SOLVE THE PROBLEM OR DIE TRYING
// THE SOLUTION IS ALWAYS SIMPLE
// THE CODE IS ALWAYS SHORT

const int N = 2e5 + 5;
const int L = 20;

vector<int> g[N];
int tin[N];
int tout[N];
int up[N][L];
int timer = 1;

void dfs_lca(int v = 1, int p = 1) {
    tin[v] = timer++;
    up[v][0] = p;
    for (int i = 1; i < L; i++) {
        up[v][i] = up[up[v][i - 1]][i - 1];
    }
    for (int u : g[v]) {
        if (u != p) dfs_lca(u, v);
    }
    tout[v] = timer - 1;
}

bool is_anc(int u, int v) {
    return tin[u] <= tin[v] && tin[v] <= tout[u];
}

int lca(int u, int v) {
    if (is_anc(u, v)) return u;
    if (is_anc(v, u)) return v;
    for (int i = L - 1; i >= 0; i--) {
        if (!is_anc(up[u][i], v)) u = up[u][i];
    }
    return up[u][0];
}

void add_e(int u, int v) {
    g[u].push_back(v);
    g[v].push_back(u);
}

int dp[N];
vector<int> ls;
set<int> tree;
set<int> ls_set;

void dfs(int v = 1, int p = 0) {
    if (v > 1 && g[v].size() == 1) {
        g[v].clear();
        dp[v] = 1;
        return;
    }
    vector<pair<int, int>> cur;
    for (int u : g[v]) {
        if (u == p) continue;
        dfs(u, v);
        cur.push_back({dp[u], u});
    }
    sort(cur.rbegin(), cur.rend());
    g[v].clear();
    for (auto [d, u] : cur) {
        g[v].push_back(u);
    }
    dp[v] = 1;
    for (int i = 0; i <= 1; i++) {
        if (i < cur.size()) {
            dp[v] += cur[i].first;
        }
    }
}

vector<vector<int>> ts;
vector<int> t[N];

void merge(vector<int>& a, vector<int>& b) {
    if (a.size() < b.size()) {
        swap(a, b);
    }
    for (int i : b) {
        a.push_back(i);
    }
    b.clear();
}

void dfs_t(int v = 1) {
    if (g[v].empty()) {
        t[v] = {v};
        return;
    }
    for (int u : g[v]) {
        dfs_t(u);
    }
    if (g[v].size() >= 1) {
        merge(t[v], t[g[v][0]]);
    }
    if (g[v].size() >= 2) {
        merge(t[v], t[g[v][1]]);
    }
    for (int i = 2; i < g[v].size(); i += 2) {
        vector<int> cur;
        merge(cur, t[g[v][i]]);
        if (i + 1 < g[v].size()) {
            merge(cur, t[g[v][i + 1]]);
        }
        ts.push_back(cur);
    }
}

void Solve() {
    int n;
    cin >> n;
    for (int i = 2; i <= n; i++) {
        int p;
        cin >> p;
        add_e(i, p);
    }
    dfs();
    dfs_lca();
    dfs_t();
    ts.push_back(t[1]);
    sort(ts.begin(), ts.end(), [&](vector<int>& a, vector<int>& b) {
        return a.size() > b.size();
    });
    for (auto& i : ts) {
        for (auto j : i) {
            ls.push_back(j);
        }
    }
    for (int l : ls) {
        auto it = ls_set.lower_bound(l);
        if (it != ls_set.end()) {
            tree.insert(lca(l, *it));
        }
        if (it != ls_set.begin()) {
            --it;
            tree.insert(lca(l, *it));
        }
        tree.insert(l);
        ls_set.insert(l);
        cout << tree.size() << " ";
    }
    cout << "\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

7
1 1 2 4 2 2

output:

1 3 5 6 

result:

ok 4 number(s): "1 3 5 6"

Test #2:

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

input:

10
1 1 2 2 1 1 1 2 4

output:

1 3 5 6 7 8 9 

result:

ok 7 numbers

Test #3:

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

input:

10
1 2 2 4 5 6 1 2 4

output:

1 3 5 7 8 

result:

ok 5 number(s): "1 3 5 7 8"

Test #4:

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

input:

10
1 2 3 3 3 5 6 4 9

output:

1 3 4 

result:

ok 3 number(s): "1 3 4"

Test #5:

score: -100
Wrong Answer
time: 2ms
memory: 7996kb

input:

1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 4 1 2 2 1 1 1 1 2 1 1 1 1 2 2 1 1 1 1 1 6 3 1 1 1 2 2 1 1 2 1 2 1 3 1 2 1 1 2 1 1 1 1 1 1 1 4 1 5 1 1 1 1 1 2 1 1 2 1 2 1 2 5 3 1 3 1 1 2 1 2 1 1 3 2 1 6 2 1 2 5 1 1 1 3 2 1 2 1 1 1 1 1...

output:

1 3 5 7 8 10 11 12 13 15 16 17 18 20 21 23 24 25 26 27 28 29 30 31 32 33 34 35 36 38 39 40 41 42 43 44 45 46 47 49 50 52 53 55 56 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111...

result:

wrong answer 8th numbers differ - expected: '13', found: '12'