cf Educational Codeforces Round 45 D. Graph And Its Complement

原题:

D. Graph And Its Complement
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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) of the adjacency matrix contains 1if 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(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 n lines, output n digits such that j-th digit of
i-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
3 1 2
output
YES
001
001
110
input
3 3 3
output
NO

中文:

给你三个数,n,a,b表示有一个无向图,图中有n个顶点,组成a个联通分量。现在让你找这个图的补图,能否组成b个联通分量?

补图的意思就是相连的边全去掉,没有的边全连上。

代码:

#include
using namespace std;
typedef long long ll;

int mat[1001][1001];
int n,a,b;

int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>a>>b)
    {
        if(a!=1&&b!=1)
        {
            cout<<"NO"<continue;
        }

        memset(mat,0,sizeof(mat));
        if(a==1&&b==1)
        {
            if(n==2||n==3)
            {
                cout<<"NO"<continue;
            }
            for(int i=1;i1][i]=mat[i][i+1]=1;
            }
        }
        else
        {
            if(a==1)
            {
                for(int i=b;i<=n;i++)
                {
                    for(int j=b;j<=n;j++)
                    {
                        if(j==i)
                            continue;
                        mat[i][j]=1;
                    }
                }
                for(int i=1;i<=n;i++)
                {
                    for(int j=1;j<=n;j++)
                    {
                        if(j==i)
                            continue;
                        mat[i][j]=1-mat[i][j];
                    }
                }
            }
            else
            {
                for(int i=a;i<=n;i++)
                {
                    for(int j=a;j<=n;j++)
                    {
                        if(j==i)
                            continue;
                        mat[i][j]=1;
                    }
                }
            }
        }
        cout<<"YES"<for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
                cout<cout<return 0;
}

思路:

cf如果以后都是这种题,我就不做了 -_-

一般构造算法的题目先在纸上画一画,感觉是什么样,差不多这题就是什么样。

可以,得到这样的结论,如果a等于1或者b等于1的时候才能够形成对应的补图,有两个特殊情况,就是在a和b都是1的情况下,节点数为2和3的是不会形成对应的补图的。

简单的证明方式如下,考虑有一个图有a个联通分量记为 ai a i ,这里面a大于1,如果这a个联通分析形成补图,那么一定是每一个联通分量 ai a i 中的顶点都会与其他联通分量的顶点相连接,最后得到的总联通分量必定是1。反过来如果有b个联通分量,寻找其补图是否可以为a个联通分量,同理可证。

知道结论了以后,直接按照最脑残的方式去构造即可。

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