社区讨论

在线等!1道基环树好题,但浮点异常,33分,求大佬给看看

P3533[POI 2012] RAN-Rendezvous参与者 1已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mdn32zwl
此快照首次捕获于
2025/07/28 20:28
7 个月前
此快照最后确认于
2025/07/28 21:40
7 个月前
查看原帖
代码如下,报
MARKDOWN
Runtime Error.
Received signal 8: Floating-point exception.
代码如下,求大佬给看看,是哪错了,在线等
CPP
#include <bits/stdc++.h>

using namespace std;


const int maxn = 5e5 + 5;

vector<int> g[maxn];
int in[maxn], fa[maxn][20], depth[maxn], lg[maxn], rt[maxn], f[maxn], step[maxn], cnt = 0, len[maxn];

int Find(int x) {
    return f[x] == x ? x : f[x] = Find(f[x]);
}

void dfs(int u, int father, int root) {
    fa[u][0] = father;
    depth[u] = depth[father] + 1;
    rt[u] = root;
    for (int i = 1; i <= lg[depth[u]]; ++i) {
        fa[u][i] = fa[fa[u][i - 1]][i - 1];
    }
    for (int v: g[u]) {
        f[Find(v)] = Find(u);
        if (v != father && in[v] == 0) {
            dfs(v, u, root);
        }
    }
}

int lca(int x, int y) {
    if (depth[x] < depth[y]) {
        swap(x, y);
    }
    while (depth[x] > depth[y]) {
        x = fa[x][lg[depth[x] - depth[y]] - 1];
    }
    if (x == y) {
        return x;
    }
    for (int i = lg[depth[x]] - 1; i >= 0; --i) {
        if (fa[x][i] != fa[y][i]) {
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return fa[x][0];
}

void check(int u, int root) {
    if (step[u] != 0) {
        return;
    }
    step[u] = ++cnt;
    ++len[root];
    for (int v: g[u]) {
        if (in[v] != 0) {
            check(v, root);
        }
    }
}

bool check(int a, int b, int c, int d) {
    if (max(a, b) != max(c, d)) {
        return max(a, b) < max(c, d);
    }
    if (min(a, b) != min(c, d)) {
        return min(a, b) < min(c, d);
    }
    return a >= b;
}

void coder() {
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        int v;
        cin >> v;
        g[v].push_back(i);
        fa[i][0] = v;
        ++in[v];
        f[i] = i;
    }

    queue<int> q;
    for (int i = 1; i <= n; ++i) {
        if (!in[i]) {
            q.push(i);
        }
    }
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        if (--in[fa[u][0]] == 0) {
            q.push(fa[u][0]);
        }
    }
    depth[0] = -1;
    for (int i = 1; i <= n; ++i) {
        lg[i] = lg[i - 1] + (1 << lg[i - 1] == i);
    }
    for (int i = 1; i <= n; ++i) {
        if (in[i] != 0) {
            dfs(i, 0, i);
            check(i, i);
        }
    }
    while (k--) {
        int u, v;
        cin >> u >> v;
        if (Find(u) != Find(v)) {
            cout << -1 << ' ' << -1 << '\n';
        } else if (rt[u] == rt[v]) {
            int l = lca(u, v);
            cout << depth[u] - depth[l] << ' ' << depth[v] - depth[l] << '\n';
        } else {
            int t1 = rt[u], t2 = rt[v];
            int ans = depth[u] + (step[t1] - step[t2] + len[t1]) % len[t1];
            int res = depth[v] + (step[t2] - step[t1] + len[t1]) % len[t1];
            if (check(depth[u], res, depth[v], ans)) {
                cout << depth[u] << ' ' << res << '\n';
            } else {
                cout << ans << ' ' << depth[v] << '\n';
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    coder();

    return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...