zoukankan      html  css  js  c++  java
  • CF 990D Graph And Its Complement 第十八 构造、思维

    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('
    ');
            }
        }
        return 0;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    SpringMVC(四)-- 文件下载、自定义拦截器、异常处理
    SpringMVC(三)-- 视图和视图解析器、数据格式化标签、数据类型转换、SpringMVC处理JSON数据、文件上传
    SpringMVC(二)--处理数据模型、ModelAndView、Model、Map、重定向、@ModelAttribute、
    SpringMVC(一)--基础、REST、@RequestParam、POST请求乱码等
    反射
    servlet基础
    eclipse为项目设置jdk
    mysql创建表时符号``的作用
    redis进阶
    相对路径和绝对路径的区别
  • 原文地址:https://www.cnblogs.com/l609929321/p/9231375.html
Copyright © 2011-2022 走看看