码迷,mamicode.com
首页 > 其他好文 > 详细

CF 990D Graph And Its Complement 第十八 构造、思维

时间:2018-06-26 22:35:07      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:output   tar   blog   putc   mit   end   condition   directed   note   

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

 

 引用别人的题解了。。。

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

 

CF 990D Graph And Its Complement 第十八 构造、思维

标签:output   tar   blog   putc   mit   end   condition   directed   note   

原文地址:https://www.cnblogs.com/l609929321/p/9231375.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!