社区讨论
WA on6~13求助
P9220「TAOI-1」椎名真昼参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @m411fqor
- 此快照首次捕获于
- 2024/11/28 16:13 去年
- 此快照最后确认于
- 2025/11/04 13:45 4 个月前
Rt,感觉没什么问题,求大佬帮忙看一眼
CPP#include <bits/stdc++.h>
#define maxn 100010
#define maxm 200010
using namespace std;
template < typename T >
inline void read(T &X)
{
X = 0; bool f = false; char ch = getchar();
while (!isdigit(ch)) {f |= ch == '-'; ch = getchar();}
while (isdigit(ch)) {X = (X * 10) + (ch ^ 48); ch = getchar();}
X = f ? -X : X;
}
struct side {int to, nxt;} sidelist[maxm];
int head[maxn], sidecnt;
inline void buildside(const int &u, const int &v) {sidelist[++sidecnt] = {v, head[u]}; head[u] = sidecnt;}
int T;
int n, m;
int col[maxn], colcnt[2];
int dfn[maxn], low[maxn];
bool instack[maxn];
int dfncnt;
stack < int > st;
int dcc[maxn], dcccol[maxn];
int dcccnt;
void Karjan(const int &x)
{
dfn[x] = low[x] = ++dfncnt;
st.emplace(x); instack[x] = true;
for (int i = head[x]; i; i = sidelist[i].nxt)
{
int to = sidelist[i].to;
if (instack[to]) low[x] = min(low[x], dfn[to]);
else if (!dfn[to]) {Karjan(to); low[x] = min(low[x], low[to]);}
}
if (dfn[x] == low[x])
{
++dcccnt; dcc[x] = dcccnt; instack[x] = false;
while (st.top() != x) {dcc[st.top()] = dcccnt; instack[st.top()] = false; st.pop();}
st.pop();
}
}
vector < int > newside[maxn];
int in[maxn];
queue < int > q;
bool dfs(const int &x)
{
if (dcccol[x] == 0) return true;
--colcnt[1];
bool f = false;
for (auto to : newside[x]) f |= dfs(to);
return f;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("P9220.in", "r", stdin);
freopen("P9220.out", "w", stdout);
#endif
read(T);
while (T--)
{
memset(head, 0, sizeof(head)); sidecnt = 0;
memset(dfn, 0, sizeof(dfn)); memset(low, 0, sizeof(low)); dcccnt = 0;
memset(dcccol, -1, sizeof(dcccol));
colcnt[0] = colcnt[1] = 0;
while (!q.empty()) q.pop();
read(n); read(m);
for (int i = 1; i <= n; ++i) read(col[i]);
for (int i = 1; i <= m; ++i)
{
int u, v;
read(u); read(v);
buildside(u, v);
}
for (int i = 1; i <= n; ++i) if (!dfn[i]) Karjan(i);
bool f = false;
for (int i = 1; i <= n; ++i)
{
if (dcccol[dcc[i]] == -1) dcccol[dcc[i]] = col[i], ++colcnt[col[i]];
f |= dcccol[dcc[i]] != col[i];
for (int j = head[i]; j; j = sidelist[j].nxt)
{
int to = sidelist[j].to;
if (dcc[i] != dcc[to]) {newside[dcc[i]].emplace_back(dcc[to]); ++in[to];}
}
}
if (f) {putchar('N'); continue;}
for (int i = 1; i <= dcccnt; ++i) if (!in[i]) q.emplace(i);
if (colcnt[1] == 0 || (colcnt[1] == 2 && colcnt[0] == 0 && q.size() == 2) || (colcnt[1] == 1 && colcnt[0] == 1 && q.size() == 1 && dcccol[q.front()] == 1)) {putchar('B'); continue;}
int x = q.front();
while (!q.empty() && dcccol[x] == 0)
{
q.pop();
for (auto to : newside[x]) q.push(to);
if (!q.empty()) x = q.front();
}
if (!dfs(x)) putchar(colcnt[1] == 0 ? 'A' : 'N');
else putchar('N');
}
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...