社区讨论

求助厌氧代码

学术版参与者 4已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@lwr9qtug
此快照首次捕获于
2024/05/29 11:30
2 年前
此快照最后确认于
2024/05/29 15:59
2 年前
查看原帖
P3144
CPP
#include <iostream>

#include <vector>

#include <cstring>

#define endl '\n' 

using namespace std;

const int N = 3e3 + 15;

int n, m, a[N], b[N];
bool v[N][N], cnt[N];

vector<string>anss;

int function1(int x){
	if(x == b[x]){
		return x;
	}
	return b[x] = function1(b[x]);
}

int function2(int x, int y){
	b[function1(y)] = function1(x);
}

int main(){
	cin >> n >> m;
	for(int i = 1; i <= n; ++i){
		b[i] = i;
	}
	for(int i = 1, x, y; i <= m; ++i){
		cin >> x >> y;
		v[x][y] = v[y][x] = 1;
	}
	for(int i = 1; i <= n; ++i){
		cin >> a[i];
	}
	for(int i = n; i >= 1; --i){
		for(int j = 1; j <= n; ++j){
			if(cnt[j] == 1 && v[a[i]][j] == 1){
				function2(a[i], j);
			} 
		}
		int flag = function1(a[i]); 
		bool ans = 0;
		for(int j = 1; j <= n; ++j){
			if(cnt[j] == 1 && function1(b[j]) != flag){
				ans = 1;
			}
			// cout << function1(b[j]) << " ";
		}
		cnt[a[i]] = 1;
		anss.push_back(ans == 1 ? "NO" : "YES");
	}
	while(!anss.empty()){
		cout << anss.back() << endl;
		anss.pop_back();
	}
	return 0;
} 
用的并查集,开 O2 会 RE,不开全 A

回复

3 条回复,欢迎继续交流。

正在加载回复...