火箭升空 @ 2020-02-23 00:06:48
我尽力了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#define maxn 2100
using namespace std;
vector<int > G[maxn];
int dfn[maxn], low[maxn], vis[maxn], color[maxn];
int T, n, m, num, tot;
stack<int> S;
void add(int x, int a, int y, int b) {
x = x * 2 + a;
y = y * 2 + b;
G[x^1].push_back(y);
G[y^1].push_back(x);
}
void init() {
for (int i = 0; i <= maxn; i++) G[i].clear();
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(vis, 0, sizeof(vis));
memset(color, 0, sizeof(color));
num = 0;
tot = 0;
while (!S.empty()) S.pop();
}
void Tarjan(int x) {
dfn[x] = low[x] = ++num;
vis[x] = 1;
S.push(x);
for (int i = 0; i < G[x].size(); i++) {
int y = G[x][i];
if (!dfn[y]) {
Tarjan(y);
low[x] = min(low[x], low[y]);
}
else if (vis[y]) {
low[x] = min(low[x], dfn[y]);
}
}
if (dfn[x] == low[x]) {
int y = S.top(); S.pop();
color[x] = ++tot;
vis[x] = 0;
while (x != y) {
color[y] = tot;
y = S.top(); S.pop();
vis[y] = 0;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> T;
while (T--) {
init();
cin >> n >> m;
string s1, s2;
int x, a, y, b;
for (int i = 0; i < m; i++) {
cin >> s1 >> s2;
if (s1[0] == 'h') a = 0;
else a = 1;
if (s2[0] == 'h') b = 0;
else b = 1;
x = s1[1] - '1';
y = s2[1] - '1';
add(x, a, y, b);
}
for (int i = 0; i < n * 2; i++) {
if (!dfn[i]) Tarjan(i);
}
bool bj = 1;
for (int i = 0; i < n; i++) {
if (color[i * 2] == color[i * 2 + 1]) {
bj = 0;
break;
}
}
if (bj) cout << "GOOD" << endl;
else cout << "BAD" << endl;
}
return 0;
}
by _Herobrine_ @ 2020-02-23 08:29:51
前排Orz
by jor蛋 @ 2023-10-18 14:27:43