Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.6 kB
6
Indexable
#include <iostream>
#define N 305
using namespace std;
int n;
int a[N][N];
int b[N][N];
int sx[N];
int dd[N];
int sv,sl,sc;
void doc()
{
	sv=0;
	sl=0;
	sc=0;
	
	cin>>n;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			{
				cin>>a[i][j];
				b[i][j]=0;
		}
}

int bfs(int x)
{
	//cout<<x<<" - "<<y<<"\n";
	
	int sd=1;
	int dau=1,cuoi=0;
	sx[++cuoi]=x;
	dd[x]=1;
	while(dau<=cuoi)
	{
		int u=sx[dau];
		dau++;
		for(int v=1;v<=n;v++)
			{
				if(a[u][v]&&dd[v]==0)
		{
			dd[v]=1;
			sd++;
			sx[++cuoi]=v;
			
		}
		}
	}
	return sd;
}
int pre[N];
void bfs1(int x)
{
	//cout<<x<<" - "<<y<<"\n";
	
	int dau=1,cuoi=0;
	sx[++cuoi]=x;
	dd[x]=1;
	while(dau<=cuoi)
	{
		int u=sx[dau];
		dau++;
		for(int v=1;v<=n;v++)if(v!=pre[u])
			{
				if(a[u][v]&&dd[v]==0)
		{
			dd[v]=1;
			sx[++cuoi]=v;
			pre[v]=u;
		}else if(a[u][v]&&dd[v]==1)b[u][v]=b[v][u]=1;
		}
	}
}
int tinhvung()
{
	int ans=0;
	for(int i=1;i<=n;i++)dd[i]=0;
	for(int i=1;i<=n;i++)if(dd[i]==0)
	{
		ans++;
		bfs(i);
	}
	return ans;
}

int main()
{
	//freopen("input.txt","r",stdin);
	int TC;
	cin>>TC;
	for(int tc=1;tc<=TC;tc++)
	{
		doc();
		for(int i=1;i<=n;i++)dd[i]=0;
		bfs1(1);
		for(int i=1;i<=n;i++)dd[i]=0;
		for(int i=1;i<=n;i++)if(dd[i]==0)
		{
			if(bfs(i)==1)sl++;
			sv++;
		}
		//if(tc==49)sc=0;else
        {
		for(int i=1;i<=n;i++)
			for(int j=i+1;j<=n;j++)if(a[i][j]&&b[i][j]==0)
			{
				a[i][j]=a[j][i]=0;
				if(tinhvung()>sv)sc++;
				a[i][j]=a[j][i]=1;
			}
		}
			//cout<<"Case "<<tc<<"\n";
		cout<<sv<<" "<<sl<<" "<<sc<<"\n";
	}
	return 0;
}