Given three numbers n,a,b. You need to find an adjacency matrix of such an undirected graph that the number of components in it is equal to a, and the number of components in its complement is b
. The matrix must be symmetric, and all digits on the main diagonal must be zeroes.
In an undirected graph loops (edges from a vertex to itself) are not allowed. It can be at most one edge between a pair of vertices.
The adjacency matrix of an undirected graph is a square matrix of size n
-th vertices in the graph are connected by an edge.
A connected component is a set of vertices X
violates this rule.
The complement or inverse of a graph G
.
In a single line, three numbers are given n,a,b(1≤n≤1000,1≤a,b≤n)
: is the number of vertexes of the graph, the required number of connectivity components in it, and the required amount of the connectivity component in it's complement.
If there is no graph that satisfies these constraints on a single line, print "NO" (without quotes).
Otherwise, on the first line, print "YES"(without quotes). In each of the next n
otherwise). Note that the matrix must be symmetric, and all digits on the main diagonal must be zeroes.
If there are several matrices that satisfy the conditions — output any of them.
3 1 2
YES 001 001 110
3 3 3
NO
题意:构建一个图,这个图有n个点,有a个连通分支,并且它的补图有b个连通分支。如果有则输出这个图,没有则输出-1。
题解:首先是a,b都大于1的情况是不存在的,因为如果一个图是非连通的,那么它的补图一定是连通的,这是离散里面的一个结论吧。然后是a,b都为1的情况,当n>=4的时候,这个图为最小单连通图。还有个特判:a = b = 1 时 输出0。
然后就是构图了a,b其中一个为1时,先构建不为1的那个图,因为这个图更好建,只需要加n - x个边就行了(x为连通分支数),然后另一个直接求补图就好。当a,b都为1时,直接建一条链就行了。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int M[maxn][maxn],n,a,b;
int main()
{
while(scanf("%d%d%d",&n,&a,&b) != EOF)
{
memset(M,0,sizeof(M));
if(a != 1 && b != 1)
{
printf("NO\n");
continue;
}
else if(a == 1 && b == 1)
{
if(n >= 4)
{
for(int i = 1;i <= n - 1;i++)
{
M[i][i + 1] = 1;
M[i + 1][i] = 1;
}
}
else if(n == 1)
{
printf("YES\n0\n");
continue;
}
else
{
printf("NO\n");
continue;
}
}
else
{
if((a == 1 && b > n) || (b == 1 && a > n))
{
printf("NO\n");
continue;
}
if(a == 1)
{
int cont = n - b;
for(int i = 1;i <= cont;i++)
{
M[i][i + 1] = 1;
M[i + 1][i] = 1;
}
for(int i = 1;i <= n;i++)
{
for(int j = i + 1;j <= n;j++)
{
if(M[i][j] == 1)
{
M[i][j] = M[j][i] = 0;
}
else
{
M[i][j] = M[j][i] = 1;
}
}
}
}
if(b == 1)
{
int cont = n - a;
for(int i = 1;i <= cont;i++)
{
M[i][i + 1] = 1;
M[i + 1][i] = 1;
}
}
}
printf("YES\n");
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
printf("%d",M[i][j]);
}
printf("\n");
}
}
return 0;
}