并查集 wa55pts
查看原帖
并查集 wa55pts
482660
konyakest楼主2022/5/29 17:30

调了两个小时未果

// Problem: P7073 [CSP-J2020] 表达式
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P7073
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define F(i,j,k) for (signed i=signed(j);i<=signed(k);i++)
#define endl '\n'
//#define int long long
string s;
int n,a[(int)1e5+5],fa[(int)1e5+5],num[(int)1e5+5];
bool flag[(int)1e5+5];
int find_root(int x){
	if(fa[x]==x) return x;
	return fa[x]=find_root(fa[x]);
}
struct node{
	int res,id;
};
stack<node> stk;
main() { 
	// ios::sync_with_stdio(0);
	// cin.tie(0);
	// cout.tie(0);
	getline(cin,s);
	cin>>n;
	F(i,1,n) fa[i]=i;
	F(i,1,n) cin>>a[i];
	vector<int> st,stda;
	int cnt=-1,tot=0;
	for(auto i:s){
		if(isdigit(i)) {
			if(cnt==-1) cnt=0;
			cnt=cnt*10+i-'0';
		}
		else if(cnt!=-1) tot++,st.push_back(cnt),num[tot]=cnt,cnt=-1;
	}
	stda.resize(n+5);
	F(i,1,n) {
		stda[i]=a[st[i-1]];
	}
	cnt=0;
	for(auto i:s){
		if(i=='x') stk.push({stda[++cnt],cnt});
		else if(isdigit(i)||i==' ') continue;
		else if(i=='!') {
			node n1=stk.top();
			stk.pop();
			n1.res=!n1.res;
			stk.push(n1);
		} 
		else if(i=='&'){
			node n1=stk.top();stk.pop();
			node n2=stk.top();stk.pop();
			if(n1.res==0&&n2.res==0) stk.push({0,0});
			else if(n1.res==1&&n2.res==0) stk.push({0,n2.id});
			else if(n1.res==0&&n2.res==1) stk.push({0,n1.id});
			else{
				fa[find_root(n1.id)]=find_root(n2.id);
				stk.push({1,n2.id});
			}
		}
		else if(i=='|'){
			node n1=stk.top();stk.pop();
			node n2=stk.top();stk.pop();
			if(n1.res==1&&n2.res==1) stk.push({1,0});
			else if(n1.res==1&&n2.res==0) stk.push({1,n1.id});
			else if(n1.res==0&&n2.res==1) stk.push({1,n2.id});
			else{
				fa[find_root(n1.id)]=find_root(n2.id);
				stk.push({0,n2.id});
			}
		}
	}
	int father=find_root(stk.top().id);
	F(i,1,n) if(find_root(i)==father) flag[num[i]]=1;
	int ans=stk.top().res;
	int m,op;
	cin>>m;
	F(i,1,m) cin>>op,cout<<(flag[op]?(!ans):ans)<<endl;
	return 0; 
}






2022/5/29 17:30
加载中...