题解:P17481 平衡路线

· · 题解

真的没有人直接用 CCF 的代码吗?

理解 CCF 的思路

当无法反复刷分平衡时(仅有加或减),输出距离。否则答案必为 1 或 0。

若为第二种情况,则通过二分图染色和 bfs 来判断是 0 还是 1。

代码

#include <iostream>
constexpr int N = 200005;
constexpr int M = 400005;
int n, m, s, t;
int h[N], e[M << 1], ne[M << 1], w[M << 1], idx;
int q[N], d[N], c[N];
void add(int a, int b, int z) {
    e[idx] = b;
    w[idx] = z;
    ne[idx] = h[a];
    h[a] = idx++;
}
int main() {
    std::cin >> n >> m >> s >> t;
    for (int i = 1; i <= n; i++)
        h[i] = d[i] = c[i] = -1;
    for (int i = 0; i < m; i++) {
        int a, b;
        char op[2];
        std::cin >> a >> b >> op;
        int z = op[0] == '+' ? 1 : -1;
        add(a, b, z);
        add(b, a, z);
    }
    int hh = 0, tt = 0;
    int p = 0, ng = 0, ok = 1;
    q[tt++] = s;
    d[s] = c[s] = 0;
    while (hh < tt) {
        int x = q[hh++];
        for (int i = h[x]; i != -1; i = ne[i]) {
            int y = e[i];
            if (w[i] > 0) p = 1;
            if (w[i] < 0) ng = 1;
            if (d[y] == -1) {
                d[y] = d[x] + 1;
                c[y] = c[x] ^ 1;
                q[tt++] = y;
            } 
            else if (c[y] == c[x]) ok = 0;
        }
    }
    if (d[t] == -1) {
        std::cout << -1;
        return 0;
    }
    if (!p || !ng) {
        std::cout << d[t];
        return 0;
    }
    if (!ok || c[s] == c[t]) std::cout << 0;
    else std::cout << 1;
    return 0;
}

管理员大大辛苦了!