0了,求调(带注释)
查看原帖
0了,求调(带注释)
760548
Jerry114514楼主2024/7/25 12:58
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define PII pair<int,int>
#define fir first
#define sec second
using namespace std;
const int N=1e5+7;
int n,m;
int cc,tt;
PII f[N];
PII g[N];//fir是祖先,sec是和它爹的关系
char s[N];//原始的元素
vector<PII> q[N];//与其他数的关系
inline int find(int x){
	if(f[x].fir==x){
		return x;
	}
	return f[x].fir=find(f[x].fir);
}
inline char calc(int i){
	if(s[i]=='T'){
		return 'F';
	}
	else if(s[i]=='F'){
		return 'T';
	}
	return s[i];
}
inline void init(){
	memset(s,'\0',sizeof s);
	for(int i=0;i<=n;i++){
		q[i].clear();
	}
	for(int i=1;i<=n;i++){
		f[i]={i,0};
	}
	return ;
}
inline PII total(int h,int e){
	if(f[h].fir==h){
		return {h,e};
	}
	int r=find(h);
	if(e==-2){
		for(auto i:q[h]){
			if(i.fir==r){
				e=i.sec;
				break;
			}
		}
	}
	else{
		for(auto i:q[h]){
			if(i.fir==r){
				int v=i.sec;
				if(f[r].fir==r){
					return {r,e};
				}
				else{
					if(v==1&&e==1){
						e=1;
					}
					else if(v==2&&e==1){
						e=2;
					}
					else if(v==1&&e==2){
						e=2;
					}
					else if(v==2&&e==2){
						e=1;
					}
				}
				return total(v,e);
			}
		}
	}
	return {0,0};
}
inline void print(){
	for(int i=1;i<=n;i++){
		printf("i=%d,fa=%d,relation=%d\n",i,f[i].fir,g[i].sec);
	}
	return ;
}
signed main(){
	freopen("tribool.in","r",stdin);
	freopen("tribool.out","w",stdout);
	cin.tie(NULL);
	scanf("%d%d",&cc,&tt);
	while(tt--){
		scanf("%d%d",&n,&m);
		init();
		char op;
		int x,y;
		for(int i=0;i<m;i++){
			cin>>op;
			if(op=='T'||op=='F'||op=='U'){
				scanf("%d",&x);
				s[x]=op;
			}
			else if(op=='+'){
				scanf("%d%d",&x,&y);
				q[x].push_back({y,1});
				f[x]={find(y),1};
			}
			else if(op=='-'){
				scanf("%d%d",&x,&y);
				q[x].push_back({y,2});
				f[x]={find(y),2};
			}
		}
		//从下往上对每个非根节点进行从下往上的归并
		for(int i=1;i<=n;i++){
			if(f[i].fir!=i){
				g[i]=total(i,-2);
			}
		}
//		print();
		printf("%d\n",f[1].fir);
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}
/*
对于pair
{a,b}
a代表那个元素
b代表关系
1<-->=
2<-->not
*/
2024/7/25 12:58
加载中...