Educational Codeforces Round 45 990 D. Graph And Its Complement [构造 + 图论]

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,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 consisting only of "0" and "1", where n is the number of vertices of the graph and the i - th row and the i - th column correspond to the i-th vertex of the graph. The cell (i,j)(i,j) of the adjacency matrix contains 1 if and only if the i - th and j - th vertices in the graph are connected by an edge.

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

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

Input

In a single line, three numbers are given n,a,b(1n1000,1a,bn): 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 lines, output n digits such that j - th digit of ii-th line must be 1 if and only if there is an edge between vertices i and j in G (and 0 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个连通分量);

分析:多画几个试一下,就会发现图和补图至少一个是连通图,那么就是a和b至少一个为1,这样就能转化成构造一个联通区域为x的无向图的问题了,但是还要特判一下 n == 2|| n == 3 的时候,a,b只能一个为1,n >= 4是可以出现两个都为1的情况。

代码:

#include

using namespace std;
const int maxn = 1e3+7;
int n, a, b, v[maxn][maxn];

void solve(int c)
{
    puts("YES");
    memset(v, 0, sizeof(v));
    bool f = true;
    if(c < 0) f = false, c = - c;
    if(c == 1)
        for(int i = 2; i <= n; i++) v[i-1][i] = v[i][i-1] = 1;
    else
        for(int i = c+1; i <= n; i++) v[i-1][i] = v[i][i-1] = 1;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
            printf("%d", f||(i == j) ? v[i][j] : !v[i][j]);
        printf("\n");
    }
}

int main()
{
    //freopen("in.txt", "r", stdin);
    while(~scanf("%d%d%d", &n, &a,&b)) {
        if(a!=1&&b!=1) puts("NO");
        else if(a == 1&& b == 1) {
            if(n == 1) puts("YES\n0");
            else if(n < 4) puts("NO");
            else solve(1);
        } else if(a != 1) solve(a);
          else if(b != 1) solve(-b);
    }
    return 0;
}

你可能感兴趣的:(图论)