Graph And Its Complement CodeForces - 990D (思维)

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 n,a,bn,a,b. You need to find an adjacency matrix of such an undirected graph that the number of components in it is equal to aa, and the number of components in its complement is bb. 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 nn consisting only of "0" and "1", where nn is the number of vertices of the graph and the ii-th row and the ii-th column correspond to the ii-th vertex of the graph. The cell (i,j)(i,j) of the adjacency matrix contains 11if and only if the ii-th and jj-th vertices in the graph are connected by an edge.

A connected component is a set of vertices XX 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 XX violates this rule.

The complement or inverse of a graph GG is a graph HH on the same vertices such that two distinct vertices of HH are adjacent if and only if they are not adjacent in GG.

Input

In a single line, three numbers are given n,a,b(1n1000,1a,bn)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.

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 nn lines, output nn digits such that jj-th digit of ii-th line must be 11if and only if there is an edge between vertices ii and jj in GG (and 00 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.

Examples
input
Copy
3 1 2
output
Copy
YES
001
001
110
input
Copy
3 3 3
output
Copy
NO

题意:给出n,a,b,问,是否有这样的一个图,有n个节点,分成a块,现将两两节点之间有边的把边删除,两两节点之间无边的建边,使得形成的图分成b块。如果有,输出YES,并且输出这个图的邻接矩阵,否则输出NO。这题难就难在是否能推出结论以及答案的输出。


思路:任何一个图,不管分成多少块,经过上述操作后,必然变成一个联通图。

证:有n个节点,分成m块,任取一块,经过上述操作后,所取的这一块内的任意节点必定与其他块的所有节点相连!

这个结论换句话说就是,a和b里面,至少有一个数是1!知道这个结论就简单了,首先排除所有不含1的情况,然后,b==1的情况,我们将图分成a块,直接另前a-1块都只有一个节点,最后1块包含剩下的节点就行了。当然,对于a==1,只要将要输出反过来一下就可以了(注意,这里要讨论一下a和b都为1的情况,当n==2和n==3时,是不行的直接排除,其他情况都是可行的,特殊处理一下a和b都等于1的输出)我的输出比较繁琐,写完之后,看了一下大佬的输出方法,然后突然发现自己好菜啊QAQ。附上我的写法和大佬的写法(思路是一样的,输出不同)。


菜鸡的写法

#include "iostream"
#include "algorithm"
using namespace std;
const int Max=1e3+10;
int n,a,b,G[Max][Max],vis[Max];
char ch1='1',ch2='0';
int main() {
    cin>>n>>a>>b;
    if (a!=1&&b!=1||a==1&&b==1&&n<=3&&n>=2||a>n||b>n){
        cout<<"NO"<=a) cout<

大佬的写法

#include "iostream"
#include "algorithm"
using namespace std;
int n,a,b;
char ch1='0',ch2='1';
int main() {
    cin>>n>>a>>b;
    if (a!=1&&b!=1||a==1&&b==1&&n<=3&&n>=2||a>n||b>n) {cout<<"NO"<=a||j+1==i&&i>=a?ch1:ch2));
        }
        cout<

你可能感兴趣的:(水题)