【CodeForces 990D】Graph And Its Complement

传送门

luogu & CodeForces

题目描述

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.

 

 题目翻译(摘自luogu)

一张无向图一共有n个点,a个联通块,它的补图一共有b个联通块,如果能构造出这张图输出'YES'并输出它的邻接矩阵,否则输出'NO'

输入:

三个整数n,a,b

输出:

如果有解输出'YES'和邻接矩阵,否则输出'NO'

感谢@zhaotiensn 提供翻译

解题思路

首先我们得要判断能否根据题目所给的数据构造出一个图,那怎么办呢?

代码

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int n,a,b;
 7 bool map[1005][1005];
 8 int main(){
 9     cin>>n>>a>>b;
10     if(a>1&&b>1){
11         cout<<"NO"<<endl;
12     }
13     else if(a>n||b>n){
14         cout<<"NO"<<endl;
15     }
16     else if(a==1&&b==1){
17         if(n<=3&&n>1)cout<<"NO"<<endl;
18         else {
19             cout<<"YES"<<endl;
20             for(register int i=1;i<n;i++){
21                 map[i][i+1]=map[i+1][i]=1;
22             }
23             for(register int i=1;i<=n;i++){
24                 for(register int j=1;j<=n;j++){
25                     printf("%d",map[i][j]);
26                 }
27                 putchar('\n');
28             }
29         }
30     }
31     else if(a>1){
32         cout<<"YES"<<endl;
33         for(register int i=a;i<=n;i++){
34             for(register int j=a;j<=n;j++){
35                 if(i==j)continue;
36                 map[i][j]=true;
37             }
38         }
39         for(register int i=1;i<=n;i++){
40             for(register int j=1;j<=n;j++){
41                 if(i==j)putchar('0');
42                 else printf("%d",map[i][j]);
43             }
44             putchar('\n');
45         }
46     }
47     else if(a==1){
48         cout<<"YES"<<endl;
49         for(register int i=b;i<=n;i++){
50             for(register int j=b;j<=n;j++){
51                 if(i==j)continue;
52                 map[i][j]=true;
53             }
54         }
55         for(register int i=1;i<=n;i++){
56             for(register int j=1;j<=n;j++){
57                 if(i==j)putchar('0');
58                 else printf("%d",1-map[i][j]);
59             }
60             putchar('\n');
61         }
62     }
63 }

猜你喜欢

转载自www.cnblogs.com/Fang-Hao/p/9254620.html
今日推荐