求优化

Mrnobody 2022-01-10 22:26:01

#include<bits/stdc++.h> using namespace std; const int N=1e6+10; int ans; int boom,x,y; int total,totalju; int fa[N*3]; int find(int x) { if(x!=fa[x]) { fa[x]=find(fa[x]); } return fa[x]; }

void together(int x,int y) { x=find(x); y=find(y); if(x!=y) fa[y]=x; }

int main() { cin>>total>>totalju; //对于每种生物:设 x 为本身,x+total为猎物,x+2*total为天敌 if(totalju==0) { ans=0; cout<<ans; return 0; }

for(int i=1;i<=total*3;i++)
{
	fa[i]=i;
}
for(int i=1;i<=totalju;i++)
{
	int boom,x,y;
	cin>>boom>>x>>y;
	if(x>total || y>total)
	{
		ans++;
		continue;
	}
	if(boom==1)
	{
		if(find(x+total)==find(y) || find(x+total*2)==find(y))
		{
			ans++;
			continue;
		}
		together(x,y);
		together(x+total,y+total);
		together(x+total*2,y+total*2);	
	}
	else if(boom==2)
	{
		if(find(x)==find(y) || find(x+total*2)==find(y))
		{
			ans++;
			continue;
		}
		together(x,y+total*2);
		together(x+total,y);
		together(x+total*2,y+total);
	}
}
cout<<ans;

}

共 1 条回复

long_hao

建议添加上 return 0;