蓝桥杯 七段码 并查集+二进制枚举/dfs

答案:80

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long ll;
ll ans=0;
int e[10][10];
int b[10],p[10];
void init()
{
	e[1][2]=e[1][6]=1;
	e[2][3]=e[2][7]=1;
	e[3][7]=e[3][4]=1;
	e[4][5]=1;
	e[5][6]=e[5][7]=1;
	e[6][7]=1;
}
int Find(int x)
{
	if(x!=p[x]) p[x]=Find(p[x]);
	return p[x];
}
bool check()
{
	for(int i=1; i<=7; i++) p[i]=i;
	for(int i=1; i<7; i++)
	{
		for(int j=i+1; j<=7; j++)
		{
			if((e[i][j]||e[j][i])&&b[i]&&b[j])
			{
				
				p[Find(i)]=Find(j);
			}
		}
	}
	int cnt=0,cnt2=0;
	for(int i=1; i<=7; i++)
	{
		if(b[i]) 
		{
			cnt2++;
			if(p[i]==i) cnt++;
		}
 	}
 	if(cnt==1&&cnt2>1) return true;
 	else return false;
}
int main()
{
	init();
	int ma=1<<7;
	//cout<<e<<endl;
	for(int i=0; i<ma; i++)
	{
		mem(b,0);
		int x=i;
		for(int j=1; j<=7; j++)
		{
			b[j]=x&1;
			x>>=1;
		}
		if(check()) ans++;
	}
	cout<<ans+7<<endl;
	return 0;
 } 
posted @ 2022-08-28 08:43  翔村亲亲鸟  阅读(21)  评论(0编辑  收藏  举报