【CF990DGraph And Its Complement】图论构造
做的第一道构造题OwOCF990D
lines, output digits such that -th digit of -th line must be if and only if there is an edge between vertices and in (and
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.
D. Graph And Its Complement
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Given three numbers
. You need to find an adjacency matrix of such an undirected graph that the number of components in it is equal to , and the number of components in its complement is
. 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
consisting only of "0" and "1", where is the number of vertices of the graph and the -th row and the -th column correspond to the -th vertex of the graph. The cell of the adjacency matrix contains if and only if the -th and
-th vertices in the graph are connected by an edge.
A connected component is a set of vertices
such that for every two vertices from this set there exists at least one path in the graph connecting this pair of vertices, but adding any other vertex to
violates this rule.
The complement or inverse of a graph
is a graph on the same vertices such that two distinct vertices of are adjacent if and only if they are not adjacent in
.
Input
In a single line, three numbers are given
: 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.
Output
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使得正图连通量为a,补图连通量为b。题解
显然必定正图和补图中有一个的联通量为1,那么就很好构造了,考虑构造不为1的那个图,如为 a ,那么我们构造a-1个单点,其他连通即可。 但是要考虑特殊情况,当两个连通量都为1的情况,可以发现当n=2,3不行,n=1特判直接YES 0 ,n>=4的时候直接构造一条链就可以了。#include<bits/stdc++.h> using namespace std; int n,a,b; int ma[1005][1005]; int main() { scanf("%d%d%d",&n,&a,&b); if(a>n||b>n) { printf("NO"); return 0; } if((a!=1)&&(b!=1)) { printf("NO"); return 0; } if(a>1) { for(int i=a+1;i<=n;i++) { ma[i][a] = ma[a][i] = 1; } printf("YES\n"); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { printf("%d",ma[i][j]); } printf("\n"); } return 0; } if(b>1) { for(int i=b+1;i<=n;i++) { ma[i][b] = ma[b][i] = 1; } for(int i=1;i<=n;i++) ma[i][i] = 1; printf("YES\n"); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { printf("%d",!ma[i][j]); } printf("\n"); } return 0; } if(a==1&&b==1&&n>=4) { printf("YES\n"); for(int i=1;i<=n-1;i++) ma[i][i+1] = ma[i+1][i] = 1; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { printf("%d",ma[i][j]); } printf("\n"); } return 0; } if(a==1&&b==1) { if(n==1) { printf("YES\n0"); return 0; } else { printf("NO"); return 0; } } }