codeforces990D - Graph And Its Complement

链接http://codeforces.com/problemset/problem/990/D

构造类型的题目。

这道题目要求构造一个无向图,使得原图和补图的极大连通子图符合要求的数量。

这种构造类型的题目,都要去寻找规律和特点(对我来说很难)。看了题解,也算是学习了新知识了。

官方的题解:链接

关键点就是 如果原图或补图有一个图的极大连通子图的数目大于1,则另一个图的极大连通子图的数目一定等于1。证明如下:

假设原图的极大连通子图的数目大于1,u,v为图中的任意一对顶点。

如果u,v位于不同的极大连通子图,那么u,v在补图中是连通的。

如果u,v位于同一个极大连通子图,那么u,v可以与另一个极大连通子图中的同一个点相连,所以也是连通的。

设原图极大连通子图数目为a,补图为b

特殊情况为a=1,b=1。n=2或3时无解,其他情况有解

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#define DEBUG(x) cout<<#x<<" = "<<x<<endl
using namespace std;
const int MAXN=1e3+10;
int n,a,b;
int mat[MAXN][MAXN];
int main()
{
//    freopen("in.txt","r",stdin);
    scanf("%d%d%d",&n,&a,&b);
    if((n==2||n==3)&&a==1&&b==1)printf("NO\n");
    else if(a!=1&&b!=1)printf("NO\n");
    else {
        printf("YES\n");
        int cnt=0;
        if(a>1){
            int cur=1;
            while(cur<=n-a){//使用首尾相连的方法构图
                mat[cur-1][cur]=mat[cur][cur-1]=1;
                cur++;
            }
        }
        else if(b>1){
            int cur=1;
            while(cur<=n-b){
                mat[cur-1][cur]=mat[cur][cur-1]=1;
                cur++;
            }
            for(int i=0;i<n ;i++ ){
                for(int j=0;j<n ;j++ ){
                    if(i!=j)mat[i][j]=1-mat[i][j];
                }
            }
        }
        else {
            int cur=1;
            while(cur<n){
                mat[cur-1][cur]=1;
                mat[cur][cur-1]=1;
                cur++;
            }
        }
        for(int i=0;i<n ;i++ ){
            for(int j=0;j<n ;j++ ){
                printf("%d",mat[i][j]);
            }
            printf("\n");
        }
    }
}

Let‘s prove that if a>1a>1, then b=1b=1. Let GG be the original graph, and HH — the complement of the graph GG. Let‘s look at each pair of vertices (u,v)(u,v). If uu and vv belong to different components of the graph GG, then there is an edge between them in the graph HH. Otherwise, uu and vv belong to the same component of the graph GG, but since GG has more than one component, there is vertex xx in other component of GG, and there are edges {u,x}{u,x} and {v,x}{v,x} in HH. That‘s why, there is a connected path for any pair of vertices (u,v)(u,v), and the graph HH is connected. Similarly, the case b>1b>1 is proved.

So, if min(a,b)>1min(a,b)>1, then the answer is "NO". Otherwise, min(a,b)=1min(a,b)=1. Consider the case where b=1b=1 (if b>ab>a, we can swap aa and bb, and output complement of the constructed graph). To have aa components in the graph GG, it is enough to connect the vertex 11 with the vertex 22, the vertex 22 with the vertex 33, ??, the vertex n?an?a with the vertex n?a+1n?a+1.

A particular cases are the tests n=2,a=1,b=1n=2,a=1,b=1 and n=3,a=1,b=1n=3,a=1,b=1. There is no suitable graph for them.

原文地址:https://www.cnblogs.com/MalcolmMeng/p/9692161.html

时间: 2024-10-29 18:05:10

codeforces990D - Graph And Its Complement的相关文章

CodeForces - 990D Graph And Its Complement

这道题是一道构造 可惜我看了题解才会 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; inline int read(){ int x=0,f=1,ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch&

hdu 5876 Sparse Graph 无权图bfs求最短路

Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if

2016大连网络赛 Sparse Graph

Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if

HDU 5876 Sparse Graph BFS 最短路

Sparse Graph Problem Description In graph theory, the complement 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 notadjacent in G. Now you are given an undirected graph G of N n

HDU 5876:Sparse Graph(BFS)

http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description In graph theory, the complement 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

Awesome Machine Learning

Awesome Machine Learning  A curated list of awesome machine learning frameworks, libraries and software (by language). Inspired by awesome-php. If you want to contribute to this list (please do), send me a pull request or contact me @josephmisiti Als

HDOJ5876(补图的最短路)

Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1563    Accepted Submission(s): 549 Problem Description In graph theory, the complement of a graph G is a graph H on the same verti

机器学习框架、库

C General-Purpose Machine Learning Computer Vision C++ Computer Vision General-Purpose Machine Learning Natural Language Processing Sequence Analysis Common Lisp General-Purpose Machine Learning Clojure Natural Language Processing General-Purpose Mac

HDU 5876 补图 单源 最短路

---恢复内容开始--- Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 2590    Accepted Submission(s): 902 Problem Description In graph theory, the complement of a graph G is a graph H on t