求调,sub4WA前三个点
查看原帖
求调,sub4WA前三个点
676634
Albert_Wei楼主2024/8/6 12:52
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define MP make_pair
typedef long long LL;
typedef unsigned long long ull;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<ull, int> pui;
const int MOD = 998244353, N = 3e5 + 7;
template<typename T>
T& Fmin(T &x, T y) { return x = x < y ? x : y; }
template<typename T>
T& Fmax(T &x, T y) { return x = y < x ? x : y; }

LL read() {
    char c = getchar(); LL x = 0, p = 1;
    while ((c < '0' || c > '9') && c != '-') c = getchar();
    if (c == '-') p = -1, c = getchar();
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return x * p;
}

bool vis[N], shx[N], dp[N];
vector<int> e[N], scc[N], scc_e[N];
int n, m, tot, a[N], col[N], sk[N], tp;
int dfn[N], low[N], sum[N], cnt, cn, siz[N];

void Tarjan(int x) {
    dfn[x] = low[x] = ++ cn;
    vis[x] = 1, sk[++ tp] = x;
    for (int i : e[x]) {
        if (!vis[i]) Tarjan(i), Fmin(low[x], low[i]);
        else if (!col[i]) Fmin(low[x], dfn[i]);
    }
    if (low[x] == dfn[x]) {
        cnt ++; int y;
        do {
            col[y = sk[tp --]] = cnt;
            scc[cnt].push_back(y);
        } while (y != x);
    }
}

void clr() {
    for (int i = 1; i <= n; i ++) e[i].clear();
    for (int i = 1; i <= cnt; i ++)
        scc[i].clear(), scc_e[i].clear();
    cn = cnt = tp = tot = 0;
    memset(dp, 0, sizeof(dp));
    memset(vis, 0, sizeof(vis));
    memset(col, 0, sizeof(col));
    memset(sum, 0, sizeof(sum));
    memset(siz, 0, sizeof(siz));
}

void mian() {
    clr();
    n = read(), m = read();
    for (int i = 1; i <= n; i ++)
        tot += (a[i] = read());
    for (int i = 1, u, v; i <= m; i ++) {
        u = read(), v = read();
        e[u].push_back(v);
    }
    for (int i = 1; i <= n; i ++)
        if (!vis[i]) Tarjan(i);
    if (tot == 0) return putchar('B'), void();
    for (int i = 1; i <= n; i ++) {
        sum[col[i]] += a[i];
        for (int j : e[i])
            if (col[i] != col[j])
                scc_e[col[i]].push_back(col[j]);
    }
    for (int i = 1; i <= cnt; i ++)
        if (sum[i] % scc[i].size() != 0)
            return putchar('N'), void();
        else sum[i] /= scc[i].size();
    if (cnt == 2) {
        if (!scc_e[2].size() && sum[2] && sum[1]) return putchar('B'), void();
        if (scc_e[2].size() && sum[2] && !sum[1]) return putchar('B'), void();
    }
    for (int i = 1; i <= cnt; i ++) {
        siz[i] = scc[i].size() * sum[i], dp[i] = sum[i];
        for (int j : scc_e[i])
            if (!shx[j]) shx[j] = 1, siz[i] += siz[j], dp[i] = dp[i] && dp[j];
        for (int j : scc_e[i]) shx[j] = 0;
    }
    for (int i = 1; i <= cnt; i ++)
        if (dp[i] && siz[i] == tot) return putchar('A'), void();
    putchar('N');
}

int main() {
    int tt; cin >> tt;
    while (tt --) mian();
    return 0;
}
2024/8/6 12:52
加载中...