这个为什么会RE?
查看原帖
这个为什么会RE?
1287996
jeff12205楼主2024/10/31 20:45
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int t, n, m;
const int N = 2e6 + 10;
const int M = 2000+10;

int in[N][2], edges[N][2];
bool flag = true;
// vector<int> edges[N];
vector<int> line[M];
int arange[M], color[M];

bool cross(int i, int j)
{
	int x1 = edges[i][0], y1 = edges[i][1], x2 = edges[j][0], y2 = edges[j][1];
	if (x1 > y1)
		swap(x1, y1);
	if (x2 > y2) {
		swap(x2, y2);
	}
	if (x1 < x2 && x2 < y1 && y1 < y2)
		return true;
	if (x2 < x1 && x1 < y2 && y2 < y1)
		return true;
	return false;
}

void dfs(int pa, int cur, int c)
{
	color[cur] = c;
	for (auto j : line[cur]) {
		if (j == pa)
			continue;
		if (color[j] == c) {
			flag = false;
			return;
		}
		if (color[j] == 0) {
			dfs(cur, j, c % 2 + 1);
		}
	}
	return;
}

void solve()
{
	cin >> n >> m;
	flag = true;
	for (int i = 1; i <= m; i++) {
		cin >> in[i][0] >> in[i][1];
	}
	for (int i = 1; i <= n; i++) {
		int j;
		cin >> j;
		arange[j] = i;
	}
	if (m > 3 * n - 6) {
		cout << "NO" << endl;
		return;
	}
	int k = 0, t = 0;
	for (int i = 1; i <= m; i++) {
		int x = arange[in[i][0]], y = arange[in[i][1]];
		if (x > y)
			swap(x, y);
		if (y-x != 1 && y-x != n-1) {
			edges[++k][0] = x;
			edges[k][1] = y;
		}
	}
	for (int i = 1; i <= k; i++) {
		for (int j = i + 1; j <= k; j++) {
			if (cross(i, j)) {
				line[i].push_back(j);
				line[j].push_back(i);
				t++;
			}
		}
	}
	for (int i = 1;i<=t ; i++){
		if (color[i] == 0)
			dfs(i, i, 1);
	}
	if (flag)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
	memset(color, 0, sizeof(color));
	for (int i = 1; i <= m; i++){
		line[i].clear();
	}
}

int main()
{
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}
2024/10/31 20:45
加载中...