类 Tarjan 做法 15 分,求助!
查看原帖
类 Tarjan 做法 15 分,求助!
1046228
yezicong1104楼主2024/9/17 12:18

Subtask #2 和 Subtask #3 是对的,Subtask #0 和 Subtask #1 有 WA,Subtask #4 和 Subtask #5 有 TLE 和 WA,求助!

#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for(int i = l; i <= r; i++)
typedef long long LL;
const int N = 200010;
bool vis[N], color[N];
int dep[N];
vector<int> g[N];
map<LL, bool> edge;
void dfs(int x) {
	vis[x] = 1;
	for (int y : g[x])
		if (!vis[y]) edge[(LL)x * N + y] = 1, dep[y] = dep[x] + 1, dfs(y);
}
void dfs2(int x) {
	vis[x] = color[x] = 1;
	for (int y : g[x])
		if (!vis[y] && dep[y] > dep[x]) dfs(y);
}
int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		int n, m, u, v, x = 0, y = 0;
		scanf("%d%d", &n, &m);
		rep(i, 1, n) g[i].clear(), vis[i] = dep[i] = color[i] = 0;
		edge.clear();
		rep(i, 1, m) {
			scanf("%d%d", &u, &v);
			g[u].push_back(v), g[v].push_back(u);
		}dfs(1);
		bool flag = 0;
		rep(i, 1, n) if (!vis[i]) flag = 1;
		if (flag) {printf("-1\n"); continue;}
		rep(i, 1, n) for (int j : g[i])
			if (!edge[(LL)i * N + j] && !edge[(LL)j * N + i])
				x = i, y = j;
		if (x * y == 0) {printf("-1\n"); continue;}
		if (dep[x] > dep[y]) swap(x, y);
		rep(i, 1, n) vis[i] = 0; dfs2(y);
		rep(i, 1, n) printf(color[i] ? "B" : "W");
		puts("");
	}
	return 0;
}
2024/9/17 12:18
加载中...