调了一下午,始终找不出错误在哪里,求助巨佬!!!

P4171 [JSOI2010] 满汉全席

JeffZhao @ 2021-07-16 19:06:22

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;

const int N = 1e5 + 10;

inline int du(void) {
    int x = 0, f = 1;
    char ch = getchar();
    while (ch > '9' || ch < '0') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}
inline void write(int x) {
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9)write(x / 10);
    putchar(x % 10 ^ 48);
    return;
}

int k, n, m;

int head[N << 1], nx[N << 1], ver[N << 1], cnt;
inline void add(int x, int y) {
    ver[cnt] = y;
    nx[cnt] = head[x]; head[x] = cnt++;
    return;
}

int dfn[N << 1], low[N << 1], ti, top, st[N << 1], vis[N << 1], kind;
int co[N << 1];
inline void tarjan(int u) {
    dfn[u] = low[u] = ++ti;
    st[++top] = u, vis[u] = true;

    for (int i = head[u]; ~i; i = nx[i]) {
        int v = ver[i];
        if (!dfn[v]) {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if (vis[v]) low[u] = min(low[u], dfn[v]);
    }

    if (dfn[u] == low[u]) {
        int v;
        ++kind;
        while ((v = st[top--]) != 0) {
            co[v] = kind;
            vis[v] = false;
            if (u == v)break;
        }
    }

    return;
}

int main(void) {

    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    cin >> k;

    while (k--) {
        cin >> n >> m;

        memset(head, -1, sizeof(head));
        memset(nx, 0, sizeof(nx));
        memset(ver, 0, sizeof(ver));
        cnt = 0;

        while (m--) {
            string a, b;
            cin >> a >> b;
            int i, j, vi, vj;
            if (a[0] == 'm')vi = 0;
            if (a[0] == 'h')vi = 1;
            if (b[0] == 'm')vj = 0;
            if (b[0] == 'h')vj = 1;
            i = a[1] - '0';
            j = b[1] - '0';//坑点

            if (vi && vj) {
                add(i + n, j);
                add(j + n, i);
            }
            if (!vi && vj) {
                add(i, j);
                add(j + n, i + n);
            }
            if (vi && !vj) {
                add(i + n, j + n);
                add(j, i);
            }
            if (!vi && !vj) {
                add(i, j + n);
                add(j, i + n);
            }
        }

        memset(dfn, 0, sizeof(dfn));
        memset(low, 0, sizeof(low));
        memset(st, 0, sizeof(st));
        memset(vis, 0, sizeof(vis));
        memset(co, 0, sizeof(co));
        ti = top = kind = 0;

        for (int i = 1; i <= (n << 1); ++i) {
            if (!dfn[i])
                tarjan(i);
        }

        bool flag = true;

        for (int i = 1; i <= n; ++i) {
            if (co[i] == co[i + n]) {
                //cout << "BAD" << endl;
                flag = false;
                //break;    
            }
        }
        if (!flag)cout << "BAD" << endl;
        else if (flag)cout << "GOOD" << endl;

    }

    return 0;
}

by Acestar @ 2021-07-18 21:46:57

@JeffZhao 您的 ij 有问题,不一定是一位数,我一开始也是这里错了qwq


by JeffZhao @ 2021-07-18 22:15:09

@Acestar 多谢了


|