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个联通块。

题解:仔细想一下,会发现a与b中一定有一个为1.(若Va与Vb在一个联通块中,且存在Vc在另一个联通块中,则Va与Vb由Vc相连,在补图中也在同一个联通块内)。

所以当a,b均不为1时一定输出NO

要特别注意3,1,1和2,1,1的情况也是无法实现的。

代码:

#include <bits/stdc++.h>

using namespace std;

const int maxn = 300005;
int n, k, tmp, tot;
int a, b;
char str[maxn];
int ans[1005][1005];

int main()
{
    scanf("%d%d%d",&n, &a, &b);
    if (a != 1 && b != 1){
        printf("NO\n");
        return 0;
    }
    memset(ans,0,sizeof(ans));
    if (a == 1 && b == 1){
        if (n == 3 || n == 2){
            printf("NO\n");
        } else {
            printf("YES\n");
            for (int i=1; i<n; i++){
                ans[i][i+1] = ans[i+1][i] = 1;
            }
            for (int i=1; i<=n; i++){
                for (int j=1; j<=n; j++){
                    printf("%d",ans[i][j]);
                }
                cout<<endl;
            }
        }
        return 0;
    }
    if (a != 1){
        for (int i=2; i<=n-a+1; i++){
            ans[1][i] = 1;
            ans[i][1] = 1;
        }
    } else {
        for (int i=1; i<=n; i++)
            for (int j=1; j<=n; j++)
                if (i != j) ans[i][j] = 1;
        for (int i=2; i<=n-b+1; i++){
            ans[1][i] = 0;
            ans[i][1] = 0;
        }
    }
    cout<<"YES\n";
    for (int i=1; i<=n; i++){
        for (int j=1; j<=n; j++){
            printf("%d",ans[i][j]);
        }
        cout<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值