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 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 j-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.

Input

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.

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 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

 

 引用别人的题解了。。。

https://www.cnblogs.com/siuginhung/p/9172602.html

这是一个构造问题。

构造一张n阶简单无向图G,使得其连通分支个数为a,且其补图的连通分支个数为b。

对于一张n阶简单无向图G,若此图不连通,则其补图是连通的。

证明:

首先,在简单无向图G中,若结点u、v(u≠v)不连通,则在其补图中,u、v必然连通。

将图G=<V,E>划分为k个连通分支,Gi=<Vi,Ei>,i=1,2,...,k。在V中任取两点u、v(u≠v)。

u∈Vi,v∈Vj,且i≠j,则u、v在图G中不连通,则u、v必然在其补图中连通;

u,v∈Vi,则必然存在w∈Vj,且i≠j,使得u、w和v、w在补图中连通。

于是,在题中,a、b中至少有一个为1。

接下来构造连通分支:若一个n阶简单无向图有k(k≥2)个连通分支,则可以构造其连通分支分别为{1},{2},...,{k-1},{k,k+1,...,n}。

 

这是我的代码

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e3 + 10;
const int mod = 1e9 + 7;
typedef long long ll;
int mapn[maxn][maxn];
int main(){
    std::ios::sync_with_stdio(false);
    ll n, a, b;
    while( cin >> n >> a >> b ) {
        bool flag = true;
        if( a != 1 && b != 1 ) {
            flag = false;
        }
        if( ( n == 2 || n == 3 ) && ( a + b == 2 ) ) {
            flag = false;
        }
        if( !flag ) {
            cout << "NO" << endl;
            continue;
        }
        cout << "YES" << endl;
        if( b == 1 ) {
            memset( mapn, 0, sizeof(mapn) );
            for( ll i = a; i < n; i ++ ) {
                 mapn[i-1][i] = 1;
                 mapn[i][i-1] = 1;
            }
        } else {
            memset( mapn, -1, sizeof(mapn) );
            for( ll i = 0; i < n; i ++ ) {
                mapn[i][i] = 0;
            }
            for( ll i = b; i < n; i ++ ) {
                mapn[i-1][i] = 0;
                mapn[i][i-1] = 0;
            }
        }
        for( ll i = 0; i < n; i ++ ) {
            for( ll j = 0; j < n; j ++ ) {
                putchar( mapn[i][j] ? '1' : '0' );
            }
            putchar('\n');
        }
    }
    return 0;
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-25
  • 2022-12-23
  • 2021-08-15
  • 2021-10-22
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-23
  • 2021-10-21
  • 2021-06-12
  • 2019-08-11
  • 2021-09-08
相关资源
相似解决方案